【问题标题】:Bootstrap affix start in the middle of the pageBootstrap 词缀从页面中间开始
【发布时间】:2014-08-02 02:45:44
【问题描述】:

我希望我的页面上有多个部分,每个部分都有一个堆叠的导航。堆叠导航应保留在部分 div 中。 第一个堆叠导航位于第一部分。

第二个堆叠导航在滚动时跳到底部并返回。 谁能解释一下发生了什么,以及如何解决这个问题。

演示: http://www.bootply.com/vAT4FJgSMS#

【问题讨论】:

    标签: jquery css twitter-bootstrap twitter-bootstrap-3 affix


    【解决方案1】:

    你可以一次性定位所有可附加的侧边栏,方法是给它们一些标识类,然后applying affix for each element with the class

    $('.sideNav').each(function() {
        $(this).affix({ /* options */ });
    });
    

    然后你只需要为每个部分设置顶部和底部。

    $(this).affix({
        offset: {
            top: function(e) {
                var $curSection = $(e).closest('.row');
                return (this.top = $curSection.offset().top - 10);
            },
            bottom: function (e) {
                var $nextSection = $(e).closest('section').next('section');
                //if last element, go to bottom of page
                var bottom = ($nextSection.length === 0) ? 0 :
                             $(document).height() - $nextSection.offset().top;
                return (this.bottom = bottom);
            }
        }
    });
    

    当元素不再在topbottom 的范围内时,应用affix-topaffix-bottom。您通常只希望这些元素返回正常流程,因此您可以像这样设置您的 css(甚至完全忽略,因为相对是默认位置):

    .affix-top,
    .affix-bottom {
        position: relative;
    }
    

    应用词缀类后,您可以像这样设置列表样式:

    .affix {
        position: fixed !important; 
        top: 10px; 
    }
    

    Working Demo in fiddle

    【讨论】:

    • 非常感谢,它有效。我在这个问题上花了很多时间,甚至从未接近过这个解决方案。
    猜你喜欢
    • 2013-06-19
    • 1970-01-01
    • 2013-04-22
    • 1970-01-01
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多