我想将 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/