基于JavaScript實(shí)現(xiàn)購物網(wǎng)站商品放大鏡效果
大家?guī)缀醵加性诰W(wǎng)上購物的經(jīng)驗(yàn),有的網(wǎng)站會有一個商品放大鏡功能,就是把鼠標(biāo)移到圖片上的時候,旁邊會有另外一張大的圖片展示,等同于放大鏡效果,那這樣的效果怎樣實(shí)現(xiàn)的呢,我把代碼發(fā)給大家,請大家參考。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>放大鏡</title> <style> *{margin:0;padding: 0;} #warp{width: 1184px;height:500px;margin:50px auto 0;background-color: #ccc;overflow: hidden;padding: 10px;position: relative;} #warp #minbox{width: 350px;height: 350px;float: left;position: relative;} #maxbox{width: 400px;height: 400px;position: absolute;left: 380px;overflow: hidden;display: none;} #maxbox img{width: 800px;height: 800px;position: absolute;} #con{float: left;margin-left: 20px;} #meng{width: 175px;height: 175px;position: absolute;background-color:yellow;opacity:0.4;filter:alpha(opacity=40);left: 0;top: 0;display: none;} </style> </head> <body> <div id="warp"> <div id="minbox"> <img src="images/min.jpg" alt=""> <p id="meng"></p> </div> <div id="maxbox"> <img src="images/max.jpg" alt=""> </div> <div id="con"> <img src="images/msg.png" alt=""> </div> </div> <script> var minbox=document.getElementById('minbox'); var meng=document.getElementById('meng'); var maxbox=document.getElementById('maxbox'); var maximg=maxbox.getElementsByTagName('img')[0]; var minimg=minbox.getElementsByTagName('img')[0]; function offsetTL(obj){ var ofL=0,ofT=0; while(obj){ ofL+=obj.offsetLeft+obj.clientLeft; ofT+=obj.offsetTop+obj.clientTop; obj=obj.offsetParent; } return{left:ofL,top:ofT}; } minbox.onmousemove=function(e){ var e=e||window.event; meng.style.display='block'; maxbox.style.display='block'; var niubi1=e.clientX-offsetTL(minbox).left-meng.clientWidth/2;//蒙板的X坐標(biāo) var niubi2=e.clientY-offsetTL(minbox).top-meng.clientHeight/2;//蒙板的Y坐標(biāo) var bili=maximg.clientWidth/minimg.clientWidth; if (niubi1<=0) { niubi1=0; }else if (niubi1>=minbox.clientWidth-meng.clientWidth) { niubi1=minbox.clientWidth-meng.clientWidth; } if (niubi2<=0) { niubi2=0; }else if (niubi2>=minbox.clientHeight-meng.clientHeight) { niubi2=minbox.clientHeight-meng.clientHeight; } console.log(niubi1); console.log(niubi2); meng.style.left=niubi1+'px'; meng.style.top=niubi2+'px'; maximg.style.left=-parseInt(meng.style.left)*bili+'px'; maximg.style.top=-parseInt(meng.style.top)*bili+'px'; } minbox.onmouseout=function(){ meng.style.display='none'; maxbox.style.display='none'; } </script> </body> </html>
以上所述是小編給大家介紹的基于JavaScript實(shí)現(xiàn)購物網(wǎng)站商品放大鏡效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
javascript實(shí)現(xiàn)的右下角彈窗實(shí)例
這篇文章主要介紹了javascript實(shí)現(xiàn)的右下角彈窗,實(shí)例分析了javascript實(shí)現(xiàn)右下角彈窗的完整實(shí)現(xiàn)步驟與技巧,需要的朋友可以參考下2015-04-04BOM系列第一篇之定時器setTimeout和setInterval
這篇文章主要介紹了BOM系列第一篇之定時器setTimeout和setInterval 的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-08-08純JS實(shí)現(xiàn)的讀取excel文件內(nèi)容功能示例【支持所有瀏覽器】
這篇文章主要介紹了純JS實(shí)現(xiàn)的讀取excel文件內(nèi)容功能,結(jié)合實(shí)例形式分析了基于js相關(guān)插件進(jìn)行Excel文件讀取的相關(guān)操作技巧,需要的朋友可以參考下2018-06-06小程序使用watch監(jiān)聽數(shù)據(jù)變化的方法詳解
這篇文章主要介紹了小程序使用watch監(jiān)聽數(shù)據(jù)變化的方法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-09-09