温馨提示×

html中点击图片放大怎么实现

清风
13024
2021-03-26 19:00:06
栏目: 编程语言

html中点击图片放大的实现方法:1.创建一个html文件;2.在html文件中添加html代码架构;3.在body标签里面使用img标签添加一张图片以及使用script标签添加函数实现鼠标点击图片放大的效果;4.通过浏览器方式查看设置效果。

html中点击图片放大怎么实现

html中点击图片放大的实现方法:

1.首先创建一个html文件。

2.在html文件中添加html代码架构。

<!DOCTYPE html>

<html>

    <head>

        <title>图片放大</title>

    </head>

    <body>

    </body>

</html>

3.然后在html代码架构中的body标签里面使用img标签添加一张图片以及使用script标签添加一个函数实现鼠标点击图片放大的效果。

<div style=" width:300px; height:300px;margin-left:auto; 

        margin-right:auto; overflow:hidden; margin-top:100px;">       

        <img id="img" onmouseover="bigger()" onmouseout="smaller()"         

        src="http://mat1.gtimg.com/www/images/qq2012/guanjia2.png"         

        style="cursor:pointer;width:300px;height:300px;      

        transition:all 1s ease-out 0s; perspective-origin:bottom;"/>      

        <script type="text/javascript">       

        var img = document.getElementById('img');        

        function bigger(){        

        img.style.width = '400px';        

        img.style.height = '400px';        

        img.style.marginTop = "-50px";        

        img.style.marginLeft = "-50px";        

        }        

        function smaller(){

        img.style.width = '300px';        

        img.style.height = '300px';        

        img.style.marginTop = "0px";        

        img.style.marginLeft = "0px"; 

        }       

        </script>

4.最后可通过浏览器方式阅读html文件查看设计效果。

完整示例代码如下:

<!DOCTYPE html>

<html>

    <head>

        <title>图片放大</title>

    </head>

    <body>

        <div style=" width:300px; height:300px;margin-left:auto; 

        margin-right:auto; overflow:hidden; margin-top:100px;">

        <img id="img" onmouseover="bigger()" onmouseout="smaller()" 

        src="http://mat1.gtimg.com/www/images/qq2012/guanjia2.png" 

        style="cursor:pointer;width:300px;height:300px; 

        transition:all 1s ease-out 0s; perspective-origin:bottom;"/>

        <script type="text/javascript">

        var img = document.getElementById('img');

        function bigger(){

        img.style.width = '400px';

        img.style.height = '400px';

        img.style.marginTop = "-50px";

        img.style.marginLeft = "-50px";

        }

        function smaller(){

        img.style.width = '300px';

        img.style.height = '300px';

        img.style.marginTop = "0px";

        img.style.marginLeft = "0px";

        }

        </script>

    </body>

</html>

0