如何禁用在iframe元素内部拖动?

问题描述 投票:0回答:1

我正在尝试禁止在iframe元素内拖动。

例如我的iframe包含一张地图,我希望允许单击地图上显示的项目,但同时,我想防止拖动时地图移动(因此禁用拖动)。

“可拖动”属性无济于事,因为它阻止了iframe本身的拖动。

[我还尝试使用iframe的contentDocument和contentWindow属性,但没有运气。

例如以下代码仅适用于具有空src =“”的框架。一旦定义了src,就不会监听“ click”事件。

HTML:

<iframe id="iFrame" width="800" height="600" src="...an embedded map source..."></iframe>

JS:

var win = iFrame.contentWindow;
var doc = iFrame.contentWindow.document;
win.addEventListener("mousedown", function(){
    console.log("iframe win clicked");
doc.addEventListener("mousedown", function(){
    console.log("iframe content clicked");
javascript css iframe drag
1个回答
0
投票

您是否尝试取消本机事件行为?

var win = iFrame.contentWindow;
var doc = iFrame.contentWindow.document;
doc.addEventListener("drag", function(event){ event.preventDefault(); });
© www.soinside.com 2019 - 2024. All rights reserved.