0

同じクラスの要素から要素へとコンテンツをスクロールするためのjqueryスクリプトがあります...ボタンを次に初めて押すとすべて正常に機能し、最初の要素にスクロールされますが、2回目に押すと次の要素にスクロールしますコンソール エラー:

TypeError: オフセットが null です

脚本:

    /**  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);
    })
});

http://pastebin.com/t0PpU7Vmのように 1 つのファイルにすべてが含まれているため、html は少し面倒です。

4

0 に答える 0