简体   繁体   English

禁用iframe中的滚动

[英]Disable scroll in a iframe

I have my html page with a iframe, and when I put the mouse on the iframe and I use the mouse scroll button it will scroll the iframe page and I don't want that to happen. 我有带有iframe的html页面,当我将鼠标放在iframe上并使用鼠标滚动按钮时,它将滚动iframe页面,但我不希望这种情况发生。 But I don't want the scroll to be totaly disable in this iframe because I have a fresque and I must be abble to zoom in with the scroll 但是我不希望在此iframe中完全禁用滚动条,因为我有怪诞的表情,并且一定要迷糊才能放大滚动条

How could I do ? 我该怎么办?

I have try to do scrollTo(0, 0); 我尝试做scrollTo(0, 0); but it does it on the real page and not on the iframe. 但它是在实际页面上而不是在iframe上执行的。

尝试使用scrolling =“ no”选项,如下所示

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

You can try to use the mousewheel event to cancel the scroll. 您可以尝试使用mousewheel事件来取消滚动。

Example code: 示例代码:

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);
};

Notice that IE8 will always "internally" use event.preventDefault(); 请注意,IE8将始终“内部”使用event.preventDefault(); and the scroll won't work if you want to use a flag to enable/disable the scroll. 如果您要使用标记来启用/禁用滚动,则滚动将不起作用。

You can read more information here: http://www.quirksmode.org/dom/events/scroll.html 您可以在此处阅读更多信息: http : //www.quirksmode.org/dom/events/scroll.html


This is my old solution: 这是我的旧解决方案:

The question isn't specific enough, but I think I understood it. 这个问题不够具体,但是我想我理解了。

Here is a piece of jQuery to fix what I understood: 这是一段jQuery,用于修复我的理解:

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

This should disable using the scroll wheel to scroll the page (doesn't work on touch). 这应该禁用使用滚轮滚动页面(触摸时不起作用)。

You can include the code for the zooming(?) inside the if block. 您可以在if块中包含zooming(?)的代码。

Include this code inside the iframe! 包括iframe 的代码!

Assuming: "I want to disable the scroll of the page without disabeling the scroll (in the iframe ) " 假设:“我想禁用页面的滚动而不放弃滚动(在iframe

First, this CSS will turn off scrollbars on the page ... 首先,此CSS将关闭页面上的滚动条...

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

... and, this will disable scrolling anytime it is tried ... ...,这将在任何尝试的时间都禁止滚动...

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

... although, this might be a better option ... ...虽然这可能是一个更好的选择...

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

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

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