美文网首页
实现一张图片跟随鼠标移动,同时在限定移动范围

实现一张图片跟随鼠标移动,同时在限定移动范围

作者: 仰寒天 | 来源:发表于2024-07-01 10:42 被阅读0次

dom层:

<div class="poster" ref="posterRef">
        <img
          :src="uploadImg.link"
          alt=""
          class="bg-img"
          draggable="false"
          v-if="uploadImg.link"
        />
        <img
          src="@/assets/img/text-code.jpg"
          alt=""
          ref="codeRef"
          :style="{
            left: leftX + 'px',
            top: topY + 'px',
            width: qrcodeWidth + 'px',
            height: qrcodeWidth + 'px',
          }"
          class="code-img"
          @mousedown.prevent="handleMouseDown"
          v-if="uploadImg.link"
        />
      </div>

样式:

.poster {
  position: relative;
  width: 528px;
  height: 834px;
  background-color: #d9d9d9;
  .bg-img {
    width: 100%;
    height: 100%;
  }
  .code-img {
    position: absolute;
    width: 212px;
    height: 212px;
    border-radius: 50%;
  }
}

js:

handleMouseDown(event) {
      // 1,计算位置差
      // 因为clientX和offsetLeft的属性返回的位置不一样 要相减得到鼠标在拖动元素内实际点击的位置
      // 后面每一次拖动时都要减去这个差值 否则就会造成你拖动的位置一直都是元素的左上角 而不是你之前点击的位置
      this.disX = event.clientX - this.codeimg.offsetLeft
      this.disY = event.clientY - this.codeimg.offsetTop

      //2, 获取拖动元素的高度和容器的高度 为了下面进行限制元素拖动的范围
      let dragHeight = this.codeimg.offsetHeight
      let dragWidth = this.codeimg.offsetWidth
      let dragContainerWidth = this.poster.offsetWidth //获取容器的高度和宽度
      let dragContainerHeight = this.poster.offsetHeight

      // 添加鼠标移动事件
      document.onmousemove = (el) => {
        // 3,获取鼠标移动位置
        let moveX = el.clientX - this.disX
        let moveY = el.clientY - this.disY

        // 4,限制拖动
        //控制范围:在元素 被拖拽的过程中 判断 元素的定位值 是否到达边界 如果到了 就不能在走了
        // 左边界
        if (moveX <= 0) {
          moveX = 0
        }
        // 上边界
        if (moveY <= 0) {
          moveY = 0
        }
        // 下边界  容器高度 - 拖动元素高度
        if (moveY >= dragContainerHeight - dragHeight) {
          moveY = dragContainerHeight - dragHeight
        }

        //右边界   容器宽度 - 拖动元素宽度
        if (moveX >= dragContainerWidth - dragWidth) {
          moveX = dragContainerWidth - dragWidth
        }

        // 5, 开始移动
        this.leftX = moveX
        this.topY = moveY
      }
      /* 6,鼠标抬起解除事件 */
      document.onmouseup = () => {
        document.onmousemove = null
      }
    },

相关文章

  • js实现图片跟随鼠标移动

    这次做的是和鼠标移动然后不断获取坐标相关的一个小功能。 这里视频放不了就不放了,大家可以直接复制代码运行就行 这次...

  • 鼠标指针实现拖拽

    实现思路: 移动对象绝对定位 计算移动距离 移动对象跟随鼠标 添加 JS 监听按下松开 需要补充的知识:docum...

  • 鼠标跟随移动

    document.onmousemove = function(ev){ var div = document.g...

  • js原生拖拽的2种实现

    (1)实现在规定区域内跟随鼠标移动(鼠标事件实现) (2)拖放效果(drag事件实现) dragenter和dra...

  • Unity 制作简易虚拟摇杆

    计算方法 制作摇杆有两个需要考虑的点: 摇杆跟随鼠标移动 松开鼠标后,摇杆位置归为初始位置。 摇杆的移动范围要在圆...

  • js-图片跟随鼠标移动

    事件的兼容性代码 使用:

  • UI拖拽

    实现目标 点住一个Image,这个Image能够跟随鼠标/触摸点的移动,松开停止移动。 EventSystem中有...

  • Vue 实现可拖拽弹窗

    一、实现原理 1、获取鼠标在div中的位置2、设置 div 的 left 和 top 使其跟随鼠标位置移动,达到拖...

  • 放大镜效果

    核心原理1、鼠标经过遮罩层显示,离开隐藏;2、移动鼠标遮罩层跟随,鼠标超出大盒子后遮罩层不跟随出框;3、移动遮罩层...

  • 2020-11-23图片跟随鼠标练习&&icon制作

    功能 图片会跟随鼠标一起移动 基本思路 先通过document.addEventListener('mousemo...

网友评论

      本文标题:实现一张图片跟随鼠标移动,同时在限定移动范围

      本文链接:https://www.haomeiwen.com/subject/mdjmcjtx.html