国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

用js實現放大鏡效果

瀏覽:107日期:2024-04-14 08:37:52

本文實例為大家分享了js實現放大鏡效果的具體代碼,供大家參考,具體內容如下

該放大區域用背景圖片放大

<!DOCTYPE html><html lang='zh'> <head> <meta charset='UTF-8'> <meta name='viewport' content='width=device-width, initial-scale=1.0'> <meta http-equiv='X-UA-Compatible' content='ie=edge'> <title></title> <style type='text/css'> body { height: 1200px; background-color: lightskyblue; } ul { margin: 0; padding: 0; list-style: none; } .itemarea { position: relative; width: 500px; height: 680px; border: 1px black solid; margin: 50px auto; } .itemarea .pic { margin-bottom: 15px; } .itemarea img { width: 500px; height: 600px; } .itemarea .pic .cover { position: absolute; left: 0; top: 0; width: 200px; height: 200px; background-image: url(img/7.png); opacity: 0.6; display: none; } .itemarea .list { display: flex; } .itemarea .list li { margin: auto; } .itemarea .list img { display: block; width: 50px; height: 50px; } .itemarea .detail { position: absolute; top: 0; left: 500px; /* 此處為放大2倍,顯示框的大小是遮陰框寬高的2倍 */ width: 400px; height: 400px; display: none; border: 1px black solid; background: url(img/1.PNG); /* 此處放大2倍,背景圖片的寬高是左邊顯示圖片的2倍 */ background-size: 1000px 1200px; /* background-size: 200%; 或者這樣寫*/ } .itemarea .list .current { border: 2px green solid; } </style> </head> <body> <div class='itemarea'> <div class='pic'> <img src='http://www.cgvv.com.cn/bcjs/img/1.PNG'> <div class='cover'></div> </div> <ul class='list'> <li><img src='http://www.cgvv.com.cn/bcjs/img/1.PNG'></li> <li><img src='http://www.cgvv.com.cn/bcjs/img/2.PNG'></li> <li><img src='http://www.cgvv.com.cn/bcjs/img/3.PNG'></li> <li><img src='http://www.cgvv.com.cn/bcjs/img/4.PNG'></li> <li><img src='http://www.cgvv.com.cn/bcjs/img/5.PNG'></li> <li><img src='http://www.cgvv.com.cn/bcjs/img/6.PNG'></li> </ul> <div class='detail'> </div> </div> <script type='text/javascript'> /* 需求 1,鼠標放入圖片時候,會動態修改圖片地址 2,鼠標放入大圖,會動態修改右邊圖片位置 2.1顯示圖片的放大鏡, 2.2顯示右邊效果 */ var itemarea = document.querySelector('.itemarea'); var list = document.querySelector('.list'); /* 上面的大圖片 */ img = document.querySelector('.pic img'); /* 所有的圖片 */ imgs = list.querySelectorAll('img'); /* 主圖片展示區域 */ pic = document.querySelector('.itemarea .pic'); /* 放大鏡 */ cover = document.querySelector('.cover'); /* 放大的區域 */ detail = document.querySelector('.detail'); /* 監聽事件,切換圖片src */ list.addEventListener('mousemove', function(e) { if (e.target.tagName == 'IMG') { img.src = e.target.src; detail.style.backgroundImage = 'url(' + e.target.src + ')'; /* 遍歷 所有邊框都為空*/ imgs.forEach(function(item) { item.className = ''; }) /* 選中的改變邊框顏色*/ e.target.className = 'current'; } }) pic.addEventListener('mousemove', function(e) { /* 放大鏡距離瀏覽器的距離 */ var x = e.clientX; y = e.clientY; /* 圖片框距離瀏覽器的距離 */ cx = pic.getBoundingClientRect().left; cy = pic.getBoundingClientRect().top; tx = x - cx - 100; ty = y - cy - 100; if (tx < 0) { tx = 0; } if (ty < 0) { ty = 0; } /* 顯示圖片寬-遮陰框的寬 */ if (tx >300) { tx = 300; } /* 顯示圖片高-遮陰框的高 */ if (ty > 400) { ty = 400; } cover.style.left = tx + 'px'; cover.style.top = ty + 'px'; /* 根據遮陰框在盒子的移動距離百分比------對應放映框在大圖片的移動距離百分比 */ /* tx,ty/遮陰框的極限范圍 */ detail.style.backgroundPosition = tx / 300 * 100 + '%' + ty / 400 * 100 + '%'; }) /* 移除隱藏 */ itemarea.onmouseout = function() { cover.style.display = 'none'; detail.style.display = 'none' } itemarea.onmouseover = function() { cover.style.display = 'block'; detail.style.display = 'block'; } </script> </body></html>

效果如下:

用js實現放大鏡效果

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 黄色激情在线 | 色拍拍在精品视频69影院在线 | 国产一区二区三区在线免费观看 | 成人牲交一极毛片 | 成人精品区 | 精品午夜久久网成年网 | 美女张开腿给男人桶 | 国产在线更新 | 欧美激情毛片裸推荐 | 久久国产免费一区 | 国产日韩视频在线观看 | 成年日韩片av在线网站 | 91九色视频无限观看免费 | 一级高清毛片免费a级高清毛片 | 欧美性色xo影院69 | 视频三区精品中文字幕 | 午夜日b视频 | 久久国产美女免费观看精品 | 91成人小视频 | 99re7在线精品免费视频 | 亚洲精品免费在线 | 天天看片天天爽 | 牛牛a级毛片在线播放 | 亚洲综合久| 久久三级毛片 | 午夜爽爽爽男女免费观看hd | 久久两性 | 97在线观看完整免费 | 欧美一级片在线看 | 尤物tv已满18点击进入 | 亚洲午夜精品久久久久久抢 | 国产精品91在线 | 国产真实自拍 | 精品欧美成人高清视频在线观看 | 国产精品亚洲视频 | 美女一级毛片毛片在线播放 | 三级在线国产 | 久久久久久久久久免免费精品 | 亚洲经典在线 | 精品久久久久久久久久久久久久久 | 国产高清精品久久久久久久 |