本文實(shí)例為大家分享了js放大鏡效果展示的具體代碼,供大家參考,具體內(nèi)容如下
成都創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),沙河企業(yè)網(wǎng)站建設(shè),沙河品牌網(wǎng)站建設(shè),網(wǎng)站定制,沙河網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷(xiāo),網(wǎng)絡(luò)優(yōu)化,沙河網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專(zhuān)業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
具體代碼如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> body,div,img{ margin:0; padding:0; } img{ display:block; border:none; } #box{ position:absolute; top:20px; left:20px; width:350px; height:350px; box-shadow:3px 3px 10px 0 #111111; } #box img{ width:100%; height:100%; } #mark{ display:none; position:absolute; top:0; left:0; width:175px; height:175px; background:#000; opacity: 0.5; filter:alpha(opacity=50); cursor:move; } #boxRight{ display:none; position:absolute; top:20px; left:380px; width:350px; height:350px; overflow:hidden; } /*我們右側(cè)的圖片的大小是需要嚴(yán)格計(jì)算的: mark的width是box的width的一半,那么我們的大圖寬度也應(yīng)該是小圖的二倍 */ #boxRight img{ position:absolute; top:0; left:0; width:200%; height:200%; } </style> </head> <body> <div id='box'> <img src="img/iphone.jpg" alt=""> <div id='mark'></div> </div> <div id='boxRight'> <img src="img/iphone_big.jpg" alt=""> </div> <script> //放大鏡的原理: 我們的mark橫向是box的一半,縱向也是box的一半,那么右側(cè)的大圖橫向(縱向)應(yīng)該是左側(cè)小圖的2倍 var box = document.getElementById('box'); var mark = document.getElementById('mark'); var boxRight = document.getElementById('boxRight'); //設(shè)置mark這個(gè)盒子 function setPosition(e){ var top = e.clientY - box.offsetTop - (mark.offsetHeight/2); var left = e.clientX - box.offsetLeft - (mark.offsetWidth/2); //邊界判斷 var tempL = 0,tempT = 0; var minL = 0,minT = 0,maxL = box.offsetWidth - mark.offsetWidth,maxT = box.offsetHeight - mark.offsetHeight ; if(left<minL){ mark.style.left = minL + "px"; tempL = minL; }else if(left>maxL){ mark.style.left = maxL + "px"; tempL = maxL; }else{ mark.style.left = left + "px"; tempL = left; } if(top<minT){ mark.style.top = minT + "px"; tempT = minT; }else if(top>maxT){ mark.style.top = maxT + "px"; tempT = maxT; }else{ mark.style.top = top + "px"; tempT = top; } //右側(cè)圖片跟著運(yùn)動(dòng):左側(cè)移動(dòng)多少,右側(cè)跟著移動(dòng)他的2倍即可 var oImg = boxRight.getElementsByTagName("img")[0]; oImg.style.left = -tempL*2 + "px"; oImg.style.top = -tempT*2 + "px"; } box.onmouseenter = function(e){ e = e || window.event; mark.style.display = "block"; setPosition(e); boxRight.style.display = "block"; } box.onmousemove = function(e){ e = e || window.event; setPosition(e); } box.onmouseleave = function(e){ e = e || window.event; mark.style.display = "none"; boxRight.style.display = "none"; } </script> </body> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
文章標(biāo)題:簡(jiǎn)單實(shí)現(xiàn)js放大鏡效果
鏈接地址:http://www.chinadenli.net/article6/pgidig.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、標(biāo)簽優(yōu)化、軟件開(kāi)發(fā)、App設(shè)計(jì)、App開(kāi)發(fā)、搜索引擎優(yōu)化
聲明:本網(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)