用vue封装一个页面可以拖动按钮

用vue封装一个页面可以拖动按钮

这段代码定义了一个DraggableButton组件,该组件可用于在页面上创建可拖动按钮。该组件接受两个道具:x和y,它们指定按钮在页面上的初始位置,并在按钮被拖动时发出一个拖动事件。

组件使用data函数来管理按钮的拖动状态和位置。handleMouseDown和handleTouchStart方法处理拖动操作的开始,handleMouseMove和handleTouchMove方法处理拖动操作期间按钮的移动。handleMouseUp和handleTouchEnd方法处理拖动操作的结束。

<template>
  <div
    ref="button"
    :style="{
      position: 'absolute',
      top: `${y}px`,
      left: `${x}px`,
      cursor: 'grab',
      cursor: '-webkit-grab',
      zIndex: 9999,
    }"
    @mousedown="handleMouseDown"
    @touchstart="handleTouchStart"
  >
    Drag me!
  </div>
</template>

<script>
export default {
  props: {
    x: {
      type: Number,
      default: 0,
    },
    y: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      isDragging: false,
      offsetX: 0,
      offsetY: 0,
    };
  },
  methods: {
    handleMouseDown(event) {
      this.isDragging = true;
      this.offsetX = event.clientX - this.x;
      this.offsetY = event.clientY - this.y;
      document.addEventListener('mousemove', this.handleMouseMove);
      document.addEventListener('mouseup', this.handleMouseUp);
    },
    handleMouseMove(event) {
      if (this.isDragging) {
        const newX = event.clientX - this.offsetX;
        const newY = event.clientY - this.offsetY;
        this.$emit('drag', newX, newY);
      }
    },
    handleMouseUp() {
      this.isDragging = false;
      document.removeEventListener('mousemove', this.handleMouseMove);
      document.removeEventListener('mouseup', this.handleMouseUp);
    },
    handleTouchStart(event) {
      this.isDragging = true;
      this.offsetX = event.touches[0].clientX - this.x;
      this.offsetY = event.touches[0].clientY - this.y;
      document.addEventListener('touchmove', this.handleTouchMove);
      document.addEventListener('touchend', this.handleTouchEnd);
    },
    handleTouchMove(event) {
      if (this.isDragging) {
        const newX = event.touches[0].clientX - this.offsetX;
        const newY = event.touches[0].clientY - this.offsetY;
        this.$emit('drag', newX, newY);
      }
    },
    handleTouchEnd() {
      this.isDragging = false;
      document.removeEventListener('touchmove', this.handleTouchMove);
      document.removeEventListener('touchend', this.handleTouchEnd);
    },
  },
  mounted() {
    this.$refs.button.addEventListener('mousedown', this.handleMouseDown);
    this.$refs.button.addEventListener('touchstart', this.handleTouchStart);
  },
  beforeDestroy() {
    this.$refs.button.removeEventListener('mousedown', this.handleMouseDown);
    this.$refs.button.removeEventListener('touchstart', this.handleTouchStart);
  },
};
</script>

要使用这个组件,你可以将它导入到你的页面中,并像这样渲染它:

<template>
  <div>
    <DraggableButton :x="buttonPosition.x" :y="buttonPosition.y" @drag="handleDrag" />
  </div>
</template
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值