Javascript:我如何使用event.target定位一个外部div所谓的边距?

时间:2017-02-15 22:37:32

标签: javascript

我有一个元素,我拖动一个元素列表。 我已经获得了节点列表。我的所有节点都有边距。

当我拖动元素边距或该元素(该节点)时,我想触发警报。

如果我在具有边距的元素之间右键单击,它会拾取父元素。所以我理解的方法是我可以找到外部的div?

这里有一个检查我要检查是否我拖过一个列表并且我拖过节点并且它可以工作但是如果我拖过元素边缘,我似乎会触发。

我是用纯JavaScript编写的,而不是Jquery或外部库的粉丝。

 if (listItemNode.parentNode == listNode && listItemNode != placeholderNode) {
            // If the mouse pointer is in the upper half of the list item element,
            // we position the placeholder before the list item, otherwise after it.
            var rect = listItemNode.getBoundingClientRect();
            if (listSettings.horizontal) {
                console.log( rect.left);
              var isFirstHalf = event.clientX < rect.left + rect.width / 2;

            } else {
              var isFirstHalf = event.clientY < rect.top + rect.height / 2;
            }
            listNode.insertBefore(placeholderNode,
                isFirstHalf ? listItemNode : listItemNode.nextSibling);


}

1 个答案:

答案 0 :(得分:1)

元素的边距实际上不是元素的一部分,它是元素之间的差距。因此,绑定到元素的事件不会被它的边距触发。用填充或边框替换边距以获得所需的结果 可能有用的一件事是用边缘用其他元素包装你的元素,并将包装器定义为你的droppables。这样他们就会落后于边缘并可能触发事件