0

我有 jquery 脚本,用于在具有相同类的元素之间滚动内容...当我下一次按下按钮时一切正常,它会滚动到第一个元素,但是当我第二次按下它以滚动到下一个元素时,我得到了控制台错误:

类型错误:偏移量为空

脚本:

    /**  scroll to element function **/

function scrollToElement(selector, time, verticalOffset) {
    time = typeof (time) != 'undefined' ? time : 500;
    verticalOffset = typeof (verticalOffset) != 'undefined' ? verticalOffset : 0;
    element = $(selector);
    offset = element.offset();
    offsetTop = offset.top + verticalOffset;
    $('html, body').animate({
        scrollTop: offsetTop
    }, time);
}

/**document ready**/
$(document).ready(function () {
    count = 0;
    var max_length = $('.highlight1').length;
    /* scroll to 150px before .highlight with animation time of 400ms */
    $('#next1').click(function (e) {
        if (count < max_length) {
            count++;
        } else {
            count = 1;
        }
        e.preventDefault();
        scrollToElement('.highlight1:nth-child(' + count + ')', 400, -150);
    });

    $('#prev1').click(function (e) {
        if (count > 1) {
            count--;
        } else {
            count = max_length;
        }
        e.preventDefault();
        scrollToElement('.highlight1:nth-child(' + count + ')', 400, -150);
    })
});

html 有点混乱,因为一个文件中的所有内容看起来像这样http://pastebin.com/t0PpU7Vm

4

0 回答 0