我有网站我想要’sub navigation’.所以当您滚动到部分时,它的工具栏会粘贴在主工具栏下面.我有这个工作,但是我不能在初始化后更改顶部的偏移量.文件说:
affix(‘refresh’)
When using affix in conjunction with adding or removing of elements
from the DOM,you’ll want to call the refresh method:
但是当我尝试这个我得到:
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}});
我无法获得更改的偏移量.你如何改变贴片插件的偏移?谢谢.
解决方法
我想到了.而不是动态更新偏移量,我将偏移值更改为一个函数:
$(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}}); }); });