Twitter bootstrap change affix offset

前端 未结 3 951
心在旅途
心在旅途 2021-02-04 14:33

I have site where I want \'sub navigation\'. So when you scroll to section, its tool bar will affix below the main toolbar. I have this working, but I can\'t change the top offs

相关标签:
3条回答
  • 2021-02-04 15:14

    I figured it out. Instead of dynamically updating the offset, I changed the offset value to a function:

    $(function () {
        $('.sec-nav').addClass("affix-top").each(function (){
            var $self = $(this);
            var offsetFn = function () {
                var $p = $self.closest('.sec');
                var $$ = $p.prevAll('.sec');
                var h = 0;
                $$.each(function () { h+=$(this).outerHeight();});
                return h;
            }
            $self.affix({offset: {top: offsetFn}});
        });
    });
    

    Now when a section changes due to dom manipulation or window re-size, the offset is automaticly changed due to the functions return value changing.

    0 讨论(0)
  • 2021-02-04 15:15

    how about:

                $(window).off('.affix');
                $("#ID_NAME")
                    .removeClass("affix affix-top affix-bottom")
                    .removeData("bs.affix");
                $('#ID_NAME').affix({ 
                  offset: {
                    top: OTHER_VALUE
                  }
                });
    
    0 讨论(0)
  • 2021-02-04 15:28

    I simplified Fatmuemoo's answer slightly for a more basic case: My home page's nav bar (the default bootstrap #masthead) sits below a big background slider with id #bg. The height of the slider varies with 3 css breakpoints. We just set the offset of the #masthead to equal the height of the slider. Easy!

        $('header#masthead').affix({ //sticky header at bottom of bg
            offset: {
              top: function(){return $("#bg").outerHeight();}
            }
        });
    
    0 讨论(0)
提交回复
热议问题