vue2 实现组件拖拽

直接看代码
<template>
  <!-- 视频弹窗 -->
  <div class="hover-div">
    <video
      class="hover-div-video"
      v-drag:[office]
      autoplay="autoplay"
      controls="controls"
      muted="muted"
      controlsList="nodownload"
    >
      <source
        src="http://127.0.0.1/test/video/test.mp4"
        type="video/mp4"
      />
    </video>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 视频弹窗拖拽初始位置
      office: {
        right: 100,
        top: 50,
      },
    };
  },
  // 自定义指令: drag,用于拖拽组件
  directives: {
    drag: {
      inserted: function (el, binding) {
        el.style.position = "absolute";
        el.style.left = binding.arg.left + "px";
        el.style.top = binding.arg.top + "px";
        el.onmousedown = function (e) {
          var x = e.clientX - el.offsetLeft;
          var y = e.clientY - el.offsetTop;
          document.onmousemove = function (eve) {
            el.style.left = eve.clientX - x + "px";
            el.style.top = eve.clientY - y + "px";
          };
          document.onmouseup = function () {
            document.onmousemove = null;
            document.onmouseup = null;
          };
        };
      },
    },
  },
};
</script>

<style lang="scss" scoped>
.hover-div {
  background: #ffffff;
  width: 20%;
  height: 26%;
  position: fixed;
  bottom: 5rem;
  right: 2rem;
  padding: 5px;
  background-color: rgba(0, 0, 0, 0);
  border-bottom-left-radius: 5px;
  border-bottom-right-radius: 5px;
  border-top-left-radius: 5px;
  border-top-right-radius: 5px;
  .hover-div-video {
    width: 100%;
    height: 100%;
    object-fit: fill;
  }
}
</style>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3 的组件排序可以通过使用 `Sortable.js` 库来实现。以下是一个简单的示例: 1. 安装 `Sortable.js` 库: ``` npm install sortablejs --save ``` 2. 在需要排序的组件中导入 `Sortable.js`: ```javascript import Sortable from 'sortablejs'; ``` 3. 在组件的 `mounted` 钩子中初始化排序: ```javascript mounted() { const el = this.$refs.sortable; this.sortable = Sortable.create(el, { onEnd: this.onSortEnd }); }, ``` 4. 创建 `onSortEnd` 方法来处理排序结束后的逻辑: ```javascript methods: { onSortEnd(event) { const itemEl = event.item; const newIndex = event.newIndex; // 处理排序结束后的逻辑 } } ``` 5. 在组件中添加排序的 HTML: ```html <template> <div ref="sortable"> <div v-for="(item, index) in items" :key="item.id"> {{ item.text }} </div> </div> </template> ``` 在以上代码中,`items` 是一个数组,包含需要排序的项目。将 `items` 数组绑定到组件的属性中即可。 完整的代码示例: ```javascript <template> <div ref="sortable"> <div v-for="(item, index) in items" :key="item.id"> {{ item.text }} </div> </div> </template> <script> import Sortable from 'sortablejs'; export default { data() { return { items: [ { id: 1, text: 'Item 1' }, { id: 2, text: 'Item 2' }, { id: 3, text: 'Item 3' }, { id: 4, text: 'Item 4' } ], sortable: null }; }, mounted() { const el = this.$refs.sortable; this.sortable = Sortable.create(el, { onEnd: this.onSortEnd }); }, methods: { onSortEnd(event) { const itemEl = event.item; const newIndex = event.newIndex; // 处理排序结束后的逻辑 } } }; </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值