H5 页面拖动悬浮按钮

  <div class="main-plus" :style="{ bottom: (clientHeight * 5/100) + '%'}" ref="dragBox" @click="goItem" @touchstart="touchstartHandle('dragBox',$event)" @touchmove="touchmoveHandle('dragBox',$event)" @touchend="touchendHandle('dragBox',$event)">
    <van-icon name="http://chuantu.xyz/t6/742/1672300762x2890317095.png"  @click="delPhnoe" size="30"/>
  </div>
   goItem() {
      this.$emit('goEtone')
    },
    touchstartHandle(refName, e) {
      let element = e.targetTouches[0]
      // 记录点击的坐标
      this.coordinate.client = {
        x: element.clientX,
        y: element.clientY
      }
      // 记录需要移动的元素坐标
      this.coordinate.elePosition.left = this.$refs[refName].offsetLeft
      this.coordinate.elePosition.top = this.$refs[refName].offsetTop
    },
    touchmoveHandle(refName, e) {
      e.returnValue = false;
      let element = e.targetTouches[0]
      // 根据初始 client 位置计算移动距离(元素移动位置=元素初始位置+光标移动后的位置-光标点击时的初始位置)
      let x = this.coordinate.elePosition.left + (element.clientX - this.coordinate.client.x)
      let y = this.coordinate.elePosition.top + (element.clientY - this.coordinate.client.y)
      // 限制可移动距离,不超出可视区域
      x = x <= 0 ? 0 : x >= innerWidth - this.$refs[refName].offsetWidth ? innerWidth - this.$refs[refName].offsetWidth : x
      y = y <= 0 ? 0 : y >= innerHeight - this.$refs[refName].offsetHeight ? innerHeight - this.$refs[refName].offsetHeight : y
      // 移动当前元素
      this.$refs[refName].style.left = x + 'px'
      this.$refs[refName].style.top = y + 'px'
      e.returnValue = true;
    },
    touchendHandle(refName, e) {
      let element = e.changedTouches[0]
      let scree = document.body.clientWidth / 2
      let _clientX = element.clientX

      if (_clientX < scree) {
        this.$refs[refName].style.left = 10 + 'px'
      } else {
        this.$refs[refName].style.left = 8.3 + 'rem'
      }
    }

.main-plus {
  width: 1rem;
  height: 1rem;
  position: fixed;
  z-index: 10;
  bottom: 5rem;
  right: 0.2rem;
  border-radius: 50%;
  /*background-color: #5080FF;*/
  color: #fff;
  line-height: 1rem;
  text-align: center;
  font-size: 0.45rem;
}

在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现一个可拖动悬浮按钮,可以使用以下步骤: 1. 在 HTML 中添加一个悬浮按钮的元素,比如一个 `<div>` 标签。 ```html <div class="floating-button"></div> ``` 2. 使用 CSS 设置该元素的样式,比如设置它的位置、大小、背景色、圆角等。 ```css .floating-button { position: fixed; bottom: 20px; right: 20px; width: 50px; height: 50px; background-color: #007bff; border-radius: 50%; z-index: 9999; } ``` 3. 使用 JavaScript 实现拖动功能。可以监听该元素的 `mousedown`、`mousemove`、`mouseup` 事件,通过计算鼠标的位置和元素的位置差,实现元素的拖动。 ```javascript var floatingButton = document.querySelector('.floating-button'); var isDragging = false; var lastX, lastY; floatingButton.addEventListener('mousedown', function(e) { isDragging = true; lastX = e.clientX; lastY = e.clientY; }); floatingButton.addEventListener('mousemove', function(e) { if (isDragging) { var deltaX = e.clientX - lastX; var deltaY = e.clientY - lastY; var rect = floatingButton.getBoundingClientRect(); floatingButton.style.left = rect.left + deltaX + 'px'; floatingButton.style.top = rect.top + deltaY + 'px'; lastX = e.clientX; lastY = e.clientY; } }); floatingButton.addEventListener('mouseup', function() { isDragging = false; }); ``` 这样就实现了一个可拖动悬浮按钮。需要注意的是,对于移动端,需要考虑触摸事件而非鼠标事件。同时,需要使用 CSS 设置 `touch-action: none;`,防止浏览器默认的滚动行为干扰拖动操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值