我想在主页上禁用滚动,但当鼠标悬停在 iframe 上时不在 iframe 内滚动。 你可以更清楚地看到我的意思here .
如果 iframe 正在接收鼠标滚轮事件,那么当鼠标位于 iframe 内时,我不希望整个页面向下滚动。请注意,它在 chrome 中有效,但在 ff 中无效。
编辑:键盘组件甚至可以按预期工作!
注意:
滚动启用/禁用基于代码 here
最佳答案
更简单的解决方案:
给你的父 body 一个id(在例子中是“bodyID”
),然后在你的启用和禁用滚动功能中添加:
function disable_scroll()
{
document.getElementById("bodyID").style.overflow="hidden";
}
function enable_scroll()
{
document.getElementById("bodyID").style.overflow="auto";
}
基本上,因为您不希望父页面滚动,而不是针对单个事件,只需在光标位于 iframe
内时完全禁用父页面的滚动。简单、跨浏览器且更直接。
事实上,您可以删除 90% 的代码,只使用
function disable_scroll()
{
document.getElementById("bodyID").style.overflow="hidden";
}
function enable_scroll()
{
document.getElementById("bodyID").style.overflow="auto";
}
document.getElementById("miframe").onmouseenter = disable_scroll;
document.getElementById("miframe").onmouseleave = enable_scroll;
你可以使用 document.body
,但是当你使用 JSBin 或类似的东西时,这会让人感到困惑,并且有六个(好吧,三个,但仍然)文档
和 正文
。
检查修改后的 JSBIN here .
关于javascript - 在 Firefox 中禁用主窗口滚动 onmouseenter iframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19982722/