发布于2021-05-30 12:28 阅读(2068) 评论(0) 点赞(12) 收藏(0)
Vue.Draggable是一款基于Sortable.js实现的vue拖拽插件。支持移动设备、拖拽和选择文本、智能滚动,可以在不同列表间拖拽、不依赖jQuery为基础、vue 2过渡动画兼容、支持撤销操作,总之是一款非常优秀的vue拖拽组件。本篇将介绍如何搭建环境及简单的例子,使用起来特别简单对被拖拽元素也没有CSS样式的特殊要求。
NPM或yarn安装方式
- yarn add vuedraggable
- npm i -S vuedraggable
UMD浏览器直接引用JS方式
- <script src="http://www.itxst.com/package/vue/vue.min.js"></script>
- <script src="http://www.itxst.com/package/sortable/Sortable.min.js"></script>
- <script src="http://www.itxst.com/package/vuedraggable/vuedraggable.umd.min.js"></script>
在对应的页面引入draggable组件
import draggable from 'vuedraggable'
- <template>
- <div>
- <div>{{drag?'拖拽中':'拖拽停止'}}</div>
- <!--使用draggable组件-->
- <draggable v-model="myArray" chosenClass="chosen" forceFallback="true" group="people" animation="1000" @start="onStart" @end="onEnd">
- <transition-group>
- <div class="item" v-for="element in myArray" :key="element.id">{{element.name}}</div>
- </transition-group>
- </draggable>
- </div>
- </template>
- <style scoped>
- /*被拖拽对象的样式*/
- .item {
- padding: 6px;
- background-color: #fdfdfd;
- border: solid 1px #eee;
- margin-bottom: 10px;
- cursor: move;
- }
- /*选中样式*/
- .chosen {
- border: solid 1px #3089dc !important;
- }
- </style>
- <script>
- //导入draggable组件
- import draggable from 'vuedraggable'
- export default {
- //注册draggable组件
- components: {
- draggable,
- },
- data() {
- return {
- drag:false,
- //定义要被拖拽对象的数组
- myArray:[
- {people:'cn',id:10,name:'www.itxst.com'},
- {people:'cn',id:20,name:'www.baidu.com'},
- {people:'cn',id:30,name:'www.taobao.com'},
- {people:'us',id:40,name:'www.yahoo.com'}
- ]
- };
- },
- methods: {
- //开始拖拽事件
- onStart(){
- this.drag=true;
- },
- //拖拽结束事件
- onEnd() {
- this.drag=false;
- },
- },
- };
- </script>
- // 大概意思就是标签带 singlePerson class的可以拖动
- <draggable
- ghostClass="ghost"
- draggable=".singlePerson"
- :list="list"
- group="people"></draggable>
- <template>
- <draggable
- @end="endFn"
- style="width: 100%; min-height: 80vh;background: #f00"
- ghostClass="ghost"
- @end="endFn"
- @start="startFn"
- draggable=".singlePerson"
- :list="organizeDataV2"
- group="people">
- <div :class="{'singlePerson': modularStart}">
- </div>
- </draggable>
- </template>
-
- <script>
- export default {
- data () {
- return {
- modularStart: false
- }
- },
- methods: {
- endFn () {
- this.modularStart = false
- },
- startFn () {
- this.modularStart = true
- }
- }
- }
- </script>
- <template>
- <draggable
- ghostClass="ghost"
- :list="list"
- group="people"></draggable>
-
- </template>
-
- <style scoped lang="less" rel="stylesheet/less">
- .ghost {
- width: 100%;
- height: 70px;
- text-align: center;
- background-color: #00f;
- background-repeat: no-repeat;
- background-size: cover;
- color: transparent;
- }
- .ghost::after {
- content: '可将模块放置到此区域';
- color: #3089dc;
- margin-top: 15px;
- position: relative;
- top: -20px;
- height: 40px;
- line-height: 40px;
- -webkit-box-sizing: border-box;
- box-sizing: border-box;
- padding: 0 20px;
- background: #e4f0fd;
- display: inline-block;
- }
- </style>
- <template>
- <draggable
- @end="endFn"
- ghostClass="ghost"
- :list="list"
- group="people"></draggable>
-
- </template>
-
- methods: {
- endFn (e) {
- const oldIndex = e.oldIndex
- const newIndex = e.newIndex
- }
- }
原文链接:https://blog.csdn.net/weixin_41854372/article/details/117291573
作者:小兔崽子
链接:http://www.qianduanheidong.com/blog/article/116017/12d0ced358ea6350a908/
来源:前端黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 前端黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-3
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!