vue实现可拖拽移动悬浮球

封装悬浮球组件,文件名s-icons.vue 

<template>
  <div ref="icons" class="icons-container" :style="{ left: left + 'px', top: top + 'px' }">
    <slot></slot>
  </div>
</template>
<script>
export default {
  name: 'FloatIcons',
  props: {
    // 滚动id
    scroller: {
      type: String,
      default: ''
    },
    // 距离上有下左的安全距离
    padding: {
      type: String,
      default: '10 10 10 10'
    },
    // 初始位置距离底部的距离
    bottom: {
      type: Number,
      default: 0
    }
  },
  data () {
    return {
      timer: null,
      currentTop: 0,
      clientWidth: 0,
      clientHeight: 0,
      itemWidth: 0,
      itemHeight: 0,
      left: null,
      top: null
    }
  },
  computed: {
    // 滚动对象,默认空返回window
    scrollContainer () {
      if (this.scroller === '') {
        return window
      } else {
        return document.getElementById(this.scroller)
      }
    },
    // 安全距离
    safeArea () {
      return this.padding.split(' ')
    }
  },
  created () {
    // 屏幕宽度
    this.clientWidth = document.documentElement.clientWidth
    // 屏幕高度
    this.clientHeight = document.documentElement.clientHeight
  },
  mounted () {
    this.$nextTick(() => {
      // 获取滚动元素
      this.scrollContainer.addEventListener('scroll', this.handleScrollStart)
      const div = this.$refs.icons
      // 获取宽度
      this.itemWidth = this.$refs.icons.offsetWidth
      this.itemHeight = this.$refs.icons.offsetHeight
      // 设置位置
      this.left = this.clientWidth - this.itemWidth - this.safeArea[1]
      this.top = this.clientHeight - this.itemWidth - this.bottom
      div.addEventListener('touchstart', () => {
        div.style.transition = 'none'
      })
      div.addEventListener('touchmove', e => {
        // 阻止默认动作
        e.preventDefault()
        if (e.targetTouches.length === 1) {
          const touch = event.targetTouches[0]
          this.left = touch.clientX - this.itemWidth / 2
          this.top = touch.clientY - this.itemHeight / 2
        }
      })
      div.addEventListener('touchend', () => {
        div.style.transition = 'all 0.3s'
        // 手指放开left位置
        if (this.left > this.clientWidth / 2) {
          this.left = this.clientWidth - this.itemWidth - this.safeArea[1]
        } else {
          this.left = this.safeArea[3]
        }
        // 手指放开top位置
        if (this.top < this.safeArea[0]) {
          this.top = this.safeArea[0]
        } else if (this.top > this.clientHeight - this.itemHeight - this.safeArea[2]) {
          // 不低于最低
          // this.top = this.clientHeight - this.itemHeight - this.safeArea[2]
          this.top = this.clientHeight - this.itemHeight
        }
      })
    })
  },
  beforeDestroy () {
    this.scrollContainer.removeEventListener('scroll', this.handleScrollStart)
  },
  methods: {
    // 滚动时候执行
    handleScrollStart () {
      this.timer && clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        this.handleScrollEnd()
      }, 300)
      this.currentTop = document.documentElement.scrollTop || document.body.scrollTop
      if (this.left > this.clientWidth / 2) {
        this.left = this.clientWidth - this.itemWidth / 2
      } else {
        this.left = -this.itemWidth / 2
      }
    },
    handleScrollEnd () {
      const scrollTop = document.documentElement.scrollTop || document.body.scrollTop
      if (scrollTop === this.currentTop) {
        if (this.left > this.clientWidth / 2) {
          this.left = this.clientWidth - this.itemWidth - this.safeArea[1]
        } else {
          this.left = this.safeArea[3]
        }
        clearTimeout(this.timer)
      }
    }
  }
}
</script>
<style lang="less" scoped>
.icons-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  position: fixed;
  background: #ffffff;
  box-shadow: 0px 2px 4px 0px rgba(0, 97, 209, 0.1);
  border: 1px solid rgba(21, 95, 186, 0.1);
  // border-radius: 50%;
  z-index: 1000;
  transition: all 0.3s;
}
</style>

在封装了一层组件引入s-icons.vue 文件,文件名goToHome.vue

<template>
  <float-icons class="icons-warp" :bottom="bottom" :scroller="scroller">
    <div class="float-icon-item" @click="goHomeClick">
      <div class="home-title">返回顶部</div>
    </div>
  </float-icons>
</template>
<script>
import FloatIcons from './s-icons'
export default {
  components: {
    'float-icons': FloatIcons
  },
  props: {
    // 滚动id
    scroller: {
      type: String,
      default: ''
    },
    // 初始位置距离底部的距离
    bottom: {
      type: Number,
      default: 60
    }
  },
  created () {
  },
  methods: {
    goHomeClick () {
      // 跳转其他页面
      // window.history.go(-(window.history.length - 2))
      // 回到顶部
      window.scrollTo({
        top: 0,
        left: 0,
        behavior: 'smooth' // 平滑滚动效果
      })
    }
  }
}
</script>
<style lang="less" scoped>
.icons-warp {
  border-radius: 50%;
  .float-icon-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    position: relative;
    width: 52px;
    height: 52px;
    .home-title {
      font-size: 14px;
      font-weight: 400;
      color: #1763bf;
    }
  }
}
</style>

最后一步,引入到页面中

<template>
  <div class="main">
    <h1>顶部</h1>
    <h1>11111</h1>
    <h1>11111</h1>
    <h1>11111</h1>
    <h1>11111</h1>
    <h1>11111</h1>
    <h1>11111</h1>
    <h1>11111</h1>
    <h1>11111</h1>
    <h1>11111</h1>
    <h1>11111</h1>
    <h1>11111</h1>
    <h1>11111</h1>
    <h1>11111</h1>
    <h1>11111</h1>
    <h1>11111</h1>
    <h1>11111</h1>
    <h1>11111</h1>
    <h1>11111</h1>
    <h1>11111</h1>
    <h1>11111</h1>
    <h1>11111</h1>
    <h1>11111</h1>
    <h1>11111</h1>
    <h1>11111</h1>
    <h1>11111</h1>
    <h1>11111</h1>
    <h1>11111</h1>
    <h1>11111</h1>
    <h1>底部</h1>
    <GoToHome :bottom="bottom" />
  </div>
</template>

<script>
import GoToHome from './goToHome.vue'
export default {
  data () {
    return {
      bottom: 105
    }
  },
  methods: {
  },
  components: {
    GoToHome
  }
}
</script>

<style lang="less" scoped>
</style>

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
实现拖拽移动按钮的方法有很多,以下是一个基于Vue.js实现示例: 1. 在Vue组件中,定义按钮的样式和初始位置: ```html <template> <div class="button" :style="{left: x + 'px', top: y + 'px'}" @mousedown="startDrag"> 按钮 </div> </template> <script> export default { data() { return { x: 0, y: 0, isDragging: false, startX: 0, startY: 0 }; }, methods: { startDrag(e) { this.isDragging = true; this.startX = e.clientX; this.startY = e.clientY; } } }; </script> <style> .button { position: absolute; width: 100px; height: 50px; background-color: #4CAF50; color: white; text-align: center; line-height: 50px; cursor: move; } </style> ``` 2. 添加鼠标移动事件,实现拖拽: ```html <template> <div class="button" :style="{left: x + 'px', top: y + 'px'}" @mousedown="startDrag" @mousemove="drag" @mouseup="stopDrag"> 按钮 </div> </template> <script> export default { data() { return { x: 0, y: 0, isDragging: false, startX: 0, startY: 0 }; }, methods: { startDrag(e) { this.isDragging = true; this.startX = e.clientX; this.startY = e.clientY; }, drag(e) { if (this.isDragging) { let dx = e.clientX - this.startX; let dy = e.clientY - this.startY; this.x += dx; this.y += dy; this.startX = e.clientX; this.startY = e.clientY; } }, stopDrag() { this.isDragging = false; } } }; </script> ``` 3. 在父组件中,将按钮组件引入并使用: ```html <template> <div> <draggable-button></draggable-button> </div> </template> <script> import DraggableButton from './DraggableButton.vue'; export default { components: { DraggableButton } }; </script> ``` 这样就可以在Vue应用中实现拖拽移动的按钮了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

学不会•

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

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

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

打赏作者

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

抵扣说明:

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

余额充值