到达iFrame底部时不要滚动网页

时间:2015-02-25 19:30:55

标签: javascript jquery html css iframe

当我使用滚动条到达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,当你到达底部时,整个页面将向下滚动。

如何禁用它?

2 个答案:

答案 0 :(得分:4)

你可以这样接近

$('.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/

答案 1 :(得分:0)

$('.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();
    }
});

做了这个伎俩