vue移动端实现拖拽

前言:

前言:最近一直忙于生活中的事情,今天有时间写个日记吧=吧,在开发移动端的时候经常用到拖拽模块 ,移动端拖拽事件也是开发中经常用到的情况,所以现在自己整一个,废话不多说


代码部分:

在components里面创建一个公共组件dragMove.vue

写下如上代码:(你们直接复制就可以使用)

<!-- 拖拽滑动效果 -->
<template>
  <div id="item_box" @click="goMove" @touchstart="down" @touchmove="move"         
  @touchend="end">
       //这里注意,我使用了阿里图标,你们使用的话,删除掉写自己的样式就好
    <i class="iconfont icon-a-bianlidian2x"></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  lang="scss" scoped>
#item_box {
  width: 1rem;
  height: 1rem;
  position: fixed;
  z-index: 1000;
  bottom: 60px;
  right: 0.4rem;
  border-radius: 50%;
  border: 2px solid #ffffff;
  box-shadow: 0 0 0.4rem 2px skyblue;
  background: #81f8e8;
  display: flex;
  justify-content: center;
  align-items: center;
  .iconfont {
    color: orange;
  }
}
</style>

然后在你们需要使用拖拽的组件中引入 dragMove.vue子组件  就好  

(不要忘记在components中注册子组件哟)

//:goMove是子传夫的事件,不用执行任何步骤,写到methods中就好
<dragMove  @goMove="watchCar"></dragMove>

效果图如下:

都给俺点赞!!!

  • 15
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

甘大猫

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

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

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

打赏作者

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

抵扣说明:

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

余额充值