js放大鏡放大購物圖片效果,js放大鏡購物圖片

來源:互聯網
上載者:User

js放大鏡放大購物圖片效果,js放大鏡購物圖片

圖片放大鏡效果,供大家參考,具體內容如下

一痛點:不讓黃盒子出界

二痛點:讓大盒子相應移動(比例)

<html lang="en"><head> <meta charset="UTF-8"> <title>我的放大鏡</title> <style>  *{   margin: 0;   padding: 0;  }  .box{   margin: 100px;   position: relative;  }  .small{   width: 350px;   height: 350px;   border: 1px solid #999;   position: relative;  }  .select{   display: none;   width: 100px;   height: 100px;   background: rgba(255,255,0,0.4);   position: absolute;   left: 0;   top: 0;   cursor: move;  }  .big{   display: none;   position: absolute;   left: 360px;   top: 0;   width: 450px;   height: 450px;   border: 1px solid #ccc;   overflow: hidden;  }  .big img{   position: absolute;   left: 0;   top: 0;  } </style> </head><body> <div class="box">  <div class="small" id="smallbox">   <img src="images/001.jpg" alt="">   <div class="select" id="mask" style="display:none;"></div>  </div>  <div class="big" id="bigbox">   <img src="images/0001.jpg" alt="">  </div> </div> <script>   var smallbox = document.getElementById('smallbox');  var bigbox = document.getElementById('bigbox');  var mask = document.getElementById('mask');  var bigImg = bigbox.children[0];  smallbox.onmouseover = function(){   mask.style.display = "block";   bigbox.style.display = "block";  }  smallbox.onmouseout = function(){   mask.style.display = "none";   bigbox.style.display = "none";  }  smallbox.onmousemove = function(event){   var event = event || window.event;   var x = event.clientX - this.offsetParent.offsetLeft - mask.offsetWidth/2;   var y = event.clientY - this.offsetParent.offsetTop - mask.offsetHeight/2;   //不讓黃盒子出界   if(x < 0){    x = 0;   }else if(x > smallbox.offsetWidth - mask.offsetWidth){    x = smallbox.offsetWidth - mask.offsetWidth;   }   if(y<0) {    y = 0;   }else if(y > smallbox.offsetHeight - mask.offsetHeight){     y = smallbox.offsetHeight - mask.offsetHeight;   }   mask.style.left = x + "px";   mask.style.top = y + "px";   bigImg.style.left = -x/smallbox.offsetWidth * bigbox.offsetWidth + "px"; //注意是-x   bigImg.style.top = -y/smallbox.offsetHeight * bigbox.offsetHeight + "px";  } </script></body></html>

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.