原生js實(shí)現(xiàn)商品放大鏡效果
實(shí)現(xiàn)原理
大圖上的放大鏡:小圖的顯示區(qū)域=大圖片大小:小圖片大小=大圖片的offsetLeft:小圖片的offsetLeft
那么以上的公式中只有大圖片的offsetLeft 是未知的,所以大圖片的offsetLeft=大圖片大小/小圖片大小*小圖片的offsetLeft
代碼中有詳細(xì)注釋
完整代碼
注:復(fù)制到本地后自行替換圖片查看效果
<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>demo</title> <style> body,h1,h2,h3,h4,h5,h6,hr,p,blockquote,dl,dt,dd,ul,ol,li,pre,form,fieldset,legend,button,input,textarea,th,td{margin:0;padding:0;} h1,h2,h3,h4,h5,h6{font-size:100%;} address,cite,dfn,em,var{font-style:normal;} code,kbd,pre,samp{font-family:courier new,courier,monospace;} ul,ol{list-style:none;} a{text-decoration:none;} a:hover{text-decoration:none;} sup{vertical-align:text-top;} sub{vertical-align:text-bottom;} legend{color:#000;} fieldset,img{border:0;} button,input,select,textarea{font-size:100%;} table{border-collapse:collapse;border-spacing:0;} .clear{clear: both;float: none;height: 0;overflow: hidden;} #demo{display:block;width:400px;height:255px;margin:50px;position:relative;border:1px solid#ccc} #small-box{position:relative;z-index:1} #float-box{display:none;width:160px;height:120px;position:absolute;background:#ffffcc;border:1px solid#ccc;filter:alpha(opacity=50);opacity:0.5} #mark{position:absolute;display:block;width:400px;height:255px;background-color:#fff;filter:alpha(opacity=0);opacity:0;z-index:10} #big-box{display:none;position:absolute;top:0;left:460px;width:400px;height:300px;overflow:hidden;border:1px solid#ccc;z-index:1} #big-box img{position:absolute;z-index:5} </style> </head> <body> <div id="demo"> <div id="small-box"> <div id="mark"></div> <div id="float-box"></div> <img src="images/small.jpg"/> </div> <div id="big-box"> <img src="images/big.jpg"/> </div> </div> <script type="text/javascript"> //在頁面加載完后立即執(zhí)行多個(gè)函數(shù)方案 function addloadEvent(func){ var oldonload=window.onload; if(typeof window.onload !="function"){ window.onload=func; } else{ window.onload=function(){ if(oldonload){ oldonload(); } func(); } } } //在頁面加載完后立即執(zhí)行多個(gè)函數(shù)方案結(jié)束 addloadEvent(b); function b(){ //獲取外圍容器 var demo=document.getElementById("demo"); //獲取小圖片容器 var s_Box=document.getElementById("small-box"); //獲取大圖片容器 var b_Box=document.getElementById("big-box"); //獲取大圖片 var b_Image=b_Box.getElementsByTagName("img")[0]; //獲取放大鏡 var f_Box=document.getElementById("float-box"); //覆蓋在最上面的蓋板為鼠標(biāo)移動(dòng)用 var mark=document.getElementById("mark"); //移入放大鏡和大圖片容器顯示 mark.onmouseover=function(){ f_Box.style.display="block"; b_Box.style.display="block"; } //移出放大鏡和大圖片容器隱藏 mark.onmouseout=function(){ f_Box.style.display="none"; b_Box.style.display="none"; } //移動(dòng)事件 mark.onmousemove=function(ev){ //獲取鼠標(biāo)坐標(biāo)window兼容ie var e=ev||window.event; //當(dāng)前鼠標(biāo)x軸-容器相對(duì)body偏移量-小容器相對(duì)父容器偏移值-放大鏡寬度的一半=放大鏡的當(dāng)前位置 var left=e.clientX-demo.offsetLeft-s_Box.offsetLeft-f_Box.offsetWidth/2; //公式同上 var top=e.clientY-demo.offsetTop-s_Box.offsetTop-f_Box.offsetHeight/2; //判斷當(dāng)放大鏡移出容器時(shí)在邊緣顯示 if(left<0){ left=0; }else if(left>(s_Box.offsetWidth-f_Box.offsetWidth)){ left=s_Box.offsetWidth-f_Box.offsetWidth; } if(top<0){ top=0; }else if(top>(s_Box.offsetHeight-f_Box.offsetHeight)){ top=s_Box.offsetHeight-f_Box.offsetHeight; } //放大鏡當(dāng)前位置 f_Box.style.left=left+"px"; f_Box.style.top=top+"px"; //獲取比例 var z=b_Image.offsetWidth/s_Box.offsetWidth; //用放大鏡偏移量*比例=大圖片的偏移量,方向相反所以負(fù)值 b_Image.style.left=-left*z+"px"; b_Image.style.top=-top*z+"px"; } } </script> </body> </html>
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
JavaScript實(shí)現(xiàn)LRU緩存的三種方式詳解
LRU全稱為L(zhǎng)east?Recently?Used,即最近使用的。針對(duì)的是在有限的內(nèi)存空間內(nèi),只緩存最近使用的數(shù)據(jù)(即get和set的數(shù)據(jù))。本文介紹了JavaScript實(shí)現(xiàn)LRU緩存的三種方式,需要的可以參考一下2022-06-06js抽獎(jiǎng)轉(zhuǎn)盤實(shí)現(xiàn)方法分析
這篇文章主要介紹了js抽獎(jiǎng)轉(zhuǎn)盤實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了js抽獎(jiǎng)轉(zhuǎn)盤原理、實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下2020-05-05關(guān)于List.ToArray()方法的效率測(cè)試
這篇文章主要介紹了關(guān)于List.ToArray()方法的效率測(cè)試的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09JavaScript實(shí)現(xiàn)數(shù)組去重的十種方法分享
去重是開發(fā)中經(jīng)常會(huì)碰到的一個(gè)熱點(diǎn)問題,這篇文章主要介紹了JavaScript中實(shí)現(xiàn)數(shù)組去重的10種方法,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-11-11我要點(diǎn)爆”微信小程序云開發(fā)之項(xiàng)目建立與我的頁面功能實(shí)現(xiàn)
這篇文章主要介紹了我要點(diǎn)爆”微信小程序云開發(fā)之項(xiàng)目建立與我的頁面功能實(shí)現(xiàn),本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-05-05在IE下獲取object(ActiveX)的Param的代碼
在IE下,獲取Param的時(shí)候有個(gè)詭異現(xiàn)象(不知道算不算bug)。2009-09-09