【vue】【vue-selecto】框选元素拖动到另一个元素

逃离我推掉我的手 2022-10-28 14:21 318阅读 0赞

场景需求:选中列表中的某一项拖动到目标中

  • 使用工具vue-selecto进行框选
  • 使用H5的draggable属性进行拖拽放置

使用工具vue-selecto进行框选

git地址:https://github.com/daybrush/selecto
demo地址: https://daybrush.com/selecto/storybook/?path=/story/selecto–select-in-real-time

以下是代码,来源是vue-selecto插件

说明:HTML中vue-selecto主要使用依据是class名称进行使用,dragContainerbind:selectableTargets属性中引用均为操作框选对象的css名称,在项目中使用时对应原有即可

hitRate属性是框选对象选中多大范围算选中,例:100为全部框选为选中

HTML

  1. <!-- 这里是html代码 -->
  2. <div class="container">
  3. <vue-selecto dragContainer=".elements" v-bind:selectableTargets='[".selecto-area .cube"]' v-bind:hitRate='100' v-bind:selectByClick='true' v-bind:selectFromInside='true' v-bind:ratio='0' @select="onSelect" @selectEnd="selectEnd" ></vue-selecto>
  4. <div class="elements selecto-area" id="selecto1">
  5. <div class="cube" v-for="(cube,index) in cubes" :key="index" :id="index+1" @mousedown.stop>{
  6. {index+1}}</div>
  7. </div>
  8. <div class="empty elements"></div>
  9. </div>

JS

  1. // 这里是js代码
  2. import { VueSelecto } from 'vue-selecto'
  3. export default {
  4. components: {
  5. VueSelecto
  6. },
  7. data () {
  8. return {
  9. cubes: [],
  10. selected: [],
  11. }
  12. },
  13. mounted () {
  14. for (let i = 0; i < 60; ++i) {
  15. this.cubes.push(i)
  16. }
  17. },
  18. methods: {
  19. onSelect (e) {
  20. e.added.forEach(el => {
  21. el.classList.add('selected')
  22. })
  23. e.removed.forEach(el => {
  24. el.classList.remove('selected')
  25. this.selected = []
  26. })
  27. },
  28. // 框选结束存储数据
  29. selectEnd (e) {
  30. e.selected.map(item => {
  31. this.selected.push(item.id)
  32. })
  33. },
  34. }
  35. }

css

  1. /* 这里是css样式 */
  2. .container {
  3. max-width: 800px;
  4. }
  5. .cube {
  6. display: inline-block;
  7. border-radius: 5px;
  8. width: 40px;
  9. height: 40px;
  10. margin: 4px;
  11. background: #eee;
  12. --color: #4af;
  13. }
  14. h1, .description {
  15. text-align: center;
  16. }
  17. .elements {
  18. margin-top: 40px;
  19. border: 2px solid #eee;
  20. }
  21. .selecto-area {
  22. padding: 20px;
  23. }
  24. #selecto1 .cube {
  25. transition: all ease 0.2s;
  26. }
  27. .moveable #selecto1 .cube {
  28. transition: none;
  29. }
  30. .selecto-area .selected {
  31. color: #fff;
  32. background: var(--color);
  33. }
  34. .empty.elements {
  35. border: none;
  36. }

使用H5的draggable属性进行拖拽放置

菜鸟教程地址:https://www.runoob.com/html/html5-draganddrop.html

说明:可以使用多个目的地元素,例如:a,b,c 拖动到1号目的地,e,f,g拖动到二号目的地

有坑!!!框选元素与拖拽元素操作冲突导致框选与拖拽失效

解决办法:给框选元素加上@mousedown.stop,防止操作冲突

HTML

  1. <!-- 拖拽后的目的地元素 -->
  2. <div id="droptarget" @dragover.prevent="allowDrop" @drop.prevent="drop">
  3. {
  4. {selectEndList}}
  5. </div>
  6. <!-- 展示当前状态文本 -->
  7. <p id="text"></p>
  8. <!-- 给元素添加拖拽属性 @dragstart="dragStart" draggable="true" -->
  9. <div class="cube" v-for="(cube,index) in cubes" @dragstart="dragStart" draggable="true" :key="index" :id="index+1" @mousedown.stop>{
  10. {index+1}}</div>

JS

  1. // 这里是js代码
  2. data () {
  3. return {
  4. selectEndList: [] // 展示拖拽到目的地后的数据
  5. }
  6. },
  7. methods: {
  8. // 拖拽开始将数据给目标元素
  9. dragStart () {
  10. console.log('start')
  11. },
  12. // 拖拽元素放置到了目的地元素上面
  13. allowDrop () {
  14. document.getElementById('demo').innerHTML = '元素在放置目标上'
  15. },
  16. // 拖拽元素结束了操作
  17. drop () {
  18. this.selectEndList = this.selected
  19. this.selected = []
  20. document.getElementById('demo').innerHTML = '元素被拖动'
  21. }
  22. }

下面是展示效果:
在这里插入图片描述
总结: 项目实现是多个目的地和多个框选元素,项目开始前花了很长的时间去找插件实现这两种功能,直到后面发现拖拽H5中有属性可以直接使用。这里学到的就是及时去了解已有的新属性,就算当时不会记住,至少在脑子里有印象,后期需要用的时候可以凭借印象去定位一定范围搜索合适的技术,避免后续海底捞针似的寻找;当多种功能重叠时,若找不到所有功能都使用的插件时,应及时分解功能去找对应的技术。

发表评论

表情:
评论列表 (有 0 条评论,318人围观)

还没有评论,来说两句吧...

相关阅读