JS實(shí)現(xiàn)電商商品展示放大鏡特效
本文實(shí)例為大家分享了JS實(shí)現(xiàn)電商商品展示放大鏡的具體代碼,供大家參考,具體內(nèi)容如下
知識點(diǎn)
1.使用children獲取字標(biāo)簽組
2.求當(dāng)前鼠標(biāo)坐標(biāo)
3.碰撞檢測
4.大小盒子通過比例同步
運(yùn)行效果
代碼
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> * { margin: 0; padding: 0; list-style: none; border: none; } #box { width: 350px; height: 350px; margin: 100px 0 0 100px; position: relative; } #box > #small_box { height: 100%; width: 100%; border: 1px solid #cccccc; box-sizing: border-box; position: relative; } #box > #small_box > img { height: 100%; width: 100%; } #box > #small_box > #mask { width: 100px; height: 100px; background-color: rgba(255, 255, 0, .4); position: absolute; left: 0; top: 0; cursor: move; /*隱藏*/ display: none; } #box > #big_box { height: 600px; width: 600px; border: 1px solid #cccccc; position: absolute; left: 360px; top: 0; overflow: hidden; display: none; } #box > #big_box > img{ position: absolute; left: 0; top: 0; } #list { margin: 20px 0 0 100px; } #list ul li { float: left; margin: 5px; cursor: pointer; } </style> </head> <body> <div id="box"> <div id="small_box"> <img src="images/pic001.jpg" alt=""> <span id="mask"></span> </div> <div id="big_box"> <img src="images/pic01.jpg" alt=""> </div> </div> <div id="list"> <ul> <li><img src="images/pic0001.jpg" alt=""></li> <li><img src="images/pic0002.jpg" alt=""></li> <li><img src="images/pic0003.jpg" alt=""></li> <li><img src="images/pic0004.jpg" alt=""></li> </ul> </div> <script src="../../MyTools/MyTools.js"></script> <script> window.addEventListener('load', function (ev) { // 1. 獲取標(biāo)簽 var box = myTools.$('box'), s_box = box.children[0], b_box = box.children[1], mask = s_box.children[1]; var list_Lis = myTools.$('list').getElementsByTagName('li'); b_img = b_box.children[0]; // 2. 監(jiān)聽鼠標(biāo)進(jìn)入小盒子 s_box.addEventListener('mouseover', function (evt) { // 2.1 顯示隱藏內(nèi)容 mask.style.display = 'block'; b_box.style.display = 'block'; // 2.2 監(jiān)聽鼠標(biāo)移動(dòng) s_box.addEventListener('mousemove', function (evt1) { var e = evt1 || window.event; // 2.3 求出鼠標(biāo)坐標(biāo) var pointX = e.pageX - box.offsetLeft - mask.offsetWidth * 0.5; var pointY = e.pageY - box.offsetTop - mask.offsetHeight * 0.5; // 2.4 邊界碰撞檢測 if (pointX < 0) { pointX = 0 } else if (pointX >= s_box.offsetWidth - mask.offsetWidth - 2) { pointX = s_box.offsetWidth - mask.offsetWidth - 2; } if (pointY < 0) { pointY = 0 } else if (pointY >= s_box.offsetHeight - mask.offsetHeight - 2) { pointY = s_box.offsetHeight - mask.offsetHeight - 2; } // 2.5 讓放大鏡走起來 mask.style.left = pointX + 'px'; mask.style.top = pointY + 'px'; // 2.6 讓大盒子中圖片走起來 /* * smallX / bigX = sBox.width / bBox.width * bixX = smallX/(sBox.width / bBox.width) * */ b_img.style.left = -pointX / (s_box.offsetWidth/b_box.offsetWidth) + 'px'; b_img.style.top = -pointY / (s_box.offsetHeight/b_box.offsetHeight) + 'px'; }) }); // 3. 監(jiān)聽鼠標(biāo)離開小盒子 s_box.addEventListener('mouseout', function (evt) { // 2.1 隱藏內(nèi)容 mask.style.display = 'none'; b_box.style.display = 'none'; }); // 4. 監(jiān)聽鼠標(biāo)點(diǎn)擊li標(biāo)簽 for (var i = 0; i < list_Lis.length; i++) { (function (i) { var li = list_Lis[i]; li.addEventListener('mouseover',function (ev1) { s_box.children[0].src = 'images/pic0'+(i+1)+'.jpg'; b_img.src = 'images/pic0'+(i+1)+'.jpg'; }); })(i); } }); </script> </body> </html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
javascript中直接引用Microsoft的COM生成Word
直接引用Microsoft的COM是可以生成Word的,下面為大家介紹下實(shí)現(xiàn)的javascript代碼2014-01-01基于js Canvas實(shí)現(xiàn)二次貝塞爾曲線
這篇文章主要為大家詳細(xì)介紹了基于js Canvas實(shí)現(xiàn)二次貝塞爾曲線,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12微信小程序?qū)崿F(xiàn)點(diǎn)贊、取消點(diǎn)贊功能
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)點(diǎn)贊、取消點(diǎn)贊,和多項(xiàng)點(diǎn)擊功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11JS實(shí)現(xiàn)的簡單輪播圖運(yùn)動(dòng)效果示例
這篇文章主要介紹了JS實(shí)現(xiàn)的簡單輪播圖運(yùn)動(dòng)效果,結(jié)合完整實(shí)例形式分析了javascript基于定時(shí)器動(dòng)態(tài)修改頁面元素屬性的相關(guān)操作技巧,需要的朋友可以參考下2016-12-12JavaScript獲取IP獲取的是IPV6 如何校驗(yàn)
項(xiàng)目中遇到了關(guān)于IPV6的一些問題,特意做一個(gè)專輯說明一下,希望能夠幫助有需要的同學(xué)!2016-06-06詳細(xì)總結(jié)Javascript中的焦點(diǎn)管理
相信大家都知道焦點(diǎn)作為javascript中的一個(gè)重要功能,基本上和頁面交互都離不開焦點(diǎn)。但卻少有人對焦點(diǎn)管理系統(tǒng)地做總結(jié)歸納。本文就javascript中的焦點(diǎn)管理作詳細(xì)介紹,有需要的朋友們可以參考借鑒。2016-09-09JavaScript輸出斐波那契數(shù)列的實(shí)現(xiàn)方法
斐波那契數(shù)列來源于兔子繁殖問題,所以也叫兔子序列,下面這篇文章主要給大家介紹了關(guān)于JavaScript輸出斐波那契數(shù)列的實(shí)現(xiàn)方法,需要的朋友可以參考下2021-06-06iframe窗口高度自適應(yīng)的實(shí)現(xiàn)方法
這篇文章主要介紹了iframe窗口高度自適應(yīng)的實(shí)現(xiàn)方法,有需要的朋友可以參考一下2014-01-01