0
点赞
收藏
分享

微信扫一扫

移动端拖拽

凶猛的小白兔 2022-03-17 阅读 129
<!-- 拖拽滑动 -->
<template>
    <div id="item_box" @touchstart="down" @touchmove="move" @touchend="end">
       <i class="iconfont icon-xinxi1"></i>
    </div>
</template>

<script>
export default {
  name: "defaultDrag",
  data() {
    return {
      flags: false,
      position: { x: 0, y: 0 },
      nx: "",
      ny: "",
      dx: "",
      dy: "",
      xPum: "",
      yPum: ""
    };
  },
  methods: {
    goNext() {
      this.$emit("goMove");
    }, // 实现移动端拖拽
    down() {
      let item_box = document.querySelector("#item_box");
      this.flags = true;
      var touch;
      if (event.touches) {
        touch = event.touches[0];
      } else {
        touch = event;
      }
      this.maxW = document.body.clientWidth - item_box.offsetWidth;
      this.maxH = document.body.clientHeight - item_box.offsetHeight;

      this.position.x = touch.clientX - item_box.offsetLeft;
      this.position.y = touch.clientY - item_box.offsetTop;
      this.dx = touch.clientX;
      this.dy = touch.clientY;
    },
    move(event) {
      event.preventDefault();
      let item_box = document.querySelector("#item_box");
      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;

        if (this.nx < 0) {
          this.nx = 0;
        } else if (this.nx > this.maxW) {
          this.nx = this.maxW;
        }

        if (this.ny < 0) {
          this.ny = 0;
        } else if (this.ny >= this.maxH) {
          this.ny = this.maxH;
        }

        item_box.style.left = this.nx + "px";
        item_box.style.top = this.ny + "px";
        document.addEventListener(
          "touchmove",
          function() {
            // event.preventDefault()
          },
          false
        );
      }
    }, //鼠标释放时候的函数
    end() {
      this.flags = false;
    }
  }
};
</script>


<style scoped lang="scss">
#item_box {
  width: 130px;
  height: 130px;
  background-color: #007aff;
  position: fixed;
  right: 30px;
  bottom: 150px;
  border-radius: 130px;
  text-align: center;
  line-height: 130px;
  color: #fff;
  .icon-xinxi1 {
    font-size: 60px;
  }
}
</style>
举报

相关推荐

0 条评论