在html中怎么用js实现鼠标指向图片时图片放大到原图那么大?(具体实现)

html-css026

在html中怎么用js实现鼠标指向图片时图片放大到原图那么大?(具体实现),第1张

可以用js事件“onmouseover”和“onmouseout”来实现。

1、新建html文档,在body标签中添加图片标签,为这个标签设置“id”属性,然后设置图片的默认显示大小css属性:

2、添加“onmouseover”js事件,首先使用“document.getElementById”获取到图片标签,然后定义鼠标移动到图片上时发生的事件,这时图片将会放大:

3、添加“onmouseout”js事件,首先获取图片标签,然后定义鼠标移开图片时发生的事件,这时图片将会缩小:

给你做 第一个,用的是css3和js实现的。

下面的3个都可以用js实现,如果要做的好看一点的话,可以用jquery或者其他的组件

<!DOCTYPE html>

<html>

<head>

<style> 

.move 

{

width:100px

height:100px

background:red

position:relative

animation-name:myfirst

animation-duration:5s

animation-timing-function:linear

animation-delay:0

animation-iteration-count:infinite

animation-direction:alternate

animation-play-state:paused

/* Firefox: */

-moz-animation-name:myfirst

-moz-animation-duration:5s

-moz-animation-timing-function:linear

-moz-animation-delay:0

-moz-animation-iteration-count:infinite

-moz-animation-direction:alternate

-moz-animation-play-state:paused

/* Safari and Chrome: */

-webkit-animation-name:myfirst

-webkit-animation-duration:5s

-webkit-animation-timing-function:linear

-webkit-animation-delay:0

-webkit-animation-iteration-count:infinite

-webkit-animation-direction:alternate

-webkit-animation-play-state:paused

/* Opera: */

-o-animation-name:myfirst

-o-animation-duration:5s

-o-animation-timing-function:linear

-o-animation-delay:0

-o-animation-iteration-count:infinite

-o-animation-direction:alternate

-o-animation-play-state:paused

}

@keyframes myfirst

{

0%   {background:red left:0px top:0px}

25%  {background:yellow left:200px top:0px}

50%  {background:blue left:200px top:200px}

75%  {background:green left:0px top:200px}

100% {background:red left:0px top:0px}

}

@-moz-keyframes myfirst /* Firefox */

{

0%   {background:red left:0px top:0px}

25%  {background:yellow left:200px top:0px}

50%  {background:blue left:200px top:200px}

75%  {background:green left:0px top:200px}

100% {background:red left:0px top:0px}

}

@-webkit-keyframes myfirst /* Safari and Chrome */

{

0%   {background:red left:0px top:0px}

25%  {background:yellow left:200px top:0px}

50%  {background:blue left:200px top:200px}

75%  {background:green left:0px top:200px}

100% {background:red left:0px top:0px}

}

@-o-keyframes myfirst /* Opera */

{

0%   {background:red left:0px top:0px}

25%  {background:yellow left:200px top:0px}

50%  {background:blue left:200px top:200px}

75%  {background:green left:0px top:200px}

100% {background:red left:0px top:0px}

}

.c{

position: absolute

width:300px

height: 300px

border:solid 1px red

}

</style>

</head>

<body>

<p><b>注释:</b>本例在 Internet Explorer 中无效。</p>

<div class="c">

<div class="move" id="move"></div>

</div>

</body>

<script>

window.onload = function(){

document.getElementById("move").onclick = function(){

this.style.animationPlayState = "running"

}

}

</script>

</html>

使用原生js自己写或js库(框架)都是可以的,由于目前HTML5并不是所有的浏览器都完美支持,使用兼容性比较好的js库是个不错的选择。

例如Highcharts图标库就可以实现各类曲线图、折线图、区域图、3D图、柱状图等等。具体使用参考:http://www.hcharts.cn/demo/index.php。

Highcharts中做折线图的demo代码可以作为参考:

<html lang="en">

<head>

  <script type="text/javascript" src="http://cdn.hcharts.cn/jquery/jquery-1.8.3.min.js"></script>

  <script type="text/javascript" src="http://cdn.hcharts.cn/highcharts/highcharts.js"></script>

  <script type="text/javascript" src="http://cdn.hcharts.cn/highcharts/exporting.js"></script>

  <script>

    $(function () {

    $('#container').highcharts({

        chart: {

            type: 'line'

        },

        title: {

            text: 'Monthly Average Temperature'

        },

        subtitle: {

            text: 'Source: WorldClimate.com'

        },

        xAxis: {

            categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

        },

        yAxis: {

            title: {

                text: 'Temperature (°C)'

            }

        },

        tooltip: {

            enabled: false,

            formatter: function() {

                return '<b>'+ this.series.name +'</b><br/>'+this.x +': '+ this.y +'°C'

            }

        },

        plotOptions: {

            line: {

                dataLabels: {

                    enabled: true

                },

                enableMouseTracking: false

            }

        },

        series: [{

            name: 'Tokyo',

            data: [7.0, 6.9, 9.5, 14.5, 18.4, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]

        }, {

            name: 'London',

            data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8]

        }]

    })

})

  </script>

</head>

<body>

  <div id="container" style="min-width:700pxheight:400px"></div>

</body>

</html>