美文网首页
vue悬浮图标拖拽

vue悬浮图标拖拽

作者: 依然_8deb | 来源:发表于2022-10-27 16:29 被阅读0次
    <div class="service" @click="toKefu" @mousedown="down" @touchstart="down" @mousemove="move"
                @touchmove="move" @mouseup="end" @touchend="end" ref="kefu">
        <img src="../assets/images/icon-service.png" />
      </div>
    data() {
        return {
          flags: false, //控制使用
          position: {
              x: 0,
              y: 0,
          },
          nx: "",
          ny: "",
          dx: "",
          dy: "",
          xPum: "",
          yPum: "",
        };
      }
    methods: {
    down() {
          this.flags = true;
          var touch;
          if (event.touches) {
            touch = event.touches[0];
          } else {
            touch = event;
          }
          this.position.x = touch.clientX;
          this.position.y = touch.clientY;
          this.dx = this.$refs.kefu.offsetLeft;
          this.dy = this.$refs.kefu.offsetTop;
        },
        move() {
          if (this.flags) {
            var touch;
            if (event.touches) {
              touch = event.touches[0];
            } else {
              touch = event;
            }
            this.nx = touch.clientX - this.position.x;
            this.ny = touch.clientY - this.position.y;
            this.xPum = this.dx + this.nx;
            this.yPum = this.dy + this.ny;
            let width = window.innerWidth - this.$refs.kefu.offsetWidth; //屏幕宽度减去自身控件宽度
            let height = window.innerHeight - this.$refs.kefu.offsetHeight; //屏幕高度减去自身控件高度
            this.xPum < 0 && (this.xPum = 0);
            this.yPum < 0 && (this.yPum = 0);
            this.xPum > width && (this.xPum = width);
            this.yPum > height && (this.yPum = height);
            // if (this.xPum >= 0 && this.yPum >= 0 && this.xPum<= width &&this.yPum<= height) {
            this.$refs.kefu.style.left = this.xPum + "px";
            this.$refs.kefu.style.top = this.yPum + "px";
            // }
            //阻止页面的滑动默认事件
            document.addEventListener(
              "touchmove",
              function () {
                event.preventDefault();
              },
              false
            );
          }
        },
        //鼠标释放时候的函数
        end() {
          this.flags = false;
        },
    }
      
    

    样式表中要增加

    * {
        touch-action:manipulation
    }
    

    相关文章

      网友评论

          本文标题:vue悬浮图标拖拽

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