首页 > 其他分享 >鼠标任意拖动元素排列顺序(vue)

鼠标任意拖动元素排列顺序(vue)

时间:2023-08-28 09:44:26浏览次数:27  
标签:vue 鼠标 newItems 拖动 list 列表 height label newIndex

参考地址:https://codesandbox.io/s/condescending-butterfly-enjqpr?file=/src/App.vue

 

<template>
  <div>
    <transition-group name="drag" class="list" tag="ul">
      <li
        @dragstart="dragStart(item, index)"
        @dragover.prevent="dragOver(index)"
        @dragend="dragEnd()"
        draggable
        v-for="(item, index) in todolist"
        :key="item.label"
        :class="['list-item', { 'is-dragover': index === newIndex }]"
      >
        {{ item.label }}
      </li>
    </transition-group>
  </div>
</template>

<script>
export default {
  data() {
    return {
      todolist: [
        { label: "列表1" },
        { label: "列表2" },
        { label: "列表3" },
        { label: "列表4" },
        { label: "列表5" },
        { label: "列表6" },
      ],
      oldIndex: "",
      oldData: "",
      newIndex: "",
    };
  },
  methods: {
    dragStart(val, i) {
      this.oldIndex = i;
      this.oldData = val;
    },
    dragOver(i) {
      this.newIndex = i;
    },
    dragEnd() {
      let newItems = [...this.todolist];
      // 删除老的节点
      newItems.splice(this.oldIndex, 1);
      // 在列表中目标位置增加新的节点
      newItems.splice(this.newIndex, 0, this.oldData);
      this.todolist = [...newItems];
      this.newIndex = "";
    },
  },
};
</script>

<style lang="scss" scoped>
.list {
  list-style: none;
  .drag-move {
    transition: transform 0.3s;
  }
  .list-item {
    position: relative;
    cursor: move;
    width: 300px;
    background: #2997f4;
    border-radius: 4px;
    color: #FFF;
    margin: 10px 0;
    height: 50px;
    line-height: 50px;
    text-align: center;
  }
}

.list-item.is-dragover::before {
  content: "";
  position: absolute;
  bottom: -8px;
  left: 0;
  width: 100%;
  height: 4px;
  background-color: #0c6bc9;
}
.list-item.is-dragover::after {
  content: "";
  position: absolute;
  bottom: -12px;
  left: -6px;
  border: 3px solid #0c6bc9;
  border-radius: 50%;
  width: 6px;
  height: 6px;
  background-color: #fff;
}
</style>

 

标签:vue,鼠标,newItems,拖动,list,列表,height,label,newIndex
From: https://www.cnblogs.com/qianduan-lucky/p/17661459.html

相关文章

  • vue-颜色选择器(vColorPicker)
      //安装插件//官网:http://vue-color-picker.rxshc.com/npminstallvcolorpicker-S//main.js中全局引入importvcolorpickerfrom'vcolorpicker'Vue.use(vcolorpicker)//在页面中使用<colorPickerv-model="color"v-on:change="he......
  • vue3中使用provide/inject
    父组件<template><hello-world/><button@click="changeMessage">按钮</button></template><scriptsetuplang="ts">importHelloWorldfrom"./components/HelloWorld.vue";import{provide,ref......
  • Vue的使用(2)
    一个简单的Vue项目的创建创建一个UserList.vue组件在App.vue中使用该组件使用组件的第一步,导入组件使用组件的第二部,申明这个组件使用组件的第三步:引用组件结果:事件和插值语法<template><div><!--template只允许有一个唯一的孩子--><h1>这是一个一级标题</h1><di......
  • vue2
    介绍Vue.js是什么Vue(读音/vjuː/,类似于view)是一套用于构建用户界面的渐进式框架。Vue被设计为可以自底向上逐层应用。Vue.js使用了基于HTML的模板语法,允许开发者声明式地将DOM绑定至底层Vue实例的数据。所有Vue.js的模板都是合法的HTML,所以能被遵循规范的浏......
  • vue微信H5项目使用腾讯地图获取当前位置经纬度
    1.在index.html引入js文件<scriptsrc="https://3gimg.qq.com/lightmap/components/geolocation/geolocation.min.js"></script>2.在需要页面中你自己的key要去腾讯地图工具去申请https://lbs.qq.com/webApi/component/componentGuide/componentPickercreated(){this.getM......
  • vue项目打包之后, 生成一个可修改IP地址的文件
     在做项目的时候遇到了一个问题,就是把项目部署到不同的服务器上,但不能每次修改IP的时候就打包一次,这就增加了前端的工作量,经过百度,发现有一些方法是可以的,亲测可用。具体操作是,1,在static文件夹下面建立一个config.js文件, 1234567(function (){ ......
  • vue3探索——组件通信之v-model父子组件数据同步
    背景再很多场景中,我们可能想在子组件中修改父组件的数据,但事实上,vue不推荐我们这么做,因为数据的修改不容易溯源。Vue2写法在vue2中,我们使用.sync修饰符+自定义事件'update:xxx',来使父子组件数据同步。//父组件<template><div><h2>我是父组件,我有{{money}}¥......
  • vue中添加音频和视频
    视频播放功能1.安装vue-video-playernpminstallvue-video-player--save或yarnaddvue-video-player--save2.在main.js中全局引用importVueVideoPlayerfrom'vue-video-player'import'video.js/dist/video-js.css'import'vue-video-player/src/cu......
  • 小白整理了VUEX
    在小白开发的项目中前端使用的是Vue,虽然在日常工作中可以使用Vue进行开发工作。但是没有系统的学习过Vue,对Vue的一些特性和方法使用时常常需要查询资料解决问题,查询资料常常会占用大量时间,尤其对Vuex的使用。虽然可以通过很多Vue框架中带有的Vuex框架直接使用,但是用的越多,小白就会......
  • 在vue中使用svg
    背景svg作为矢量图,放大不失真,但是如果在vue文件中直接引入svg的话会导致vue文件过长,不友好。想要在组件中使用的时候使用下面的方式来引入svg//svg-name的值就是svg图片的名称<svg-iconsvg-name="xueren"/>步骤在components下创建SvgIcon.vue文件,并引入assets/icons/sv......