Affix is not working in Bootstrap 4 (alpha)

前端 未结 10 500
暖寄归人
暖寄归人 2020-12-02 21:30

According to Bootstrap 3 docs I have added following attributes to a navbar:

相关标签:
10条回答
  • 2020-12-02 22:17

    To build on Anwar Hussain's answer. I found success with this:

    $(window).on('scroll', function (event) {
        var scrollValue = $(window).scrollTop();
        if (scrollValue > 120) {
            $('.navbar').addClass('affix');
        } else{
            $('.navbar').removeClass('affix');
        }
    });
    

    This will apply the class to the navbar when scrolling down, but will also remove the class when scrolling back up. Previously, when scrolling back up, the applied class would stay applied to the navbar.

    0 讨论(0)
  • 2020-12-02 22:20

    With bootstrap 4 there is special class for that. Try removing the data-spy="affix" data-offset-top="90" attributes, and just add 'sticky-top'.

    documentation

    0 讨论(0)
  • 2020-12-02 22:22

    Although the affix is removed from Bootstrap in version 4. However, you can achieve your goal through this jQuery Code:

    $(window).on('scroll', function(event) {
        var scrollValue = $(window).scrollTop();
        if (scrollValue == settings.scrollTopPx || scrollValue > 70) {
             $('.navbar').addClass('fixed-top');
        } 
    });
    
    0 讨论(0)
  • 2020-12-02 22:27

    After trying every solution I could find (and being aware that affix was removed from bootstrap 4), the only way I could get the desired sticky results I needed was to use sticky-kit.

    This is s really simple jQuery plugin that was easy to set-up.

    Just include the code in your project and then assign the element you want to stick with

    $("#sidebar").stick_in_parent();
    
    0 讨论(0)
提交回复
热议问题