javascript - Vue.Draggable (sortable.js) 中的 ondragover 等价物

标签 javascript vuejs2 sortablejs

有没有办法检测是否有东西被拖过元素?或者触发悬停事件?找到了一些有关通过 onMove 向拖过的元素添加类的信息,但它似乎对我不起作用。

最佳答案

我用解决方案制作了一个 JSBin:https://jsbin.com/xuwocis/edit?html,js,output

var sorting = false;

new Sortable(el, {
    onStart: function() {
        sorting = true;
    },
    onEnd: function() {
      sorting = false;
      // remove styling
      targetElement.style.backgroundColor = '';
    },
//     forceFallback:true
});

// For native drag&drop
targetElement.addEventListener('dragover', function(evt) {
    evt.preventDefault();
});

targetElement.addEventListener('dragenter', function(evt) {
    if (sorting && !targetElement.contains(evt.relatedTarget)) {
        // Here is where you add the styling of targetElement
        targetElement.style.backgroundColor = 'red';
    }
});

targetElement.addEventListener('dragleave', function(evt) {
    if (sorting && !targetElement.contains(evt.relatedTarget)) {
        // Here is where you remove the styling of targetElement
        targetElement.style.backgroundColor = '';
    }
});


// For fallback
targetElement.addEventListener('mouseenter', function(evt) {
  if (sorting) {
    // Here is where you change the styling of targetElement
    targetElement.style.backgroundColor = 'red';
  }
});

targetElement.addEventListener('mouseleave', function(evt) {
  if (sorting) {
    // Here is where you remove the styling of targetElement
    targetElement.style.backgroundColor = '';
  }
});

el.addEventListener('touchmove', function(evt) {
  if (!sorting) { return; }
  var x = evt.touches[0].clientX;
  var y = evt.touches[0].clientY;
  var elementAtTouchPoint = document.elementFromPoint(x, y);
  if (elementAtTouchPoint === targetElement ||
      // In case of a ghost element, the element at touch point
      // is the ghost element and thus we need to check if the parent 
      // of the ghost element is the targetElement.
      elementAtTouchPoint.parentNode === targetElement) {
    targetElement.style.backgroundColor = 'red';
  } else {
    // Here is where you remove the styling of targetElement
    targetElement.style.backgroundColor = '';
  }
});

基本上,如果使用 SortableJS 进行排序,您会为回退执行 mouseenter 和 mouseleave 事件,并为本地拖放执行 dragenter 和 dragleave 事件(忽略气泡)。如果您没有 forceFallback: true,您将需要两者。

关于javascript - Vue.Draggable (sortable.js) 中的 ondragover 等价物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54917268/

相关文章:

javascript - Vue.可拖动 : How to "drag an item onto another item" instead of adding/removing

javascript - ReactJS:如何为sortableJS动态设置禁用选项?

javascript - Bootstrap 3 : Dynamically re-size a panel and keep panel footer on bottom

javascript - 仅当屏幕上 <1000 px 时调用 JS 函数

javascript - 在条件变量中渲染 css 类

php - 在同一台服务器(同一端口)上运行 Vue.js 和 Laravel

javascript - 使用 JavaScript 增加局部变量

javascript - 删除 Vuetify 中的默认 v-input-file 图标

javascript - Cordova cordova-plugin-qrscanner : Opaque camera view

javascript - 使用 SortableJS 访问拖动的元素和替换的元素的 data-id