国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

原生js實現商品放大鏡效果

2019-11-19 17:59:27
字體:
來源:轉載
供稿:網友

實現原理

大圖上的放大鏡:小圖的顯示區(qū)域=大圖片大?。盒D片大小=大圖片的offsetLeft:小圖片的offsetLeft

那么以上的公式中只有大圖片的offsetLeft 是未知的,所以大圖片的offsetLeft=大圖片大小/小圖片大小*小圖片的offsetLeft

代碼中有詳細注釋

完整代碼

注:復制到本地后自行替換圖片查看效果

<!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í)行多個函數方案  function addloadEvent(func){    var oldonload=window.onload;    if(typeof window.onload !="function"){      window.onload=func;    }    else{      window.onload=function(){        if(oldonload){          oldonload();         }        func();      }    }  }  //在頁面加載完后立即執(zhí)行多個函數方案結束  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");   //覆蓋在最上面的蓋板為鼠標移動用   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";   }   //移動事件   mark.onmousemove=function(ev){   //獲取鼠標坐標window兼容ie   var e=ev||window.event;   //當前鼠標x軸-容器相對body偏移量-小容器相對父容器偏移值-放大鏡寬度的一半=放大鏡的當前位置   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;   //判斷當放大鏡移出容器時在邊緣顯示   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;   }   //放大鏡當前位置   f_Box.style.left=left+"px";   f_Box.style.top=top+"px";   //獲取比例   var z=b_Image.offsetWidth/s_Box.offsetWidth;   //用放大鏡偏移量*比例=大圖片的偏移量,方向相反所以負值   b_Image.style.left=-left*z+"px";   b_Image.style.top=-top*z+"px";   }  }</script></body></html>

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持武林網!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 当涂县| 额尔古纳市| 龙江县| 曲阳县| 湟中县| 胶州市| 钦州市| 库车县| 上饶县| 康平县| 怀安县| 海南省| 友谊县| 鱼台县| 杭锦后旗| 东莞市| 长丰县| 新龙县| 嘉峪关市| 马关县| 长治县| 新乡县| 奎屯市| 万荣县| 米脂县| 石景山区| 洮南市| 沁阳市| 酉阳| 高雄县| 互助| 佛山市| 衡阳县| 长丰县| 武山县| 黄浦区| 景德镇市| 东平县| 宝山区| 高阳县| 绿春县|