使iframe点击,而不是iframe的正文

For*_*vin 13 html javascript css iframe click-through

如何制作iframe点击,但是iframe的正文仍然可以点击?

我试过了:

iframe.style.width = '100%'
iframe.style.height = '100%'
iframe.style.display = 'block'
iframe.style.position = 'fixed'
iframe.style.backgroundColor = 'transparent'
iframe.style.pointerEvents = 'none'
iframe.style.border = '0'
iframe.frameborder = '0'
iframe.scrolling = 'no'
iframe.allowTransparency = 'true'
Run Code Online (Sandbox Code Playgroud)

在我的框架内我使用以下css:

html, body {
    /* background:none transparent; */
    pointer-events:auto;
}
Run Code Online (Sandbox Code Playgroud)

这会导致身体可见(这就是我想要的),但它像iframe的其余部分一样是点击式的.我希望iframe的主体可以点击,但实际的iframe元素的其余部分应该是点击式的.

iframe总是比它里面的身体大.

不幸的是我无法从主站点访问iframe内容(因此无法访问scrollHeight等),我只能更改其实际的源代码.

fin*_*ron 7

免责声明:OP差不多两年前创建了这个问题,我的回答是在Ian Wise提出问题并详细说明之后(见评论).


您在此处描述的内容涉及文档和子文档之间的逻辑:"如果单击事件在子文档中没有执行任何操作,请将该单击事件应用于父文档",因此无法使用HTML/CSS进行处理.

iframe是不同的文件.它们与容器之间存在子父关系,但iframe中发生的事件将由iframe处理.

一个需要一些代码但可以工作的想法:

  • 在所有堆叠的iframe上方放置一个透明div,并捕获click事件pos.
  • 父逻辑 - >
    • 迭代现有iframe元素的数组.
    • 发送Click click,直到其中一个iframe返回肯定响应.

function clickOnCover(e, i) {
	if(e && e.preventDefaule) e.preventDefault();
	if(i && i >= iframes.length) {
		console.log("No action.");
		return;
	}
	var iframe = iframes[i || 0];
	if(iframe.contentWindow && iframe.contentWindow.postMessage) {
		iframe.contentWindow.postMessage({ x: e.clientX, y: e.clientY, i: i });
	}
}
function iframeResponse(e) {
	var response = e.data, iframeIndex = response.i;
	if(response.success)
		console.log("Action done on iframe index -> " + iframeIndex);
	else 
		clickOnCover({ clientX: response.x, clientY: response.y }, iframeIndex+1);
}
Run Code Online (Sandbox Code Playgroud)

  • iFrames逻辑 - >
    • 有一个函数接受clientX, clientY并检查该位置的可能活动(可能很棘手!).
    • 如果一个动作发生,将会积极响应,反之亦然.

window.addEventListener("message", function(e) {
	// Logic for checking e.x & e.y
	
	e.success = actionExists; // Some indicator if an action occurred.
	
	if(window.parent && window.parent.postMessage) {
		window.parent.postMessage(e);
	}
});
Run Code Online (Sandbox Code Playgroud)

此解决方案始终在父文档中管理事件,并且只需要迭代任意数量的堆叠iframe.


找到相关的SO问题以进一步支持我的声明:检测Clickrame中的Click


And*_*kyi 6

CSS无法实现这一点.
最简单的方法是正确调整iframe的大小.假设您可以访问iframe内容,则可以使用以下解决方案:

您可以添加一个小JS以允许父页面知道iframe高度

mainpage.js

var iframe = getIframe();
setIntervalMaybe(() => {
    // ask for size update
    iframe.contentWindow.postMessage({action: "getSize"}, document.location.origin);
}, 100)
window.addEventListener("message", function receiveMessage(event) {
  switch(event.data.action) {
    case "returnSize":
      // updateIFrameSize(event.data.dimensions);
      console.log(event.data.dimensions);
      break;
  }
}, false);
Run Code Online (Sandbox Code Playgroud)

iframe.js

window.addEventListener("message", function receiveMessage(event) {
  switch(event.data.action) {
    case "getSize":
      event.source.postMessage({action: "returnSize", dimensions: {
        width: document.body.offsetWidth,
        height: document.body.offsetHeight
      }}, event.origin);
      break;
  }
}, false);
Run Code Online (Sandbox Code Playgroud)

  • @AndreiGheorghiu根据您的有用建议添加了一些代码. (2认同)