简体   繁体   English

在snap.svg中拖动和鼠标悬停

[英]Drag and mouseover in snap.svg

I'm relatively new to javascript, and am learning about drag and drop using snap.svg. 我对javascript比较陌生,并且正在使用snap.svg学习拖放。 My problem is in the drop. 我的问题在于下落。 I can't tell if the dragged element is over the drop target. 我无法判断所拖动的元素是否在放置目标之上。 In this code, I want to drag the circle over the square, and thought I could use mouseover. 在这段代码中,我想将圆圈拖动到正方形上,并认为可以使用鼠标悬停。 My (distilled) example may also be a simpler version of this post . 我(蒸馏水)例如也可以是一个简单的版本, 这个帖子

var paper = Snap(300, 300);
var square = paper.rect(100, 100, 40, 40).attr({fill:"blue"});
var circle = paper.circle(50, 50, 20).attr({fill:"red"});

circle.drag();
square.mouseover(
        function() {
            console.log("Over the square");
        }
);

As written, the mouseover will fire when you move the pointer over the blue square, but not when you drag the red circle over the blue square. 按照书面说明,将鼠标指针移到蓝色正方形上时会触发鼠标悬停,但是当您将红色圆圈拖动到蓝色正方形上时不会悬停。 If you reverse the creation of the square and circle, the mouseover fires either way, but of course the circle is behind the square. 如果您反转正方形和圆形的创建,则鼠标悬停都会触发,但是圆形当然会在正方形后面。

Evidently the event gets caught in the view hierarchy (or something) and doesn't propagate. 显然,该事件已陷入视图层次结构(或某些层次)中,并且不会传播。 There must be an easy way around this. 解决这个问题的方法一定很简单。 Any help? 有什么帮助吗?

(And if the best answer is, "use jQuery," fine, but I'd love to learn how to make this work directly, since snap.svg makes dragging so easy.) (如果最好的答案是“使用jQuery”,那很好,但是我很想学习如何直接使用jQuery,因为snap.svg使拖动变得如此简单。)

Addition : The direction I'm hoping for: the snap.svg documentation for Element.drag() says, in part, "When Element is dragged over another element, drag.over.<id> fires as well." 另外 :我希望的方向: Element.drag()的snap.svg文档部分说:“将Element拖动到另一个元素上时, drag.over.<id>触发drag.over.<id> 。” A fine, event-based direction, which would let me (for example) highlight the drop target without a lot of fuss. 一个很好的,基于事件的指示,这将使我(例如)突出显示放置目标而没有太多麻烦。

But I haven't figured out how to listen for that event! 但是我还没有弄清楚如何听那个事件! Any help or advice? 有什么帮助或建议吗?

Only quick way without collision or element detection from points that I can think of, is to place an almost invisible clone in front of the object, later in the DOM that you can't really see, eg ... 从我能想到的点开始,没有碰撞或元素检测的唯一快速方法是,在对象之前放置一个几乎不可见的克隆,然后在您无法真正看到的DOM中放置它,例如...

paper.append( square.clone().attr({ opacity: 0.000001 }) )

jsfiddle jsfiddle

Depends how complex your svgs are going to be as to whether this would work I guess, you also have a slight issue if you drop the element over it, your redrag start won't get picked up, so you would need to code around that as well. 我想这取决于您的svg的复杂程度,如果您将它放下,也会有一个小问题,如果您将svg放到它上面,您的redrag开始就不会被拾取,因此您需要对此进行编码也一样 I think some testing is probably going to be the most bug free solution (there are a few solutions on SO for getElementFromPoint or hit detection type solutions). 我认为某些测试可能将是最没有错误的解决方案(SO上有一些getElementFromPoint或命中检测类型解决方案的解决方案)。

jsfiddle workaround for point above 上面的点的jsfiddle解决方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM