国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > WEB開發 > 正文

【原生JS】簡單放大鏡效果

2024-04-27 15:14:58
字體:
來源:轉載
供稿:網友

這里寫圖片描述

演示地址:點我

<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> <style type="text/CSS"> * { margin: 0; padding: 0; } img{ vertical-align: top; } .fdj { width: 350px; height: 350px; position: relative; margin: 100px auto; border: 1px solid gainsboro; } .small { position: relative; } .small img { width: 350px; } .mask { width: 100px; height: 100px; background: rgba(255, 255, 0, 0.4); position: absolute; left: 0; top: 0; cursor: move; display: none; } .big { position: absolute; top: 0; left: 360px; width: 500px; height: 500px; border: 1px solid gainsboro; overflow: hidden; display: none; } .big img{ position: absolute; left: 0; top: 0; } </style> </head> <body> <div class="fdj"> <div class="small"> <img src="http://cdn.attach.qdfuns.com/notes/pics/201702/08/162503mw0fawb5b02va22i.jpg" /> <div class="mask"></div> </div> <div class="big"> <img src="http://cdn.attach.qdfuns.com/notes/pics/201702/08/162503mw0fawb5b02va22i.jpg" /> </div> </div> </body> <script type="text/javascript"> var fdj = document.querySelector('.fdj') // 獲得最大的盒子 var small = document.querySelector('.small'); //獲取小圖片盒子 var big = document.querySelector('.big'); //獲取大圖片盒子 var bigs = big.children[0] //大圖片 var smalls = small.children[0] //小圖片 var mask = small.children[1]; //遮罩 //鼠標移入小圖片盒子 small.onmouSEOver = function() { //鼠標移入圖片盒子將遮罩與大圖片顯示 mask.style.display = 'block'; big.style.display = ' block'; }; //鼠標移出小圖片盒子 small.onmouseout = function() { //鼠標移出小圖片盒子將遮罩與大圖片隱藏 mask.style.display = 'none'; big.style.display = 'none'; }; var x=0; var y=0; //鼠標在小圖片上移動時 small.onmousemove = function(ev) { var ev = event || window.event; //讓鼠標在遮罩正中 //鼠標X坐標與Y坐標 x = ev.clientX -this.offsetParent.offsetLeft- mask.offsetWidth / 2 ; y = ev.clientY -this.offsetParent.offsetTop- mask.offsetHeight / 2 ; //將遮罩限制在小圖片盒子中 if (x<0) { x=0; }else if(x>small.offsetWidth-mask.offsetWidth){ x = small.offsetWidth-mask.offsetWidth; } if(y<0){ y=0; }else if(y>small.offsetHeight-mask.offsetHeight){ y= small.offsetHeight-mask.offsetHeight } mask.style.left = x + 'px'; mask.style.top = y + 'px'; //大圖與小圖的比例 /*var scalX = bigs.offsetWidth/small.offsetWidth; var scalY = bigs.offsetHeight/small.offsetHeight;*/ var scalX = x/(small.offsetWidth-mask.offsetWidth); var scalY = y/(small.offsetHeight-mask.offsetHeight); bigs.style.left = -(x*scalX)+'px'; bigs.style.top = -(y*scalY)+'px'; }; </script></html>
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 扎兰屯市| 兴城市| 祁门县| 左云县| 贡山| 勐海县| 文昌市| 沧州市| 璧山县| 阳曲县| 大余县| 留坝县| 洮南市| 唐河县| 罗平县| 开远市| 惠州市| 张家港市| 哈尔滨市| 漯河市| 高淳县| 布尔津县| 新田县| 赣州市| 砚山县| 湖州市| 临沂市| 华阴市| 库尔勒市| 辽中县| 张家界市| 泸水县| 平乡县| 宁城县| 柳林县| 太康县| 宁晋县| 丰城市| 晋江市| 大渡口区| 临江市|