Twitter引导程序更改词缀偏移量 [英] Twitter bootstrap change affix offset
问题描述
我有一个我想要子导航"的网站.因此,当您滚动到该部分时,其工具栏将粘贴在主工具栏下方.我已经完成了这项工作,但是无法在初始化之后更改顶部偏移.文档说:
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 offset after initailization. The docs say:
缀('refresh')
affix('refresh')
将词缀与元素添加或删除结合使用时 从DOM中,您将要调用refresh方法:
When using affix in conjunction with adding or removing of elements from the DOM, you'll want to call the refresh method:
但是当我尝试这样做时,我得到了:
but when I try this I get:
未捕获的TypeError:对象#没有方法'refresh'
Uncaught TypeError: Object # has no method 'refresh'
这是我的代码:
$(function () {
$('.sec-nav').addClass("affix-top");
var adjustNavs = function (first) {
$('.sec-nav').each(function (){
var $p = $(this).closest('.sec');
var $$ = $p.prevAll('.sec');
console.log($$);
var h = 0;
$$.each(function () { h+=$(this).outerHeight();});
console.log(h);
if (first) {
$(this).affix({offset: {top: h}});
} else {
$(this).data('affix', {b: {options: {offset:{top: h}}}});
console.log(this);
$(this).affix('refresh')
}
});
}
adjustNavs(true);
$('.sec').bind('DOMSubtreeModified', function () {
adjustNavs();
});
});
我尝试了
$(this).data('affix', {b: {options: {offset:{top: h}}}});
包括:
$(this).affix({offset: {top: h}});
我无法更改偏移量.如何更改词缀插件的偏移量?谢谢.
I cannot get the offset to change. How do you change the offset of the affix plugin? Thanks.
推荐答案
我知道了.我没有动态更新偏移量,而是将偏移量值更改为一个函数:
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}});
});
});
现在,当由于dom操作或窗口调整大小而改变部分时,由于函数返回值的改变,偏移量将自动改变.
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.
这篇关于Twitter引导程序更改词缀偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!