Drag and drop events in embedded SVG?

这一生的挚爱 提交于 2019-12-06 20:37:07

问题


Is there any possibility of receiving drag and drop events from SVG elements within a web page?

I tried the Google Closure library, to no avail.

Specifically, suppose my page contains

<ul id = "list">
  <li class="item" id="item1">foo</li>
  <li class="item">bar</li>
  <li class="item">baz</li>
</ul>

And my script contains (Clojurescript/C2)

(let [items (select-all ".item")
      lst (select "#list")
      target (fx/DragDrop. lst nil)]
  (dorun (map
    (fn [item]
      (let [source (fx/DragDrop. item nil)]
        (. source (addTarget target))
        (. source (init))))
    items))
  (. target (init)))

Then I do get a drag image (ghost), although I do not manage to receive drag events e.g. by doing

(on-raw "#item1" :dragstart (fn [e] (.log js/console (str "dragstart " e))))

Using similar code for SVG elements, I do not even get a ghost...

Any hints?

Thanks


回答1:


Drag events are not supported on SVG Elements: http://www.w3.org/TR/SVG/svgdom.html#RelationshipWithDOM2Events.

You can fake the drag events with mouse events, see http://svg-whiz.com/svg/DragAndDrop.svg (view the source).




回答2:


You can always implement it. Basically, you have to check if the element is touching another one when you are dragging:

this.isTouching = function(element){
        if(this.x <= element.x && element.x <= (this.x + this.width) && 
           this.y <= element.y && element.y <= (this.y + this.height)){
           return true;
        }else{
            return false;
        }
    };

And it works in all browsers. Hope it helps :)



来源:https://stackoverflow.com/questions/12283183/drag-and-drop-events-in-embedded-svg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!