高级jQuery粘贴侧边栏

时间:2013-03-25 21:10:20

标签: javascript jquery html sidebar sticky

我正在处理(粘性)滚动侧边栏。问题是大多数粘性侧边栏没有考虑边栏可能比视口更高(例如,如果很多项目被添加到篮子(侧边栏))。这就是我想要解决的问题。这些是要求:

  • 如果侧边栏的高度比视口高,则应该 滚动内容,div的底部应该坚持 向下滚动时视口的底部。

  • 如果侧边栏的高度比视口高,则为div 只有当你在底部时,才能显示下面 页。

  • 当用户向上滚动时,侧边栏会滚动回到顶部 回到视口的顶部。

  • 如果侧边栏的高度小于视口,则应该是 滚动时从顶部粘住。

总而言之,相当一些功能而不是那么简单(我认为)。我见过的最接近我想要实现的是这个例子:http://demo.techbrij.com/730/fix-sidebar-scrolling-jquery.php但是编写代码的方式并不适合我的。

到目前为止,这就是我所做的:DEMO

我使用的jQuery代码:

jQuery(document).ready(function($) {

var $sidebar   = $('.sidebar'),
    $content   = $('.content');

if ($sidebar.length > 0 && $content.length > 0) {
    var $window    = $(window),
        offset     = $sidebar.offset(),
        timer;

    $window.scroll(function() {
        clearTimeout(timer);
        timer = setTimeout(function() {
            if ($content.height() > $sidebar.height()) {
                var new_margin = $window.scrollTop() - offset.top;
                if ($window.scrollTop() > offset.top && ($sidebar.height()+new_margin) <= $content.height()) {
                    // Following the scroll...
                    $sidebar.stop().animate({ marginTop: new_margin });
                } else if (($sidebar.height()+new_margin) > $content.height()) {
                    // Reached the bottom...
                    $sidebar.stop().animate({ marginTop: $content.height()-$sidebar.height() });
                } else if ($window.scrollTop() <= offset.top) {
                    // Initial position...
                    $sidebar.stop().animate({ marginTop: 0 });
                }
            }
        }, 100); 
    });
}

});

3 个答案:

答案 0 :(得分:3)

你正在使用边距来移动粘性侧边栏 - 我发现这是处理当前问题的一种棘手的方式(并且可能是更重的方式)。

一般情况下,我喜欢简单地在边栏上添加一个类,使其成为“position:fixed”,这样浏览器就可以保持锁定状态。

对于你当前的问题,你只需要根据它们滚动的距离以编程方式滚动该位置固定div(也是100%高度)。看看我的例子,看看这是否是你所追求的效果:http://jsfiddle.net/ZHP52/1/

这是jquery:

jQuery(document).ready(function($) {

var $sidebar   = $('.sidebar'),
    $content   = $('.content');

//Since our CSS is going to monkey with the height as you scroll, I need to know the initial height.
var sidebarHeight = $sidebar.height();

if ($sidebar.length > 0 && $content.length > 0) {
    var $window    = $(window),
        offset     = $sidebar.offset(),
        timer;

    $window.scroll(function() {

        if ($content.height() > sidebarHeight) {
            var new_margin = $window.scrollTop() - offset.top;
            if ($window.scrollTop() > offset.top) {
                // Fix sidebar
                $sidebar.addClass("fixed");
                // Scroll it the appropriate ammount
                $sidebar.scrollTop(new_margin);            
            }else{
                $sidebar.removeClass("fixed");
            }
        }
    });
}

});

答案 1 :(得分:0)

我相信这是您正在寻找的功能:http://jsfiddle.net/JVe8T/7/

对于凌乱的代码感到抱歉,但优化它应该相当容易。我还假设sidebar2(非粘性的)已经定义了高度,如果不是这种情况你可以用jquery检测它并使用.css选择器进行底部偏移。

这是我的代码:

jQuery(document).ready(function() {

    var tmpWindow = $(window),
        sidebar = $('.sidebar'),
        content = $('.content'),
        sidebar1 = $('.sidebar1'),
        sidebar2 = $('.sidebar2'),
        viewportHeight = $(window).height(),
        sidebarHeight = sidebar.height(),
        sidebar1Height = sidebar1.height(),
        sidebar2Height = sidebar2.height(),
        offsetBottom;


    tmpWindow.scroll(function(){

        offsetBottom = content.height() - sidebar2Height;

        //if sidebar height is less that viewport
        if (viewportHeight > sidebarHeight) {
            sidebar.addClass('fixed');
        } 

        //sticky sidebar1
        if ((tmpWindow.scrollTop() + viewportHeight) > sidebar1Height ) {
            sidebar1.addClass('bottom');
        } else {
            sidebar1.removeClass('bottom');
        }

        //end of content, visible sidebar2
        if ((tmpWindow.scrollTop() + viewportHeight) > offsetBottom) {
            sidebar1.removeClass('bottom');
            sidebar1.addClass('fixedBottom');
        } else {
            sidebar1.removeClass('fixedBottom');
        }

    });

});

答案 2 :(得分:0)

查看hcSticky,我只是在找这个。它是一种“完美”的粘性边栏,也可以通过选项模拟其他库。

第一个演示可能是每个人都需要的,它独立于主要内容滚动容器。 (您可以在到达页面底部之前浏览整个侧边栏,或者在到达页面顶部之前向上滚动条形图。)

检查出来:http://someweblog.com/demo/hcsticky/

相关问题