Always scroll a div element and not page itself

前端 未结 6 1086
梦毁少年i
梦毁少年i 2021-01-11 23:18

I have a page layout with an inner

element which contains the important stuff on the page. The important part about the design is:

6条回答
  •  Happy的楠姐
    2021-01-11 23:35

    I found a solution... Not perfect... http://jsfiddle.net/fGjUD/6/.

    CSS:

    body.noscroll {
        position: fixed;
        overflow-y: scroll;
        width: 100%;
    }
    

    JS (jQuery):

    if ($("body").height() > $(window).height()) {
        var top;
        $('#scrolldiv').mouseenter(function() {
            top = $(window).scrollTop();
            $('body').addClass('noscroll').css({top: -top + 'px'});
        }).mouseleave(function() {
            $('body').removeClass('noscroll');
            $(window).scrollTop(top);
        });
    }
    

    The text wrapping problem can be solved putting the whole content in fixed-width div. There is another bug for IE browser. If the page has center-aligned backgrond, it will move left-right on mouseenter on #scrolldiv

提交回复
热议问题