當前位置:首頁 > IT技術(shù) > 移動平臺 > 正文

圖片放大縮放合移動功能
2021-10-28 15:21:34

封裝個圖片放大的方法,由于測試頁面沒有進行頁面Css reset? 所以會有滾動出現(xiàn)。

需求是點擊車輛全景圖,車牌照或者車輛照片顯示放大圖片

export function showBigImage(url) {
  // // 創(chuàng)建圖片
  var imgObj = new Image();
  imgObj.src = url;

  let bodyW = document.body.clientWidth;
  let bodyH = document.body.clientHeight;

  let size = 0;
  // 創(chuàng)建圖片父級
  let div = document.createElement('div');
  div.setAttribute('style', 'cursor: pointer; display: none;position: fixed; left: 0;top: 0;right: 0;bottom: 0;background:rgba(0,0,0,0.5);z-index: 9999'); // 背景
  div.style.width = bodyW + 'px';
  div.style.height = bodyH + 'px';
  if (typeof (url) !== "function" && url.indexOf("http") === 0) {
    div.style.display = "block";
  }
  div.appendChild(imgObj);
  document.getElementsByTagName('body')[0].appendChild(div);
  // 圖片加載成功之后
  imgObj.onload = function () {
    imgObj.setAttribute('style', `position: relative;`);
    var iw = imgObj.width;
    var ih = imgObj.height;
    var dw = div.style.width.split('p')[0];
    var dh = div.style.height.split('p')[0];
    if (iw >= ih) {
      var nih = (ih / iw) * dw;
      if (nih > dh) {
        this.style.height = dh + 'px';
        this.style.left = (dw - iw / ih * dh) / 2 + 'px';
      } else {
        this.style.width = dw + 'px';
        this.style.top = (dh - nih) / 2 + 'px';
      }
    } else {
      var niw = (dh / ih) * iw;
      this.style.height = dh + 'px';
      this.style.left = (dw - niw) / 2 + 'px';
    }
  };

  // 圖片的滾動放大
  function mWheel(e) {
    let ev = e || window.event;
    // let dir = ev.deltaY;
    let dir = ev.detail ? ev.detail * (-120) : ev.wheelDelta;
    // dir = -dir;
    if (dir > 0) {
      size += dir / 1000;
      imgObj.style.transform = `scale(${1 + size})`;
      // imgObj.style.transform = 'scale('+Number(1 + size )+')';
    } else {
      size += dir / 1000;
      if (size < -1) {
        size = -1;
        imgObj.style.transform = `scale(${1 + size})`;
        return;
      }
      imgObj.style.transform = `scale(${1 + size})`;
    }
  };

  // 判斷瀏覽器是否支持滾動事件
  var mousewheelevt = (/Firefox/i.test(navigator.userAgent)) ? "DOMMouseScroll" : "mousewheel"; // FF doesn't recognize mousewheel as of FF3.x
  // 調(diào)用圖片放大
  if (div.attachEvent) {
    // if IE (and Opera depending on user setting)
    div.attachEvent("on" + mousewheelevt, mWheel);
  } else if (div.addEventListener) {
    // WC3 browsers
    div.addEventListener(mousewheelevt, mWheel, false);
  }

  // 拖動圖片
  div.onmousedown = function (e) {
    var flagInner = true;
    var ev = e || window.event;

    var mPageX = ev.pageX;
    var mPageY = ev.pageY;

    var disX = ev.pageX - imgObj.offsetLeft;
    var disY = ev.pageY - imgObj.offsetTop;
    this.onmousemove = function (e) {
      ev = e || window.event;
      var mPageX2 = ev.pageX;
      var mPageY2 = ev.pageY;
      if ((mPageX2 - mPageX > 5) || (mPageY2 - mPageY > 5) || (mPageX - mPageX2 > 5) || (mPageY - mPageY2 > 5)) {
        flagInner = false;
      }
      ev.preventDefault();
      var moveX = ev.pageX - disX;
      var moveY = ev.pageY - disY;

      imgObj.style.left = moveX + "px";
      imgObj.style.top = moveY + "px";
    };
    this.onmouseup = function (e) {
      if (flagInner) {
        if (this.isIE || this.isIE11) {
          this.removeNode(true);
        } else {
          this.remove();
        }
        this.isIE = function isIE() {
          if (!!window.ActiveXObject || "ActiveXObject" in window) {
            return true;
          } else {
            return false;
          }
        };
        this.isIE11 = function isIE11() {
          if ((/Trident/7./).test(navigator.userAgent)) {
            return true;
          } else {
            return false;
          }
        };
      }
      this.onmousemove = null;
      this.onmouseup = null;
    };
    // disable default wheel action of scrolling page
    if (ev.preventDefault) {
      ev.preventDefault();
    } else {
      return false;
    }
  };
}

?

?

?

?

?

本文摘自 :https://www.cnblogs.com/

開通會員,享受整站包年服務(wù)立即開通 >