vue拖拽悬浮球

创建组件dragBallComponent.vue
<template>
  <div class="drag-ball" ref="dragBall" @touchstart.stop.prevent="touchstart" @touchmove.stop.prevent="touchmove" @touchend.stop.prevent="touchend">
    <div class="drag-content">
      <slot name="value">{{ value }}</slot>
    </div>
  </div>
</template>
<script>
export default {
  name: 'drag-ball',
  props: {
    value: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      canDrag: false,
      // 偏移
      inset: {
        left: 0,
        top: 0
      },
      // 移动
      move: {},
      // 位置
      position: {
        left: 0,
        top: 0
      },
      // 初始位置
      positionOld: {},
      startTime: null,
      endTime: null
    };
  },
  methods: {
    toNew() {
      alert('去新版');
    },
    touchstart(e) {
      if (!this.canDrag) {
        this.startTime = e.timeStamp;
        this.positionOld =  this.getPosition(this.dragBall);
        this.position = {...this.positionOld};
        this.inset = {
          left: e.targetTouches[0].clientX - this.positionOld.left,
          top: e.targetTouches[0].clientY - this.positionOld.top
        };
        this.canDrag = true;
      }
    },
    touchmove(e) {
      if (this.canDrag) {
        let left = e.targetTouches[0].clientX - this.inset.left;
        let top = e.targetTouches[0].clientY - this.inset.top;
        if (left < 0) {
          left = 0;
        } else if (left > (window.innerWidth - this.dragBall.offsetWidth)) {
          left = window.innerWidth - this.dragBall.offsetWidth;
        }
        if (top < 0) {
          top = 0;
        } else if (top > (window.innerHeight - this.dragBall.offsetHeight)) {
          top = window.innerHeight - this.dragBall.offsetHeight;
        }
        this.dragBall.style.left = left + 'px';
        this.dragBall.style.top = top + 'px';
        this.move = {
          x: left - this.positionOld.left,
          y: top - this.positionOld.top
        };
        this.position = {left, top};
      }
    },
    touchend(e) {
      if (this.canDrag) {
        this.endTime = e.timeStamp;
        if ( this.endTime - this.startTime > 400 || Math.abs(this.move.x) > 2 || Math.abs(this.move.y) > 2 ) {
          // 非单击事件
          if ( (this.position.left + this.dragBall.offsetWidth / 2) > window.innerWidth / 2) {
            this.dragBall.style.left = window.innerWidth - this.dragBall.offsetWidth + 'px';
          } else {
            this.dragBall.style.left = 0 + 'px';
          }
        } else {
          this.$emit('click');
        }
        this.inset = {};
        this.move = {};
        this.position = {};
        this.canDrag = false;
      }
    },
    // 获取dom的绝对位置
    getPosition(source) {
      let left = source.offsetLeft; //获取元素相对于其父元素的left值var left
      let top = source.offsetTop;
      let current = source.offsetParent; // 取得元素的offsetParent // 一直循环直到根元素
      while (current != null) {
        left += current.offsetLeft;
        top += current.offsetTop;
        current = current.offsetParent;
      }
      return {
        left: left,
        top: top
      };
    }
  },
  computed: {
    dragBall() {
      return this.$refs.dragBall;
    }
  }
};
</script>
<style scoped>
.drag-ball {
  position: absolute;
  z-index: 10003;
  right: 0;
  top: 70%;
  display: inline-table;
}
.drag-ball .drag-content {
  overflow-wrap: break-word;
  font-size: 14px;
  color: #fff;
  letter-spacing: 2px;
}
</style>
在需要使用的页面引入组件
<drag-ball :value="message" @click="qqjs">
	中间是slot
</drag-ball>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现靠边悬浮的代码,可以通过以下步骤来实现: 1. 在Vue组件中,定义一个data属性来存储悬浮的位置和状态信息,如下所示: ```js data() { return { ball: { left: 0, // 悬浮左侧距离屏幕左侧的距离 top: 0, // 悬浮顶部距离屏幕顶部的距离 isFixed: false, // 悬浮是否固定在边缘 isLeft: true // 悬浮是否在左侧边缘 } } } ``` 2. 在组件的`mounted`钩子函数中,监听窗口的滚动事件,并根据窗口滚动的距离和悬浮的位置信息,来判断悬浮是否需要固定在边缘,以及固定在哪个边缘,如下所示: ```js mounted() { window.addEventListener('scroll', () => { const ball = this.ball const scrollTop = document.documentElement.scrollTop || document.body.scrollTop if (scrollTop > ball.top && !ball.isFixed) { ball.isFixed = true ball.top = 0 if (ball.left < 100) { ball.isLeft = true ball.left = 0 } else { ball.isLeft = false ball.left = document.documentElement.clientWidth - 100 } } else if (scrollTop <= ball.top && ball.isFixed) { ball.isFixed = false ball.left = ball.isLeft ? 0 : document.documentElement.clientWidth - 100 } }) } ``` 3. 在组件的模板中,根据悬浮的位置和状态信息,来动态设置悬浮的样式和位置,如下所示: ```html <template> <div> <div class="ball" :class="{ fixed: ball.isFixed, left: ball.isLeft }" :style="{ left: ball.left + 'px', top: ball.top + 'px' }"></div> </div> </template> <style scoped> .ball { position: absolute; width: 100px; height: 100px; border-radius: 50%; background-color: #f00; cursor: pointer; transition: all 0.3s ease; } .ball.fixed { position: fixed; top: 0; } .ball.left { left: 0; } .ball:not(.left) { right: 0; } </style> ``` 通过以上步骤,就可以实现一个Vue组件中的靠边悬浮了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值