设置滑动div的高度

时间:2012-08-10 16:38:37

标签: jquery

如何将高度设置为垂直滑动div?我使用过jQuery,这里是代码:

当我说设置高度时,我的意思是div只滑动说1000px它会停止滑动?我的问题是,在较小的屏幕尺寸中,滑动div总是希望显示,所以在某些页面上它会向下推动页脚,因此用户永远无法看到页脚!?

http://dev.assessmentday.co.uk/psychometric-test.htm

感谢。

210895

1 个答案:

答案 0 :(得分:0)

一个可能的解决方案是添加自动滚动条:

CSS:

#sidebar {
    overflow-y: auto;
}

JS:

$(document).ready(function() {
    var topPadding = 15;
    var sidebar = $("#sidebar");
    sidebar.css('max-height', $(window).height() - 2*topPadding); // initialize max-height
    $(window).resize(function() {
        sidebar.css('max-height', $(window).height() - 2*topPadding);
    });
    $(window).scroll(function() {
        sidebar.stop().animate({
            marginTop: $(window).scrollTop() + topPadding
        });
    });
});

jsFiddle

您网页的Javascript:

$(document).ready(function() {
    $(window).scroll(function() {
        var topPadding = 15;
        var crfoffset = $("div.contentRightFull").offset().top;
        var winoffset = $(window).scrollTop();
        var stioffset = $("#stickymojo").height() - $('#sidebar').height();
        var mtop = Math.max(0, winoffset-crfoffset);
        if (mtop>stioffset) { mtop = stioffset; }
        $("#sidebar").stop().animate({
            marginTop: mtop + topPadding
        });
    });
  });
相关问题