简体   繁体   English

检测来自不同域的iframe上的悬停

[英]Detect hover on iframe from different domain

I have an iframe loaded into a parent page. 我将iframe加载到父页面中。 The page and the iframe are hosted on different domains. 该页面和iframe托管在不同的域中。 My iframe skeleton looks something like this : http://cl.ly/image/44090J0H2S3Y 我的iframe骨架看起来像这样: http : //cl.ly/image/44090J0H2S3Y

I am trying to implement drag and drop of files into the iframe from outside. 我正在尝试从外部将文件拖放到iframe中。 Due to security reasons, browsers wont forward drag/drop events to the iframe loaded from a different domain. 出于安全原因,浏览器不会将拖放事件转发到从其他域加载的iframe。 Currently, I am working around that by placing a transparent div over the entire iframe and catching the javascript "drop" event onto the transparent div and then use iframe.postMessage to send a message to the iframe. 当前,我正在通过在整个iframe上放置一个透明div并将javascript“ drop”事件捕获到透明div上,然后使用iframe.postMessage将消息发送到iframe来解决此问题。 On the iframe side, I have some javascript listening for the message and takes appropriate action of uploading it to my server. 在iframe上,我有一些JavaScript可以监听消息,并采取适当的措施将其上传到我的服务器。

This all is working fine. 这一切都很好。 What I want to implement is the following : 我要实现以下内容:

  1. User starts dragging an image from the host page 用户开始从主机页面拖动图像
  2. As soon as s/he arrives in the "blue" area, the corresponding blue item should highlight 他/他到达“蓝色”区域后,相应的蓝色项目应突出显示
  3. When s/he lets go, the file should be uploaded 当他/他放手时,文件应上传

I can place a separate transparent div on each of the blue items but the problem is there could be any number of blue items with the scrollbar etc. I am not sure how I can achieve this. 我可以在每个蓝色项目上放置一个单独的透明div,但问题是滚动条等可以包含任意数量的蓝色项目。我不确定如何实现这一点。 Thanks for your help! 谢谢你的帮助!

How are your drag'n drop implemented? 您的拖放操作是如何实现的? HTML5 drag'n drop or DOM based? HTML5拖放还是基于DOM? From the description I suspect you are using DOM based drag'n drop 从描述中我怀疑您正在使用基于DOM的拖放

Because of browser restrictions it is not possible to read/write to the DOM of the iframe when it is hosted on another domain. 由于浏览器的限制,如果将iframe托管在另一个域上,则无法对其进行读/写操作。 It will probably not give you the desired effect, but HTML5 drag'n drop is much better for dragging between iframes and windows. 它可能不会给您带来理想的效果,但是HTML5拖放对于在iframe和窗口之间进行拖动要好得多。 But again you can not add style to the drop target from the source document code. 但是同样,您不能从源文档代码向放置目标添加样式。

Try this: 尝试这个:

  1. Navigate to: http://decafbad.com/2009/07/drag-and-drop/api-demos.html 导航至: http : //decafbad.com/2009/07/drag-and-drop/api-demos.html
  2. Open a new window and navigate to: http://funwithbonus.com/wp-content/uploads/stack-drawers.jpg 打开一个新窗口,然后浏览至: http : //funwithbonus.com/wp-content/uploads/stack-drawers.jpg
  3. Drag the image to the 4th example "Using drag feedback imagesBack to TOC" 将图像拖动到第四个示例“使用拖动反馈图像返回到目录”

If you have access to the domain of the target document you could implement a drop zone to the 'blue items' that handles the highlighting 如果您有权访问目标文档的域,则可以在“突出显示的蓝色项目”上实现放置区域

I would go about creating the transparent divs via a method such as this: 我将通过这样的方法创建透明的div:

var blueItemContainer = document.getElementById('blueItemContainer');
var blueItems = blueItemContainer.childNodes;

blueItemContainer.style.position = "relative";

for(var i = 0; i < blueItems.length; i++)
{
    var transparentDiv = document.createElement('div');
    transparentDiv.style.height = blueItems[i].style.height;
    transparentDiv.style.width = blueItems[i].style.width;
    transparentDiv.style.position = "absolute";
    transparentDiv.style.left = blueItems[i].offsetLeft;
    transparentDiv.style.top = blueItems[i].offsetTop;
    blueItemContainer.appendChild(transparentDiv);
}

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

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