js實(shí)現(xiàn)淘寶瀏覽商品放大鏡功能
本文實(shí)例為大家分享了js實(shí)現(xiàn)淘寶瀏覽商品放大鏡的具體代碼,供大家參考,具體內(nèi)容如下
1、準(zhǔn)備兩張圖片,其中一張圖片分辨率為另一張圖片的二倍。
2、前端頁(yè)面布局
//box1位左側(cè)原圖,box2為右側(cè)放大圖額顯示框,son為iv class="box1"> <div class="son"></div> <div class="ceng"></div> </div> <div class="box2"></div>
3、js寫(xiě)邏輯
let box1=document.querySelector(".box1"); let box2=document.querySelector(".box2"); let son=document.querySelector(".son"); let ceng=document.querySelector(".ceng"); //鼠標(biāo)移入:son,box2出現(xiàn) ceng.onmouseenter=function(){ son.style.display="block" box2.style.display="block" } //鼠標(biāo)移出:son,box2消失 ceng.onmouseleave=function(){ son.style.display="none" box2.style.display="none" } //鼠標(biāo)移動(dòng):son的位置隨鼠標(biāo)移動(dòng),box2背景圖的位置變化 ceng.onmousemove=function(e){ let sw=e.clientX-son.offsetWidth/2 let sh=e.clientY-son.offsetHeight/2 if(sw>ceng.offsetWidth-son.offsetWidth){ sw=ceng.offsetWidth-son.offsetWidth } else if(sw<=0){ sw=0; } if(sh>ceng.offsetHeight-son.offsetHeight){ sh=ceng.offsetHeight-son.offHeight } else if(sh<=0){ sh=0; } son.style.left=sw+"px" son.style.top=sh+"px" box2.style.backgroundPosition=`${-2*sw}px ${-2*sh}px` }
完整代碼
<!DOCTYPE html> <html lang="en"> <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> </head> <style> *{ margin: 0; padding: 0; } .box1{ width: 500px; height:333px; /* border:1px solid red; */ background: url('shatan.jpg') no-repeat; position: relative; float: left; box-sizing: border-box } .son{ width: 100px; height: 100px; background-color: #fff; position: fixed; left:0; top:0; opacity: 0.7; display: none; } .ceng{ width: 100%; height: 100%; position: absolute; top: 0; left: 0; z-index: 100; } .box2{ width: 200px; height:200px; border:1px solid red; background: url('shatan1.jpg') no-repeat; float: left; display: none; } </style> <body> <div class="box1"> <div class="son"></div> <div class="ceng"></div> </div> <div class="box2"></div> </body> </html> <script> let box1=document.querySelector(".box1"); let box2=document.querySelector(".box2"); let son=document.querySelector(".son"); let ceng=document.querySelector(".ceng"); ceng.onmouseenter=function(){ son.style.display="block" box2.style.display="block" } ceng.onmouseleave=function(){ son.style.display="none" box2.style.display="none" } ceng.onmousemove=function(e){ let sw=e.clientX-son.offsetWidth/2 let sh=e.clientY-son.offsetHeight/2 if(sw>ceng.offsetWidth-son.offsetWidth){ sw=ceng.offsetWidth-son.offsetWidth } else if(sw<=0){ sw=0; } if(sh>ceng.offsetHeight-son.offsetHeight){ sh=ceng.offsetHeight-son.offHeight } else if(sh<=0){ sh=0; } son.style.left=sw+"px" son.style.top=sh+"px" box2.style.backgroundPosition=`${-2*sw}px ${-2*sh}px` } </script>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript實(shí)現(xiàn)網(wǎng)頁(yè)頭部進(jìn)度條刷新
這篇文章主要介紹了JavaScript實(shí)現(xiàn)網(wǎng)頁(yè)頭部進(jìn)度條刷新實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-04-04JS中怎樣判斷undefined(比較不錯(cuò)的方法)
用servlet賦值給html頁(yè)面文本框值后,用alert來(lái)彈出這個(gè)值.結(jié)果顯示"undefined".下面為大家介紹下具體的判斷方法,大家可以參考下2014-03-03JavaScript原生編寫(xiě)《飛機(jī)大戰(zhàn)坦克》游戲完整實(shí)例
飛機(jī)大戰(zhàn)坦克是一款小游戲,相信很多朋友都有玩過(guò),由于最近在深入學(xué)習(xí)Javascript,所以想著用利用Javascript來(lái)實(shí)現(xiàn)這個(gè)游戲,下面這篇文章主要介紹了如何利用JavaScript原生編寫(xiě)《飛機(jī)大戰(zhàn)坦克》游戲,需要的朋友可以參考下2017-01-01隨機(jī)生成10個(gè)不重復(fù)的0-100的數(shù)字(實(shí)例講解)
下面小編就為大家?guī)?lái)一篇隨機(jī)生成10個(gè)不重復(fù)的0-100的數(shù)字(實(shí)例講解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08js父頁(yè)面與子頁(yè)面不同時(shí)顯示的方法
這篇文章主要介紹了js父頁(yè)面與子頁(yè)面不同時(shí)顯示的方法,打開(kāi)一個(gè)頁(yè)面后,父頁(yè)面DISABLE,在子頁(yè)面關(guān)閉后,父頁(yè)面ENABLE,是比較實(shí)用的技巧,需要的朋友可以參考下2014-10-10echarts多條折線圖動(dòng)態(tài)分層的實(shí)現(xiàn)方法
這篇文章主要介紹了echarts多條折線圖動(dòng)態(tài)分層的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05實(shí)現(xiàn)div滾動(dòng)條默認(rèn)最底部以及默認(rèn)最右邊的示例代碼
下面小編就為大家分享一篇實(shí)現(xiàn)div滾動(dòng)條默認(rèn)最底部以及默認(rèn)最右邊的示例代碼,代碼非常簡(jiǎn)潔,具有很好的參考價(jià)值,希望對(duì)大家有所幫助2017-11-11異步動(dòng)態(tài)加載JS并運(yùn)行(示例代碼)
這篇文章主要是對(duì)異步動(dòng)態(tài)加載JS并運(yùn)行的示例代碼進(jìn)行了介紹。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-12-12json對(duì)象與數(shù)組以及轉(zhuǎn)換成js對(duì)象的簡(jiǎn)單實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇json對(duì)象與數(shù)組以及轉(zhuǎn)換成js對(duì)象的簡(jiǎn)單實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06