美文网首页
vue拖拽改变排序

vue拖拽改变排序

作者: Wanlly | 来源:发表于2021-02-01 13:42 被阅读0次

vue拖拽改变排序

···
<template>
<div>
<div id="app">
<ul @dragstart="onDragStart" @dragover="onDragOver" @dragend="onDragEnd" ref="parentNode">
<li v-for="(item, index) in data" :key="index" class="item" draggable="true">{{ item }}</li>
</ul>
</div>
</div>
</template>
<script>
export default {
data () {
return {
data: [1, 2, 3, 4, 5, 6],
draging: null, // 被拖拽的对象
target: null // 目标对象
}
},
mounted () {
// 为了防止火狐浏览器拖拽的时候以新标签打开,此代码真实有效
document.body.ondrop = function (event) {
event.preventDefault()
event.stopPropagation()
}
},
methods: {
onDragStart (event) {
this.draging = event.target
},
onDragOver (event) {
this.target = event.target
const targetTop = event.target.getBoundingClientRect().top
const dragingTop = this.draging.getBoundingClientRect().top
if (this.target.nodeName === 'LI' && this.target !== this.draging) {
if (this.target) {
if (this.target.animated) {
return
}
}
if (this._index(this.draging) < this._index(this.target)) {
this.target.parentNode.insertBefore(this.draging, this.target.nextSibling)
} else {
this.target.parentNode.insertBefore(this.draging, this.target)
}
this._anim(targetTop, this.target)
this._anim(dragingTop, this.draging)
}
},
onDragEnd (event) {
const currentNodes = Array.from(this.refs.parentNode.childNodes) const data = currentNodes.map((i, index) => { const item = this.data.find((c) => c === i.innerText) return item }) console.log(data) }, _anim (startPos, dom) { const offset = startPos - dom.getBoundingClientRect().top dom.style.transition = 'none' dom.style.transform = `translateY({offset}px)`
// 触发重绘
// eslint模式下必须添加下面这行注释
// eslint-disable-next-line no-unused-expressions
dom.offsetWidth
dom.style.transition = 'transform .3s'
dom.style.transform = // 触发重绘 // setTimeout(()=>{ // dom.style.transition="transform .3s"; // dom.style.transform=;
// },0)
clearTimeout(dom.animated)
dom.animated = setTimeout(() => {
dom.style.transition = ''
dom.style.transform = ``
dom.animated = false
}, 300)
},
_index (el) {
const domData = Array.from(this.$refs.parentNode.childNodes)
return domData.findIndex((i) => i.innerText === el.innerText)
}
}
}
</script>
<style scoped>
ul {
list-style: none;
padding-bottom: 20px;
}
.item {
cursor: pointer;
/* height: 30px;
line-height: 30px; */
padding: 10px 0;
background-color: rgb(139, 36, 36);
border: 1px solid #d9d9d9;
border-radius: 4px;
color: #fff;
font-size: 30px;
padding: 10px;
}
</style>
···

相关文章

  • vue拖拽改变排序

    vue拖拽改变排序 ··· expo...

  • vue,iview遇到的问题(1)

    1 vue draggable 火狐拖拽搜索问题使用vue-draggable做字段拖拽排序,在谷歌浏览器上是没有...

  • Vue拖拽排序

    Awe-dnd Awe-dnd:一个轻量的 Vue 拖动排序插件,可使您的元素在 Vue 中可拖动。 demo 示...

  • Vue拖拽排序

    先看看官网demohttps://raw.githubusercontent.com/SortableJS/Vue...

  • Vue element 表格拖拽排序

    Vue element 表格拖拽排序 代码实现:1.安装sortablejs 2.引入sortablejs 3....

  • iOS collectionView拖拽排序

    iOS collectionView拖拽排序 iOS collectionView拖拽排序

  • source code:TableviewGroup阴影加圆角

    长按拖拽排序(上传者:yeliang_new)长按拖拽排序,拖拽排序。拖拽排序是新闻类的App可以说是必有的交互设...

  • vue网格拖拽排序

    上图是网格拖拽排序的效果图,如果在你的项目中也有这样的需求,那么不妨来看看如何实现。 技术框架:vue.js 拖拽...

  • 如何玩转sortablejs-vuedraggable实现表单嵌

    最近几天在研究有关vue实现拖拽的功能,不过跟一般的拖拽排序有点不同,这个需求可能出现多行多列嵌套的表单元素,数据...

  • 拖拽操作

    应用: 1.拖拽排序2.拖拽上传3.拖拽裁剪 拖拽流程 确定可拖拽的内容-->开始拖拽-->拖拽过程-->结束拖拽...

网友评论

      本文标题:vue拖拽改变排序

      本文链接:https://www.haomeiwen.com/subject/zerimktx.html