html页面里面如何实现点击小图放大查看大图

html-css011

html页面里面如何实现点击小图放大查看大图,第1张

需要准备的材料分别有:电脑、浏览器、html编辑器。

1、首先,打开html编辑器,新建html文件,例如:index.html。

2、在index.html中的<style>标签中,输入css代码:

div {width: 72pxheight: 72pxbackground: url(small3.png) no-repeatoverflow: hidden}

3、浏览器运行index.html页面,此时成功将矩形图片只显示了正方形部分。

4、点击小图后,成功显示了大图。

1、通过css引入的图片,可以通过background-szie属性调整图片大小,background-size的语法如下,可以通过length,percentage,cover及contain四个之中的任一个来调整整改尺寸。

2、将length设置为500px auto;同时添加background-repeat属性,其值为no-repeat。注意:如果不加background-repeat:no-repeat属性,则图像会重复显示。

3、将percentage设置为50% auto。注意:高度设置为auto后,图片会根据具体宽度等比例调整高度,图片不会变形。

html中图片以中心放大的代码如下:

<div style=" width:300px height:300pxmargin-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:pointerwidth:300pxheight: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>

扩展资料:

在html中用js实现鼠标指向图片时图片放大的效果的代码如下:

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

src="你的图片路径" style="width:100pxheight:100px" />

<script type="text/javascript">

var img = document.getElementById('img')

function bigger(){  img.style.width = '400px'  img.style.height = '400px' }

function smaller(){  img.style.width = '100px'  img.style.height = '100px' }

</script>