时间:2021-05-18
本文实例为大家分享了js制作淘宝放大镜效果的具体代码,供大家参考,具体内容如下
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> *{ margin: 0; padding: 0; } img{ width: 100%; height: 100%; } #content{ width: auto; height: auto; margin-left: 200px; margin-top: 60px; } #content-left{ position: relative; width: 420px; height: auto; float: left; } #middle{ border: 1px solid #d2d2d2; width: 418px; height: 418px; } #small{ width: 420px; height: auto; } #glass{ position: absolute; width: 200px; height: 200px; background-color: pink; top: 0; opacity: 0.5; z-index: 1; display: none; } ul{ width: inherit; height: 60px; } ul li{ display: inline; height: 60px; list-style: none; float: left; margin: 10px; } #content-right{ position: relative; width: 418px; height: 418px; border: 1px solid #ccc; float: left; margin-left: 10px; overflow: hidden; display: none; } #content-right img{ position: absolute; width: 836px; height: 836px; } </style> </head> <body> <div id="content"> <div id="content-left"> <div id="middle"> <div id="glass"></div> <img src="img/01.jpg"/> </div> <div id="small"> <ul> <li><img src="img/1.jpg"/></li> <li><img src="img/2.jpg"/></li> <li><img src="img/3.jpg"/></li> <li><img src="img/4.jpg"/></li> <li><img src="img/5.jpg"/></li> </ul> </div> </div> <div id="content-right"> <img src="img/001.jpg"/> </div> </div> </body> <script type="text/javascript"> var smallLi = document.getElementById("small").getElementsByTagName("ul")[0].getElementsByTagName("li"); var middle = document.getElementById("middle") var middleImg = middle.getElementsByTagName("img")[0] var contentRight = document.getElementById("content-right"); var bigImg = contentRight.getElementsByTagName("img")[0] var glass = document.getElementById("glass"); //获取放大镜大小 var bigImgWidth = getStyle(bigImg,"width"); var bigImgHeight = getStyle(bigImg,"height"); var contentRightWidth = getStyle(contentRight,"width"); var contentRightHeight = getStyle(contentRight,"height"); var middleWidth = getStyle(middle,"width"); var middleHeight = getStyle(middle,"height"); var glassWidth = contentRightWidth/bigImgWidth*middleWidth; var glassHeight = contentRightHeight/bigImgHeight*middleHeight; var middleBorder = getStyle(middle,"border") glass.style.width = glassWidth + "px"; glass.style.height = glassHeight + "px"; for(var i =0;i<smallLi.length;i++){ (function(index){ smallLi[i].onmouseover = function(){ middleImg.src = "img/0"+index+".jpg"; bigImg.src = "img/00"+index+".jpg" } })((i+1)); } middle.onmouseover = function(){ contentRight.style.display = "block" glass.style.display = "block" middle.onmousemove = function(ev){ var evt = ev||event var x = evt.x - content.offsetLeft -glass.offsetWidth/2 + window.scrollX - middleBorder var y = evt.y - content.offsetTop - glass.offsetHeight/2 + window.scrollY - middleBorder if(x<=middleBorder){ x = middleBorder; } if(x>=middle.offsetWidth-glass.offsetWidth-middleBorder){ x = middle.offsetWidth - glass.offsetWidth -middleBorder; } if(y<=middleBorder){ y = middleBorder; } if(y>=middle.offsetHeight-glass.offsetHeight-middleBorder){ y = middle.offsetHeight - glass.offsetHeight - middleBorder; } glass.style.left = x + "px" glass.style.top = y + "px" bigImg.style.left = -x*middleWidth/glassWidth + middleBorder*2 + "px" bigImg.style.top = -y*middleHeight/glassHeight + middleBorder*2 + "px" console.log(x) console.log(x*middleWidth/glassWidth) } middle.onmouseout = function(){ contentRight.style.display = "none"; glass.style.display = "none"; } } function getStyle(obj,attr){//获取css属性 return (obj.currentStyle)?parseInt(obj.currentStyle[attr]):parseInt(getComputedStyle(obj,false)[attr]); } </script></html>以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
这是我用原生js写的放大镜效果,与各种各样的框架技术相比,我喜欢使用原生的js,在这里,想和大家一起谈谈原生和框架技术的理解与个人喜好。js放大镜效果*{mar
用js实现简单放大镜效果,供大家参考,具体内容如下此处放大镜实现的效果就是当鼠标放置在图片上会有半透明遮罩,图片的一个区域就会被放大,然后展示在右边。当鼠标移动
本文实例为大家分享了js实现淘宝放大镜的具体代码,供大家参考,具体内容如下描述:JS面向对象——淘宝放大镜实现图片的引用是一个大图,一个小图传输用的ajax,记
放大镜效果就是把鼠标移到图片上的时候,旁边会有另外一张大的图片展示,放大镜效果,那这样的效果怎样实现的呢,我把代码发给大家,请大家参考。放大镜*{margin:
用放大镜做投影仪的方法如下: 1、准备好材料,鞋盒,手机,支架,胶水,锯,刀等。 2、把放大镜的手柄锯掉,根据放大镜的尺寸,用圆规在鞋盒的侧面画好一个圆,用