h5实现悬浮图标支持随意拖拽并点击跳转


<script>

export default {

  data() {

    return {

      gapWidth: 10,

      itemWidth: 68, // 图标的宽度

      itemHeight: 68, // 图标的高度

      clientWidth: '',

      clientHeight: '',

      left: '',

      top: ''

    }

  },

  created() {

    this.clientWidth = document.documentElement.clientWidth;

    this.clientHeight = document.documentElement.clientHeight;

    this.left = this.clientWidth - this.itemWidth - this.gapWidth;

    this.top = this.clientHeight * 0.6;

  },



  methods: {

    dragStart() {



      this.$refs.div.style.transition = 'none';

    },

    dragEnd() {

      this.$refs.div.style.transition = 'all 0.3s';

      // if (this.left > this.clientWidth / 2) {

      //   this.left = this.clientWidth - this.itemWidth - this.gapWidth;

      // }else {

      //   this.left = this.gapWidth;

      // }

      if (this.left < this.gapWidth) {

        this.left = this.gapWidth

      } else if (this.left > this.clientWidth - 68) {

        this.left = this.clientWidth - this.itemWidth - this.gapWidth;

      }

      if (this.top < 0) {

        this.top = this.gapWidth;

      } else if (this.top > this.clientHeight - 128) {

        this.top = this.clientHeight - 128

      }

      console.log('left', this.left, this.clientWidth - 68);

    },

    dragProgress(e) {

      e.preventDefault();

      if (e.targetTouches.length === 1) {

        let touch = e.targetTouches[0];

        this.left = touch.clientX - this.itemWidth / 2;

        this.top = touch.clientY - this.itemHeight / 2;

      }

    },

    handleBackToHome() {

      this.$router.push('/')

    },

  }

}

</script>



<style lang="less" scoped>

.back_to_home {

  width: 0.68rem;

  height: 0.68rem;

  position: fixed;

  right: 0.1rem;

  bottom: 3.8rem;

  // top: 5.6rem;

  text-align: center;

  border-radius: 50%;

  background-image: url("../../../assets/images//back_to_homePage.png");

  background-repeat: no-repeat;

  background-size: contain;

  background-position: center;

}



.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;

}

</style>
<template>

  <div

    ref="div"

    :style="{

      left: left + 'px',

      top: top + 'px',

    }"

    class="ys-float-btn back_to_home"

    @touchstart="

      (e) => {

        dragStart(e);

      }

    "

    @touchend="

      (e) => {

        dragEnd(e);

      }

    "

    @touchmove="

      (e) => {

        dragProgress(e);

      }

    "

    @click.stop="handleBackToHome"

  ></div>

</template>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过以下步骤实现 H5拖动悬浮按钮的效果: 1. 在 HTML 文件中添加一个按钮元素,并为其设置 id 和 class: ```html <button id="drag-btn" class="drag-btn">拖动我</button> ``` 2. 在 CSS 文件中为按钮元素设置样式,使其成为一个圆形的悬浮按钮: ```css .drag-btn { position: fixed; bottom: 20px; right: 20px; width: 50px; height: 50px; border-radius: 50%; background-color: #007bff; color: #fff; font-size: 20px; font-weight: bold; text-align: center; line-height: 50px; cursor: move; } ``` 3. 在 JavaScript 文件中为按钮元素添加拖动事件: ```js var dragBtn = document.getElementById('drag-btn'); // 记录鼠标按下时的坐标和按钮的初始位置 var startX, startY, initialPos; // 鼠标按下事件 dragBtn.addEventListener('mousedown', function(e) { startX = e.clientX; startY = e.clientY; initialPos = getPosition(dragBtn); // 添加鼠标移动和松开事件 document.addEventListener('mousemove', dragBtnMouseMove); document.addEventListener('mouseup', dragBtnMouseUp); }); // 鼠标移动事件 function dragBtnMouseMove(e) { var dx = e.clientX - startX; var dy = e.clientY - startY; dragBtn.style.left = initialPos.x + dx + 'px'; dragBtn.style.top = initialPos.y + dy + 'px'; } // 鼠标松开事件 function dragBtnMouseUp(e) { document.removeEventListener('mousemove', dragBtnMouseMove); document.removeEventListener('mouseup', dragBtnMouseUp); } // 获取元素的坐标 function getPosition(element) { var rect = element.getBoundingClientRect(); return { x: rect.left, y: rect.top }; } ``` 这样就可以实现一个可拖动悬浮按钮了。通过记录鼠标按下时的坐标和按钮的初始位置,然后在鼠标移动事件中计算偏移量并改变按钮的位置,最后在鼠标松开事件中移除事件监听器,就能实现拖动功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值