I'm trying to have a very basic drag and drop. It's working fine by just setting draggable = "true"
, but doesn't seem to work when the draggable element contains an iframe.
I have searched everywhere and met many complex problems with iframes, but none being about my specific problem.
As you can see, only the area that is not covered by the iframe is draggable, while I would want to drag the whole container.
div {
width: 300px;
height: 100px;
background: #A1DB6A;
}
iframe {
border: 0;
height: 80px;
width: 100%;
background: #ddd;
}
<div draggable="true">
<iframe src=""></iframe>
</div>
<br/>
<span>Only the green handle is draggable, but I want the whole container to be.</span>
iframe Part Drag start is triggered by a draggable element inside iframe. For better performance, dragging and drag end event listeners are attached in drag start and are detached in drag end. Dragging event listener is required here because the mousemove event of the upper frame breaks inside the iframe.
contents() can get both text nodes and HTML elements. That's why one can get document contents of an iframe by using it. Note that the iframe and page have to be on the same domain.
You shouldn't use iFrame excessively. It can slow down your page and pose a security risk, especially if you use content from a suspicious website. Think about an iFrame as a part of your content, but not part of your site.
Iframes Cause SEO Problems. Google recommends refraining from creating iframes. At there Webmasters Help Forum, Google clearly stated that iframes may cause problems for them: IFrames are sometimes used to display content on web pages.
An iframe
contains a completely separate window from the parent window. When a user interacts with an iframe
, the parent window no longer receives user events. The browser assumes you want to interact with the contents of the iframe
, and ignores any draggable
attributes that may be on a parent of the iframe
.
You could position an invisible element over the iframe
to prevent the user from interacting with the iframe
below. Here is an example using an absolute-positioned pseudo-element.
div {
width: 300px;
height: 100px;
background: #A1DB6A;
position: relative;/*parent must have positioning*/
}
iframe {
border: 0;
height: 80px;
width: 100%;
background: #ddd;
}
/*Cover up the entire element (could be limited to just covering the iframe element)*/
div:after {
content: "";
display: block;
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
}
This workaround brings with it the obvious side-effect of preventing the user from interacting with the contents of the iframe
, which is presumably alright in most cases where you want the element to be draggable
.
If the iframe
needs to be interacted with, you could make a toggle between draggable and not-draggable, and only apply this CSS when draggable. Otherwise you would need to use JavaScript to interact with the iframe
, either by contentWindow
if they satisfy Same-Origin Policy, or by postMessage
, and toggle it on/off based on certain events.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With