本文實(shí)例講述了原生JavaScript實(shí)現(xiàn)的簡(jiǎn)單放大鏡效果。分享給大家供大家參考,具體如下:
原理: 其實(shí)所謂的放大就是準(zhǔn)備兩張一樣的圖片,除大小不一樣。鼠標(biāo)移動(dòng)到不同位置,將會(huì)顯示大圖片對(duì)應(yīng)的圖片內(nèi)容。
完整代碼:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>放大鏡效果</title></head><body> <div id="wrap" style="position: relative;width: 900px;margin: 0 auto;text-align: center;">  <div id="smallImg" style="width: 400px;height: 400px; position: relative;z-index: 1;">   <img src="small.jpg" style="width: 400px;height: 400px;"/>   <span id="filter" style="width: 200px;height: 200px;background-color: blue;opacity: 0.1;position: absolute;top: 0;left: 0; z-index: 2;cursor: move;display: none;">   <span>  </div>  <div id="bigImg" style="width: 400px;height: 400px;overflow: hidden;position: absolute;right: 0px;top: 0;display: none;">   <img src="large.jpg" style="width: 800px;height:800px; position: absolute;left: 0;top: 0;">  </div> </div> <script type="text/javascript">  var filter = document.getElementById('filter');  var smallImg = document.getElementById('smallImg');  var bigImg = document.getElementById('bigImg');  var wrap = document.getElementById('wrap');  var largeImgs = bigImg.getElementsByTagName('img')[0];  smallImg.onmouseover = function(){   bigImg.style.display = "inline-block";   filter.style.display = "inline-block";  }  smallImg.onmousemove = function(event){   var event = event || window.event;   var mouseleft = event.clientX - wrap.offsetLeft;   var mousetop = event.clientY - wrap.offsetTop;   var left = mouseleft<smallImg.offsetWidth/4?0:mouseleft>smallImg.offsetWidth*3/4?smallImg.offsetWidth/2:(mouseleft - filter.offsetWidth/2);   var top = mousetop<smallImg.offsetHeight/4?0:mousetop>smallImg.offsetHeight*3/4?smallImg.offsetHeight/2:(mousetop - filter.offsetWidth/2);   filter.style.left = left + "px";   filter.style.top = top +"px";   largeImgs.style.left = "-" + left*bigImg.offsetWidth/smallImg.offsetWidth + "px";   largeImgs.style.top = "-" + top*bigImg.offsetHeight/smallImg.offsetHeight + "px";  }  smallImg.onmouseout = function(){   bigImg.style.display = "none";   filter.style.display = "none";  } </script></body></html>運(yùn)行效果:

更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript圖片操作技巧大全》、《JavaScript圖形繪制技巧總結(jié)》、《JavaScript頁(yè)面元素操作技巧總結(jié)》、《JavaScript事件相關(guān)操作與技巧大全》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注