offset()。top给出了错误的值

时间:2015-11-04 14:26:35

标签: javascript html css

我正在使用此js将类添加到当前节,并根据数据属性将类添加到当前导航元素。

IndexedDB

这是示例html

var cutoff = $(window).scrollTop(),
    cutoffRange = cutoff + 88,
    curSec = $('#main-page').find('.s-active'), // Current section
    curID = $(curSec).attr('id'), // Current section ID
    curNav = $('.navigation').find('li[data-navSections*="'+curID+'"]'); // Find current menu item

    $('.navigation li').removeClass('current-menu-item');
    $(curNav).addClass('current-menu-item');

$('.section').each(function(){

    if ($(this).offset().top > cutoff && $(this).offset().top < cutoffRange) {
        $('.section').removeClass('s-active')
        $(this).addClass('s-active');
        return false; // stops the iteration after the first one on screen
    }

});

第一个看法是一种视差,第二部分的边缘 - 顶部由另一个脚本添加。

上面的脚本运行良好,但不会将类添加到第一个固定的home部分。这不是offset()的错误.top?

的jsfiddle http://jsfiddle.net/f5ans6g6/

1 个答案:

答案 0 :(得分:3)

这是一个错字;) 在此代码中:$('.section').each(function(){});您指向名为.section的类。这不可用。

试试这个:

$('section')
相关问题