本文實(shí)例為大家分享了JS實(shí)現(xiàn)電商商品展示放大鏡的具體代碼,供大家參考,具體內(nèi)容如下
創(chuàng)新互聯(lián)主要從事成都網(wǎng)站建設(shè)、做網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)云州,10多年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來(lái)電咨詢建站服務(wù):18982081108
知識(shí)點(diǎn)
1.使用children獲取字標(biāo)簽組
2.求當(dāng)前鼠標(biāo)坐標(biāo)
3.碰撞檢測(cè)
4.大小盒子通過(guò)比例同步
運(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 邊界碰撞檢測(cè) 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 讓放大鏡走起來(lái) mask.style.left = pointX + 'px'; mask.style.top = pointY + 'px'; // 2.6 讓大盒子中圖片走起來(lái) /* * 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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
網(wǎng)頁(yè)標(biāo)題:JS實(shí)現(xiàn)電商商品展示放大鏡特效
標(biāo)題URL:http://jinyejixie.com/article42/pdcihc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化、網(wǎng)站改版、小程序開發(fā)、網(wǎng)站制作、網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
營(yíng)銷型網(wǎng)站建設(shè)知識(shí)