美文网首页
vue移动端自定义拖拽组件(亲测有效)

vue移动端自定义拖拽组件(亲测有效)

作者: 紫荆峰 | 来源:发表于2019-04-26 12:08 被阅读0次

一、前言

       有段时间没有些博客了,最近一直忙于工作上的事情,今天正好有时间写一个笔记。移动端拖拽事件是开发中经常用到的情况,正好最近项目中有用到这一情况,在网上也搜索了一些博客,都不太理想,于是就综合我搜集的博客做一个总结,一边将来可以随时查看。

二、正文内容

       话不多说直接上代码
(1)自定义一个公共组件drag.vue

<!-- 拖拽滑动 -->
<template>
  <div id="default_drag_comp"
    @click="goNext"
    @touchstart="down"
    @touchmove="move"
    @touchend="end"
  >
    <span>询价车</span>
  </div>
</template>

<script>
export default {
  name: "defaultDrag",
  data() {
    return {
      flags: false,
      position: { x: 0, y: 0 },
      nx: "",
      ny: "",
      dx: "",
      dy: "",
      xPum: "",
      yPum: ""
    };
  },

  components: {},

  computed: {},

  mounted() {},

  methods: {
    goNext() {
      this.$emit("goNext");
    },
    // 实现移动端拖拽
    down() {
      let default_drag_comp = document.querySelector("#default_drag_comp");
      this.flags = true;
      var touch;
      if (event.touches) {
        touch = event.touches[0];
      } else {
        touch = event;
      }
      this.maxW = document.body.clientWidth - default_drag_comp.offsetWidth;
      this.maxH = document.body.clientHeight - default_drag_comp.offsetHeight;

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

        default_drag_comp.style.left = this.nx + "px";
        default_drag_comp.style.top = this.ny + "px";
        //阻止页面的滑动默认事件;如果碰到滑动问题,1.2 请注意是否获取到 touchmove
        document.addEventListener(
          "touchmove",
          function() {
            // event.preventDefault();
          },
          false
        );
      }
    },
    //鼠标释放时候的函数
    end() {
      this.flags = false;
    }
  }
};
</script>
<style scoped lang="scss">
#default_drag_comp {
  width: 1rem;
  height: 1rem;
  border-radius: 50%;
  border: 2px solid #ffffff;
  box-shadow: 0 0 0.4rem 2px #d3d3d3;
  background: #ff9966;
  position: fixed;
  z-index: 1000;
  bottom: 0.8rem;
  right: 0.4rem;
  display: flex;
  justify-content: center;
  align-items: center;
  span {
    color: #ffffff;
    font-size: 0.24rem;
  }
}
</style>

(2)在需要引用该组件的地方注册一下即可

<drag-comp @goNext="watchCar"/>

(3)结果


TIM截图20190426120529.jpg

三、总结

       好了,到这里了,希望对大家有帮助,谢谢!

相关文章

  • vue移动端自定义拖拽组件(亲测有效)

    一、前言 有段时间没有些博客了,最近一直忙于工作上的事情,今天正好有时间写一个笔记。移动端拖拽事件是开发中经常用到...

  • 封装移动端 vue 拖拽指令

    封装移动端 vue 拖拽指令 通过vue自定义指令,将拖拽行为封装为指令 使用transform做移动效果,需要注...

  • 总结

    1,1)移动端拖拽vue-draggablehttp://www.itxst.com/vue-draggable/...

  • Vue.js 常用 UI 组件库

    Vant有赞前端团队提供的轻量、可靠的移动端 Vue 组件库 Mint UI基于 Vue.js 的移动端组件库 V...

  • 基于Vue + Vant的移动端项目,rem适配问题

    基于Vue + Vant的移动端项目Vant框架尺寸就是px,自定义的组件尺寸用rem 一: 自己计算 以750尺...

  • Vue移动端触摸事件

    其实就是一个TouchEvent对象+上Vue的自定义事件,实现移动端的上滑、下滑、左滑、右滑,长按、点击 组件 ...

  • VUE 封装拖拽组件

    vue 拖拽组件封装,组件代码如下 使用方式:

  • 移动端和pc端的拖拽事件

    pc端拖拽事件 移动端的拖拽事件 移动端的拖拽事件的思路是当手指点下记录手指的坐标和box的位置。当手指移动的时候...

  • 移动端dialog组件

    移动端dialog组件 dialogView是满足移动端下,用户自定义的dialog组件,API可扩展性强,使用便...

  • table组件

    前端项目分类: 移动端(h5页,小标题) PC端 常见的vue技术栈组件库: 移动端:Vant ,Cube-UI ...

网友评论

      本文标题:vue移动端自定义拖拽组件(亲测有效)

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