【问题标题】:How to keep dragging item at former position until release in vue draggable如何保持拖动项目在以前的位置,直到在 vue 可拖动中释放
【发布时间】:2019-10-23 14:32:24
【问题描述】:

我正在使用 vuedraggable 在 vue 中实现嵌套层组件。 我尽量让它靠近 Adob​​e 的图层面板(例如在 Illustrator 中)。

期望的行为是: 拖动项目时,它会保持在原来的位置,只有一条黑线表示释放拖动后项目将插入的位置。

黑线可以通过设置vue draggable的ghost来实现。但是如何防止项目在拖动时从其原始位置移除?

Adobe Illustrator layers example

【问题讨论】:

  • 有一个pull: 'clone' 选项......如何使用它然后在删除克隆后删除原始项目
  • 据我了解 pull 选项,如果我将它拖放到另一个可拖动元素,这只会克隆元素。但是对于我的用例,即使将它移动到同一个可拖动中,它也应该被临时克隆

标签: javascript vue.js vuedraggable


【解决方案1】:

我现在使用与Death Waltz's answer 类似的方法,但不直接操作 DOM。

相反,我复制了列表中的项目...

start(event) {
    // Make a clone of the choosen item and add it to the
    // layers list.
    const index = event.oldIndex
    const item = this.layers[index]
    this.layers.splice(index + 1, 0, {
    ...item,
    // Vue requires unique keys.
    id: item.id + '_clone',
    // Set a isClone flag to be able to delete the clone
    // afterwards.
    isClone: true
  })
},

...然后删除它

end() {
    // Delete the clone from the layers.
    this.layers = this.layers.filter(layer => !layer.isClone)
}

这里是完整的例子:https://jsfiddle.net/arnoson/587L0nx9/45/

我仍然不确定这是否是最优雅的解决方案,并希望有一个内置的方法来做到这一点。

【讨论】:

  • 你知道如何使用嵌套的可拖动对象吗?
【解决方案2】:

所以我最终使用了sl-vue-tree ,它基本上完成了模拟 Adob​​e Illustrators 图层面板所需的一切。

【讨论】:

    【解决方案3】:

    本质上,在单击时创建项目的副本,然后将所选项目设置为不可见。在鼠标上移时,隐藏副本并使该项目再次可见。

    一个例子:

    ball.onmousedown = function(event) { // (1) start the process
    
      // (2) prepare to moving: make absolute and on top by z-index
    
      var ball2 = ball; //set the balls current position so it doesn't appear to move
      ball.style.position = 'absolute';
      ball.style.visibility = "hidden"; //make the moving item invisible
    
      document.body.append(ball);
      // ...and put that absolutely positioned ball under the pointer
    
      moveAt(event.pageX, event.pageY);
    
      // centers the ball at (pageX, pageY) coordinates
      function moveAt(pageX, pageY) {
        ball.style.left = pageX - ball.offsetWidth / 2 + 'px';
        ball.style.top = pageY - ball.offsetHeight / 2 + 'px';
    
      }
    
      function onMouseMove(event) {
        moveAt(event.pageX, event.pageY);
      }
    
      // (3) move the ball on mousemove
      document.addEventListener('mousemove', onMouseMove);
    
      // (4) drop the ball, remove unneeded handlers
      ball.onmouseup = function() {
        ball.style.visibility = "visible"; //makes the moved ball visible again
        ball2.style.visibility = "hidden"; //makes the copy invisible
        document.removeEventListener('mousemove', onMouseMove);
        ball.onmouseup = null;
      };
    
    };
    

    【讨论】:

    • 感谢您的回答,但我使用的是 Vue,所以我尽量避免手动操作 DOM
    • 好吧,如果迫在眉睫而您找不到其他方法,这应该可行。希望您能找到更好的解决方案!
    猜你喜欢
    • 2016-05-04
    • 2014-01-16
    • 2014-10-03
    • 1970-01-01
    • 1970-01-01
    • 2019-12-17
    • 1970-01-01
    • 1970-01-01
    • 2022-10-18
    相关资源
    最近更新 更多