vue浮动按钮

比眉伴天荒 2021-07-26 15:56 659阅读 0赞
  1. <template>
  2. <div class="main-plus" ref="dragBox" @click="goItem" @touchstart="touchstartHandle('dragBox',$event)" @touchmove="touchmoveHandle('dragBox',$event)" @touchend="touchendHandle('dragBox',$event)">
  3. <van-icon name="wap-home-o" />
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. data() {
  9. return {
  10. coordinate: {
  11. client: {},
  12. elePosition: {}
  13. }
  14. }
  15. },
  16. methods: {
  17. goItem() {
  18. this.$emit('goEtone')
  19. },
  20. touchstartHandle(refName, e) {
  21. let element = e.targetTouches[0]
  22. // 记录点击的坐标
  23. this.coordinate.client = {
  24. x: element.clientX,
  25. y: element.clientY
  26. }
  27. // 记录需要移动的元素坐标
  28. this.coordinate.elePosition.left = this.$refs[refName].offsetLeft
  29. this.coordinate.elePosition.top = this.$refs[refName].offsetTop
  30. },
  31. touchmoveHandle(refName, e) {
  32. e.returnValue = false;
  33. let element = e.targetTouches[0]
  34. // 根据初始 client 位置计算移动距离(元素移动位置=元素初始位置+光标移动后的位置-光标点击时的初始位置)
  35. let x = this.coordinate.elePosition.left + (element.clientX - this.coordinate.client.x)
  36. let y = this.coordinate.elePosition.top + (element.clientY - this.coordinate.client.y)
  37. // 限制可移动距离,不超出可视区域
  38. x = x <= 0 ? 0 : x >= innerWidth - this.$refs[refName].offsetWidth ? innerWidth - this.$refs[refName].offsetWidth : x
  39. y = y <= 0 ? 0 : y >= innerHeight - this.$refs[refName].offsetHeight ? innerHeight - this.$refs[refName].offsetHeight : y
  40. // 移动当前元素
  41. this.$refs[refName].style.left = x + 'px'
  42. this.$refs[refName].style.top = y + 'px'
  43. e.returnValue = true;
  44. },
  45. touchendHandle(refName, e) {
  46. let element = e.changedTouches[0]
  47. let scree = document.body.clientWidth / 2
  48. let _clientX = element.clientX
  49. if (_clientX < scree) {
  50. this.$refs[refName].style.left = 10 + 'px'
  51. } else {
  52. this.$refs[refName].style.left = 6.3 + 'rem'
  53. }
  54. }
  55. }
  56. }
  57. </script>
  58. <style scoped>
  59. .main-plus {
  60. width: 1rem;
  61. height: 1rem;
  62. position: fixed;
  63. z-index: 10;
  64. bottom: 5rem;
  65. right: 0.2rem;
  66. border-radius: 50%;
  67. background-color: #5080FF;
  68. color: #fff;
  69. line-height: 1rem;
  70. text-align: center;
  71. font-size: 0.45rem;
  72. }
  73. </style>

发表评论

表情:
评论列表 (有 0 条评论,659人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Vue按钮权限

    按钮权限:通过判断当前用户是否拥有该权限控制当前组件的显示与删除 第一步、请求后端数据得到当前用户的所有的按钮权限,类型为数组 ![8f73edf4df40470886