我正在構建一個使用 SVG 元素作為畫布的編輯器。它提供了自定義的拖放行為來拖動 SVG 中的元素。然而,當我試圖拖動一個<image>標簽時,本地瀏覽器的拖動行為就會啟動,即你拖動的是影像的 "幽靈"。我怎樣才能阻止它呢?
我嘗試在 CSS 中設定 user-select: none,這確實可以阻止選擇文本,但不能阻止拖動影像。我試著設定了HTML的draggable="false"屬性,這在SVG中沒有效果。我甚至嘗試取消ondragstart事件,但該事件似乎根本就沒有發生。
<svg>
<image draggable="false" style=user-select。none;" ondragstart="return false" href="https://wehles. files.wordpress.com/2020/05/opamp.png?w=147">< /image>
</svg>/span>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
JSfiddle。https://jsfiddle.net/1dnfpray/
我如何防止圖片被拖動,以便我可以實作我自己的拖動行為?
uj5u.com熱心網友回復:
如果你取消了mousedown事件,拖動就被阻止了:
<svg>
<image draggable="false" style=user-select。none;" onmousedown="return false" href="https://wehles. files.wordpress.com/2020/05/opamp.png?w=147">< /image>
</svg>/span>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
如果您想實作您自己的拖動行為,您將需要處理該事件。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/320108.html
標籤:
下一篇:如何解決SVG下拉陰影的剪切問題
