JS实现在DIV内拖动缩放图片

HTML

  <div id="father">
    <div id="box">
      <img src="https://i04piccdn.sogoucdn.com/5d60177b8e04757a">
      <div id="scale"></div>
    </div>
  </div>

CSS

    #box {
      width: 100px;
      height: 100px;
      background-color: aquamarine;
      position: absolute;
    }

    #father {
      width: 600px;
      height: 500px;
      background-color: rgb(203, 209, 150);
      position: relative;
    }

    img {
      width: 100%;
      height: 100%;
      cursor: move;
    }

    #scale {
      width: 20px;
      height: 20px;
      overflow: hidden;
      cursor: se-resize;
      position: absolute;
      right: 0;
      bottom: 0;
      background-color: rgb(122, 191, 238);
    }

JS

    // father是图片移动缩放的范围, box是装图片的容器,scale是控制缩放的小图标
    let father = document.getElementById('father');
    let box = document.getElementById("box");
    let scale = document.getElementById("scale");

    // 图片移动效果
    box.onmousedown = function (ev) {
      let oEvent = ev;
      // 浏览器有一些图片的默认事件,这里要阻止
      oEvent.preventDefault();
      let disX = oEvent.clientX - box.offsetLeft;
      let disY = oEvent.clientY - box.offsetTop;
      father.onmousemove = function (ev) {
        oEvent = ev;
        oEvent.preventDefault();
        let x = oEvent.clientX - disX;
        let y = oEvent.clientY - disY;

        // 图形移动的边界判断
        x = x <= 0 ? 0 : x;
        x = x >= father.offsetWidth - box.offsetWidth ? father.offsetWidth - box.offsetWidth : x;
        y = y <= 0 ? 0 : y;
        y = y >= father.offsetHeight - box.offsetHeight ? father.offsetHeight - box.offsetHeight : y;
        box.style.left = x + 'px';
        box.style.top = y + 'px';
      }

      // 图形移出父盒子取消移动事件,防止移动过快触发鼠标移出事件,导致鼠标弹起事件失效
      father.onmouseleave = function () {
        father.onmousemove = null;
        father.onmouseup = null;
      }

      // 鼠标弹起后停止移动
      father.onmouseup = function () {
        father.onmousemove = null;
        father.onmouseup = null;
      }
    }

    // 图片缩放效果
    scale.onmousedown = function (e) {
      // 阻止冒泡,避免缩放时触发移动事件
      e.stopPropagation();
      e.preventDefault();
      let pos = {
        'w': box.offsetWidth,
        'h': box.offsetHeight,
        'x': e.clientX,
        'y': e.clientY
      };
      father.onmousemove = function (ev) {
        ev.preventDefault();
        // 设置图片的最小缩放为30*30
        let w = Math.max(30, ev.clientX - pos.x + pos.w)
        let h = Math.max(30, ev.clientY - pos.y + pos.h)

        // 设置图片的最大宽高
        w = w >= father.offsetWidth - box.offsetLeft ? father.offsetWidth - box.offsetLeft : w
        h = h >= father.offsetHeight - box.offsetTop ? father.offsetHeight - box.offsetTop : h
        box.style.width = w + 'px';
        box.style.height = h + 'px';
      }
      father.onmouseleave = function () {
        father.onmousemove = null;
        father.onmouseup = null;
      }
      father.onmouseup = function () {
        father.onmousemove = null;
        father.onmouseup = null;
      }
    }

线上案例

https://codepen.io/dingFY/full/abOyNoe

 

 

文章每周持续更新,可以微信搜索「 前端大集锦 」第一时间阅读,回复【视频】【书籍】领取200G视频资料和30本PDF书籍资料

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
实现移动端图片预览,可以使用一些现成的库,例如PhotoSwipe、viewer.js等,也可以自己实现。 以下是一个简单的实现步骤: 1. 首先,需要监听图片的点击事件,当图片被点击时,显示预览框,并在预览框中显示图片。 2. 接下来,需要实现手势和手势拖动。可以借助Hammer.js等手势库来实现,当用户在图片上进行拖动时,修改图片的样式,实现拖动效果。 3. 双击大功能可以通过监听双击事件来实现,当用户双击图片时,将图片大到一定的比例。 4. 最后,需要注意一些细节问题,例如在时限制最大最小比例,避免图片过小或过大,同时在预览框中显示图片时,需要对图片进行居中处理,以保证用户体验。 下面是一个简单的示例代码: ```html <!-- HTML部分 --> <div class="preview-container"> <img src="your-image-url" class="preview-image"> </div> ``` ```css /* CSS部分 */ .preview-container { position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0, 0, 0, 0.8); display: none; justify-content: center; align-items: center; } .preview-image { max-width: 100%; max-height: 100%; } ``` ```javascript // JavaScript部分 var previewContainer = document.querySelector('.preview-container'); var previewImage = document.querySelector('.preview-image'); // 监听图片点击事件 previewImage.addEventListener('click', function() { previewContainer.style.display = 'flex'; }); // 初始化手势库 var mc = new Hammer.Manager(previewImage); mc.add(new Hammer.Pinch()); mc.add(new Hammer.Pan({ threshold: 0 })); var scale = 1, lastScale = 1; var posX = 0, posY = 0, lastPosX = 0, lastPosY = 0; // 监听手势事件 mc.on('pinchstart', function(e) { lastScale = scale; }); mc.on('pinchmove', function(e) { scale = Math.max(1, Math.min(lastScale * e.scale, 3)); previewImage.style.transform = 'scale(' + scale + ') translate(' + posX + 'px,' + posY + 'px)'; }); mc.on('pinchend', function(e) { lastScale = scale; }); mc.on('panstart', function(e) { lastPosX = posX; lastPosY = posY; }); mc.on('panmove', function(e) { posX = lastPosX + e.deltaX; posY = lastPosY + e.deltaY; previewImage.style.transform = 'scale(' + scale + ') translate(' + posX + 'px,' + posY + 'px)'; }); // 监听双击事件 previewImage.addEventListener('dblclick', function() { if (scale === 1) { scale = 2; previewImage.style.transform = 'scale(' + scale + ') translate(' + posX + 'px,' + posY + 'px)'; } else { scale = 1; posX = 0; posY = 0; previewImage.style.transform = 'scale(' + scale + ') translate(' + posX + 'px,' + posY + 'px)'; } }); // 点击预览框关闭预览 previewContainer.addEventListener('click', function() { previewContainer.style.display = 'none'; scale = 1; posX = 0; posY = 0; previewImage.style.transform = 'scale(' + scale + ') translate(' + posX + 'px,' + posY + 'px)'; }); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@Demi

您的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值