禁用iframe中的滚动

时间:2014-12-04 14:17:57

标签: javascript html events

我有一个带有iframe的html页面,当我将鼠标放在iframe上并使用鼠标滚动按钮时,它将滚动iframe页面,我不希望这种情况发生。但我不希望滚动在这个iframe中完全禁用,因为我有一个fresque,我必须能够放大滚动

我该怎么办?

我尝试scrollTo(0, 0);,但它是在真实网页上而不是在iframe上进行的。

3 个答案:

答案 0 :(得分:1)

尝试使用滚动="否"选项,如

<iframe scrolling="no" src="http://www.google.com" width="400px" height="300"></iframe>

答案 1 :(得分:1)

您可以尝试使用mousewheel事件取消滚动。

示例代码:

window.onload=function(){
    setTimeout(function(){ //just to be sure that the document exists
        document.onmousewheel=function(event){
            event.preventDefault();

            //add here your code to zoom

        };
    },300);
};

请注意,IE8将始终“内部”使用event.preventDefault();,如果您想使用标志启用/禁用滚动,滚动将无效。

您可以在此处阅读更多信息:http://www.quirksmode.org/dom/events/scroll.html


这是我以前的解决方案:

这个问题不够具体,但我想我明白了。

这是一个jQuery来修复我理解的内容:

(function($){
    $(function(){
        $(window).click(function(event){
            if(event.which==3) //middle button
            {
                event.preventDefault();
                //remaining code for the zoom(?)
            }
        });
    });
})(jQuery);

这应该禁止使用滚轮滚动页面(触摸时不起作用)。

您可以在if块内包含缩放(?)的代码。

将此代码包含在内部 iframe!

答案 2 :(得分:0)

假设:&#34;我想禁用页面滚动而不会消除滚动(在iframe&#34;

首先,此CSS将关闭页面上的滚动条...

<style type="text/css">
  html, body {
    overflow: hidden;
  }
</style>

...而且,这将在任何时候尝试禁用滚动...

$(window).scroll(function() {
    scroll(0,0);
});

......虽然,这可能是一个更好的选择...

document.body.scroll = "no";
document.body.style.overflow = 'hidden';
document.height = window.innerHeight;