jquery – Twitter bootstrap更改粘贴偏移

前端之家收集整理的这篇文章主要介绍了jquery – Twitter bootstrap更改粘贴偏移前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有网站我想要’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}});
    });
});

现在当一个部分由于dom操作或窗口重新设置而改变时,由于函数返回值的改变,偏移量会自动更改.

猜你在找的jQuery相关文章