如何在窗口大小调整时重置jQuery scrollspy的最小/最大值

时间:2014-02-27 08:46:07

标签: jquery resize position scrollspy

我正在使用此scrollspy插件,我需要在调整窗口大小时重置最小/最大值,因为第一部分调整高度并影响所有position.top以下部分。最好的方法是什么?

$('section').each(function() {
    var position = $(this).position();
    $(this).scrollspy({
        min: position.top - $header.outerHeight(),
        max: position.top + $(this).outerHeight() - $header.outerHeight(),
        onEnter: function(element) {
            $('#main-menu').find('a[href$="'+$(element).attr('id')+'"]').addClass('cur');
            /* window.location.hash = 'panel-'+$(element).attr('id'); */
        },
        onLeave: function(element) {
            $('#main-menu').find('a[href$="'+$(element).attr('id')+'"]').removeClass('cur');
        }
    });
});

2 个答案:

答案 0 :(得分:0)

尝试将section元素的位置设置为relative。

position:relative;

快乐编码:)

答案 1 :(得分:0)

我有一个类似的问题,但我无法将我的元素更改为positon relative,因为它们都是自定义动画,需要处于绝对位置。

这是我的方法。

// Make sure to update Scrollspy once the last resize has fired
var rtime = new Date(1, 1, 2000, 12,00,00);
var timeout = false;
var delta = 200;
$(window).resize(function() {
    rtime = new Date();
    if (timeout === false) {
        timeout = true;
        setTimeout(resizeend, delta);
    }
});

function resizeend() {
    if (new Date() - rtime < delta) {
        setTimeout(resizeend, delta);
    } else {
        timeout = false;
        $(window).unbind("scroll");
        // Your Scrollspy function
    }               
}