Don'当到达iFrame底部时,不要滚动网页

Don't scroll webpage when reached bottom of iFrame

本文关键字:网页 滚动 底部 iFrame Don      更新时间:2024-05-26

当我到达带有滚动条的iFrame/div的底部并继续向下滚动时,整个页面将向下滚动。

我做了一个快速的JSFiddle来演示这个问题:http://jsfiddle.net/fbgxf771/1/

<div class="whole-page" style="height:2000px;"> 
    <iframe style="height: 100px;" src="http://example.com"></iframe>
</div>

继续向下滚动div或iframe,当您到达底部时,整个页面将向下滚动。

我如何禁用它?

您可以通过这种方式进行

$('.inner-content-with-y-scroll').mouseenter(function(){
   $("body").css("overflow","hidden");
});
$('.inner-content-with-y-scroll').mouseleave(function(){
   $("body").css("overflow","scroll");
});

这是一把小提琴:http://jsfiddle.net/m4feddh1/

$('.inner-content-with-y-scroll').on('DOMMouseScroll mousewheel', function(ev) {
    var $this = $(this),
        scrollTop = this.scrollTop,
        scrollHeight = this.scrollHeight,
        height = $this.height(),
        delta = (ev.type == 'DOMMouseScroll' ?
            ev.originalEvent.detail * -40 :
            ev.originalEvent.wheelDelta),
        up = delta > 0;
    var prevent = function() {
        ev.stopPropagation();
        ev.preventDefault();
        ev.returnValue = false;
        return false;
    }
    if (!up && -delta > scrollHeight - height - scrollTop) {
        // Scrolling down, but this will take us past the bottom.
        $this.scrollTop(scrollHeight);
        return prevent();
    } else if (up && delta > scrollTop) {
        // Scrolling up, but this will take us past the top.
        $this.scrollTop(0);
        return prevent();
    }
});

完成了