淘寶放大鏡的兩種實現方法JS

啥是淘寶放大鏡

clipboard.png

這個,當你的鼠標移動到左邊的主圖上時,右邊會出現一個放大的圖,暫且就把這個叫作放大鏡吧。html

大概的作法

  • 第一種,左邊一個小圖,右邊一個原圖,當鼠標在小圖上移動的時候,經過更改left和top的值來實現同步移動(原圖的position屬性設置爲absolute)
  • 第二種,鼠標在小圖上移動的時候,經過改變原圖的background-position的值來同步移動。

關鍵操做

  • 第一步,獲取鼠標在小圖上的位置,而且定位好跟隨鼠標的方塊(你應該知道是哪一個方塊吧。。)的位置。
//e.offsetX ,offsetY是鼠標的位置
//方塊的left top在你的鼠標的左上方(網頁左上角是原點),所以是減去一個方塊的一半。
      var x = e.offsetX - 方塊.offsetWidth / 2;
      var y = e.offsetY - 方塊.offsetHeight / 2;
      方塊.style.left = x + 'px';
      方塊.style.top = y + 'px';

這明顯是不足夠的!app

還須要考慮極端位置/狀況
若是隻用上面的設置,那麼當你的鼠標移動到圖片邊緣的時候,方塊有一半會出如今圖片外。ssh

clipboard.png

正確的應該是 當你的方塊觸碰到邊緣的時候,你的方塊就不能在移動了,儘管你的鼠標還在往下圖中「鼠標的有效活動區域」外移動。函數

clipboard.png

那麼得加點代碼測試

if (x < 0) {
        x = 0;
      }
      if (y < 0) {
        y = 0;
      }
      if (x > 小圖.offsetWidth - 方塊.offsetWidth) {
        x = 小圖.offsetWidth - 方塊.offsetWidth;
      }
      if (y > 小圖.offsetHeight - 方塊.offsetHeight) {
        y = 小圖.offsetHeight - 方塊.offsetHeight;
      }
  • 第二步,控制大圖裏的left - top或者background-position
//第一種方法:須要注意的是這裏的left 和 top得反過來,你鼠標在小圖上往下移的時候,對應的大圖實際上是往上移的。
      //因此:大圖上的left = -小圖上的left * 他們的縮放倍率
      大圖.style.display = "block";
      大圖.style.left = -x * 大圖.offsetWidth / 小圖.offsetWidth  + 'px';
      大圖.style.top = -y * 大圖.offsetHeight / 小圖.offsetHeight + 'px';
     
     //第二種方法,這裏須要注意 backgroundPosition的值是從0 - 100%的(得用百分比表示);
     //須要注意的是什麼時候爲百分百,從上面的極端狀況斷定咱們能夠知道
     //x 是從0 到 mask.offsetWidth - rect.offsetWidth;
     //所以這就是0 - 100%;y同理
      大圖.style.display = "block";
      大圖.style.backgroundPosition =`${x/(mask.offsetWidth - rect.offsetWidth)*100}% ${y/(mask.offsetHeight- rect.offsetHeight)*100}%`;

注意事項

  • 咱們上面說在小圖img上綁定mousemove事件來定位方塊,其實實際操做上,咱們不能直接用img來綁定,而是得用一個和img同樣大小遮罩層來綁定,否則在你鼠標移動的時候,圖片會瘋狂閃爍瘋狂!crazy!
  • 還有 就是函數節流,這個想節流就節流吧。
  • 還有個很重要的,就是右邊那個顯示大圖的div的大小,必定得是小圖上的方塊大小 * 縮放倍率 的大小,若是過大,則會多出空白,太小,顯示不徹底。下面有代碼,你能夠帶回家瘋狂測試

再詳細一點

我知道我可能說的不是很詳細,因此。。ui

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>tb放大鏡</title>
  <style>
    .small-box {
      position: relative;
      height: 300px;
    }

    .small-pic {
      width: auto;
      height: 300px;
    }

    .mask {
      width: 526px;
      position: absolute;
      top: 0;
      left: 0;
      z-index: 1;
      height: 100%;
      cursor: crosshair;
    }

    .rect {
      position: absolute;
      top: 0;
      left: 0;
      width: 100px;
      height: 100px;
      opacity: .5;
      background-color: red;
      z-index: 0;
    }

    .big-box {
      display: inline-block;
      position: relative;
      width: 266px;
      height:266px;
      border: 1px solid red;
      overflow: hidden;
    }
    .big-pic {
      position: absolute;
      width: 1400px;
      height: 798px;
      top: 0;
      left: 0;
    }
    .big-pic2{
      display: inline-block;
      width: 266px;
      height:266px;
      background-size: auto 798px;
      background-image: url("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1550846791000&di=15edaf7bce643e13b65f70c82d74de30&imgtype=0&src=http%3A%2F%2Fpic.92to.com%2F360%2F201604%2F08%2F19864861_13.jpg");
      background-position: 0 0;
}
  </style>
</head>

<body>
  <div class="small-box">
    <img class="small-pic"
      src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1550846791000&di=15edaf7bce643e13b65f70c82d74de30&imgtype=0&src=http%3A%2F%2Fpic.92to.com%2F360%2F201604%2F08%2F19864861_13.jpg"
      alt="">
    <div class="mask"></div>
    <div class="rect" style="display: none"></div>
  </div>
  <div class="big-box">
    <img class="big-pic" 
    style="display: none"
      src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1550846791000&di=15edaf7bce643e13b65f70c82d74de30&imgtype=0&src=http%3A%2F%2Fpic.92to.com%2F360%2F201604%2F08%2F19864861_13.jpg"
      alt="">
  </div>
  <div class="big-box">
    <div class="big-pic2"></div>
  </div>
</body>
<script>
  
  window.onload = function () {
    var mask = document.getElementsByClassName('mask')[0];
    //爲何要用mask呢?不直接用選中small-pic。
    //若是直接選擇圖片標籤來綁定下面的mouseover事件,圖片會一直閃爍!因此咱們得給他一個和圖片同樣大小的遮罩層
    var rect = document.getElementsByClassName('rect')[0];
    var bPic = document.getElementsByClassName("big-pic")[0];
    var bPic2 = document.getElementsByClassName("big-pic2")[0];
    mask.addEventListener('mousemove', throttle(magnifier,100))
    function magnifier(e){

      //方塊的left top在你的鼠標的左上方(網頁左上角是原點),所以是減去一個方塊的一半。
      var x = e.offsetX - rect.offsetWidth / 2;
      var y = e.offsetY - rect.offsetHeight / 2;
      //極端狀況,也就是當你的鼠標上的方塊到四個邊的邊緣的時候。
      if (x < 0) {
        x = 0;
      }
      if (y < 0) {
        y = 0;
      }
      if (x > mask.offsetWidth - rect.offsetWidth) {
        x = mask.offsetWidth - rect.offsetWidth;
      }
      if (y > mask.offsetHeight - rect.offsetHeight) {
        y = mask.offsetHeight - rect.offsetHeight;
      }

      //方塊定位
      rect.style.display="block";
      rect.style.left = x + 'px';
      rect.style.top = y + 'px';

      //第一種方法:須要注意的是這裏的left 和 top得反過來,你鼠標在小圖上往下移的時候,對應的大圖實際上是往上移的。
      //因此:大圖上的left = -小圖上的left * 他們的縮放倍率
      bPic.style.display = "block";
      bPic.style.left = -x * bPic.offsetWidth / mask.offsetWidth  + 'px';
      bPic.style.top = -y * bPic.offsetHeight / mask.offsetHeight +  'px';
     
     //第二種方法,這裏須要注意 backgroundPosition的值是從0 - 100%的(得用百分比表示);
     //須要注意的是什麼時候爲百分百,從上面的極端狀況斷定咱們能夠知道
     //x 是從0 到 mask.offsetWidth - rect.offsetWidth;
     //所以這就是0 - 100%;y同理
      bPic2.style.display = "block";
      bPic2.style.backgroundPosition =`${x/(mask.offsetWidth - rect.offsetWidth)*100}% ${y/(mask.offsetHeight- rect.offsetHeight)*100}%`;

    }


    mask.addEventListener('mouseout',function(){
      rect.style.display = "none";
      bPic.style.display = "none";
      bPic2.style.display = "none";
    })


    //函數節流
    function throttle(fn, delay) {
      var pre = new Date().getTime();
      return function () {
        var context = this;
        var args = arguments;
        var now = new Date().getTime();
        if (now - pre > delay) {
          fn.apply(this,arguments);
        }
      }
    }


  }
</script>

</html>

後語

有錯誤的地方請指出,謝謝啦this

相關文章
相關標籤/搜索