jquery - Bootstrap 词缀的 unaffix 事件?

标签 jquery twitter-bootstrap events twitter-bootstrap-3 jquery-events

我想将 affix 插件与 bootstrap navbar-fixed-top 类结合起来。到目前为止,我已经让它工作了,然后当我滚动经过导航栏时它就被修复了。但是当我向后滚动时,我希望它再次回到静态状态。我看到了一些我认为来自旧 Bootstrap 版本和 unaffix 事件的代码。为什么不见了?我可以创建一个吗?或者如何完成我在这里尝试的事情?

navbar_secondary = $( '.navbar-secondary:first' );

navbar_secondary.affix( {
    offset: {
        top: function () {
            return (this.top = navbar_secondary.offset().top )
        }
    }
} );

navbar_secondary.on( 'affix.bs.affix', function () { // this is actually the wrong event for this. I want this to fire when its *not* affixed
    console.log('affix');
    navbar_secondary.removeClass( 'navbar-fixed-top' ).addClass( 'navbar-not-fixed' );
} );

navbar_secondary.on( 'affixed.bs.affix', function () {
    console.log('affixed');
    navbar_secondary.removeClass( 'navbar-not-fixed' ).addClass( 'navbar-fixed-top' );
} );

最佳答案

我自己想出来了。这个事件的名称完全令人困惑。 affixed-top.bs.affix 实际上是回到未粘贴状态时的事件。

navbar_secondary = $( '.navbar-secondary:first' );

navbar_secondary.affix( {
    offset: {
        top: function () {
            return (this.top = navbar_secondary.offset().top )
        }
    }
} );

navbar_secondary.on( 'affixed-top.bs.affix', function () {
    console.log('unaff');
    navbar_secondary.removeClass( 'navbar-fixed-top' ).addClass( 'navbar-not-fixed' );
} );

navbar_secondary.on( 'affix.bs.affix', function () {
    console.log('aff');
    navbar_secondary.removeClass( 'navbar-not-fixed' ).addClass( 'navbar-fixed-top' );
} );

摘要

affix.bs.affix => before fixed positioning is applied to an element
affixed.bs.affix => after fixed positioning is applied to an element
affix-top.bs.affix => before a top element returns to its original (non-fixed) position
affixed-top.bs.affix => after a top element returns to its original (non-fixed) position
affix-bottom.bs.affix => before a bottom element returns to its original (non-fixed) position
affixed-bottom.bs.affix => after a bottom element returns to its original (non-fixed) position

关于jquery - Bootstrap 词缀的 unaffix 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22899844/

相关文章:

html - Bootstrap 顶部导航栏 - 如何在中心添加一个元素

html - Bootstrap tr 类 ="info"版本 3.3.7 仍然不起作用

C# 检测表单何时变得不集中?

javascript - 在 Ember 2.6 中捕获粘贴内容

javascript - jQuery 轮播 slider 停留在灯箱图像的前面

javascript - 如何停止在 dropzone 上上传第二张图片?

html - Bootstrap Glyphicons 未在本地显示

c++ - 依赖注入(inject)和事件处理

javascript - 如何使用 jQuery 将 div 的滚动条保留在 div 的底部?

javascript - 递延/ promise 相互依赖