美文网首页
vue 实现pc + 移动/h5随意拖拽

vue 实现pc + 移动/h5随意拖拽

作者: 周郭郭先生 | 来源:发表于2019-08-02 14:31 被阅读0次

// 使用指令

<div v-drag="greet"></div>

greet回调移动的距离

greet (val) { console.log(val }

// 创建指令

import Vue from 'vue'

import _ from 'lodash'

Vue.directive('drag', {

  // drag自定义指令

  bind: function (a, binding) {

    var isStart = false // 是否已经开始拖拽

    let distance = {} // 拖动的距离

    let l, t, x, y // 当前被拖动的距离

    let w // 当前屏幕宽度

    let h // 当前屏幕的高度

    // 移动端

    a.addEventListener(

      'touchstart',

      function (e) {

        isStart = true // 如果没有开始拖拽, 则可以拖拽

        if (!e.changedTouches[0]) return

        var { clientX, clientY } = e.changedTouches[0]

        x = clientX - e.changedTouches[0].target.x

        y = clientY - e.changedTouches[0].target.y

        w = document.body.clientWidth // 当前屏幕宽度

        h = document.body.clientHeight // 当前屏幕的高度

      },

      { passive: false }

    )

    // _.debounce 防抖

    a.addEventListener(

      'touchmove',

      _.debounce(function (e) {

        e.preventDefault()

        l = e.changedTouches[0].clientX - x

        t = e.changedTouches[0].clientY - y

        if (l < 0 && t < 0) {

          a.style.left = 0 + 'px'

          a.style.top = 0 + 'px'

        } else if (l < 0 && t + a.clientHeight < h) {

          a.style.left = 0 + 'px'

          a.style.top = t + 'px'

        } else if (l < 0 && t + a.clientHeight >= h) {

          a.style.left = 0 + 'px'

          a.style.top = h - a.clientHeight + 'px'

        } else if (l + a.clientWidth > w && t < 0) {

          a.style.left = w - a.clientWidth + 'px'

          a.style.top = 0 + 'px'

        } else if (l + a.clientWidth < w && t + a.clientHeight >= h) {

          a.style.left = l + 'px'

          a.style.top = h - a.clientHeight + 'px'

        } else if (l + a.clientWidth < w && t < 0) {

          a.style.left = l + 'px'

          a.style.top = 0 + 'px'

        } else if (l + a.clientWidth > w && t + a.clientHeight < h) {

          a.style.left = w - a.clientWidth + 'px'

          a.style.top = t + 'px'

        } else if (l + a.clientWidth > w && t + a.clientHeight >= h) {

          a.style.left = w - a.clientWidth + 'px'

          a.style.top = h - a.clientHeight + 'px'

        } else {

          a.style.left = l + 'px'

          a.style.top = t + 'px'

        }

      }, 5),

      { passive: false }

    )

    a.addEventListener(

      'touchend',

      function (e) {

        // console.log(w)

        isStart = false

        document.ontouchmove = null

        document.ontouchmove = null

        distance = {

          type: 'move',

          clientX: x - e.changedTouches[0].clientX, // 拖动的 x 距离

          clientY: y - e.changedTouches[0].clientY // 拖动的 y 距离

        }

        binding.value(distance) // 返回拖动的距离

      },

      { passive: false }

    )

    // 移动端-- end

    // pc端

    a.onmousedown = function (e) {

      e.preventDefault()

      w = document.body.clientWidth // 当前屏幕宽度

      h = document.body.clientHeight // 当前屏幕的高度

      if (isStart) return // 如果已经开始拖拽, 返回

      isStart = true // 如果没有开始拖拽, 则可以拖拽

      var { clientX, clientY } = e

      var x = clientX - a.offsetLeft

      var y = clientY - a.offsetTop

      document.onmousemove = _.debounce(function (e) {

        e.preventDefault()

        l = e.clientX - x

        t = e.clientY - y

        if (l < 0 && t < 0) {

          a.style.left = 0 + 'px'

          a.style.top = 0 + 'px'

        } else if (l < 0 && t + a.clientHeight < h) {

          a.style.left = 0 + 'px'

          a.style.top = t + 'px'

        } else if (l < 0 && t + a.clientHeight >= h) {

          a.style.left = 0 + 'px'

          a.style.top = h - a.clientHeight + 'px'

        } else if (l + a.clientWidth > w && t < 0) {

          a.style.left = w - a.clientWidth + 'px'

          a.style.top = 0 + 'px'

        } else if (l + a.clientWidth < w && t + a.clientHeight >= h) {

          a.style.left = l + 'px'

          a.style.top = h - a.clientHeight + 'px'

        } else if (l + a.clientWidth < w && t < 0) {

          a.style.left = l + 'px'

          a.style.top = 0 + 'px'

        } else if (l + a.clientWidth > w && t + a.clientHeight < h) {

          a.style.left = w - a.clientWidth + 'px'

          a.style.top = t + 'px'

        } else if (l + a.clientWidth > w && t + a.clientHeight >= h) {

          a.style.left = w - a.clientWidth + 'px'

          a.style.top = h - a.clientHeight + 'px'

        } else {

          a.style.left = l + 'px'

          a.style.top = t + 'px'

        }

      }, 5)

      document.onmouseup = function (e) {

        document.onmousedown = null

        document.onmousemove = null

        isStart = false

        distance = {

          type: 'move',

          clientX: clientX - e.clientX, // 拖动的 x 距离

          clientY: clientY - e.clientY // 拖动的 y 距离

        }

        binding.value(distance) // 返回拖动的距离

      }

    }

    // pc端  -  end

  }

})

相关文章

  • vue 实现pc + 移动/h5随意拖拽

    // 使用指令 greet回调移动的距离greet (val) { console.log(val } // 创建...

  • 用Selenium模拟页面滚动

    TouchActions:模拟pc和h5的点击,滑动,拖拽,多点触控等多种手势操作实现方法:new_action....

  • table组件

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

  • 封装移动端 vue 拖拽指令

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

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

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

  • h5、vue实现拖拽

    H5中拖拽属性:draggable: auto | true | false 拖动事件: dragstart 在元...

  • vue 移动端 实现div拖拽移动

    相关知识点touchstart 当在屏幕上按下手指时触发touchmove 当在屏幕上移动手指时触发touchen...

  • vue 移动端 实现div拖拽移动

    相关知识点touchstart 当在屏幕上按下手指时触发touchmove 当在屏幕上移动手指时触发touchen...

  • Vue Canvas 实现电子签名 手写板

    最近再做移动端电子签名,Vue+Canvas实现,移动端、PC端均可,也可以从github下载 。我在做这个功能的...

  • HTML5拖拽上传

    传统拖拽效果小demohtml5实现拖拽小demo调查问卷小demo拖拽拼图小demo拖拽上传小demo h5拖拽...

网友评论

      本文标题:vue 实现pc + 移动/h5随意拖拽

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