为什么我会收到错误:Uncaught TypeError:无法使用此代码读取null的属性“top”

时间:2013-10-24 03:15:16

标签: javascript jquery html

我继续在我的主页未捕获的TypeError:使用此代码无法读取属性'top'of null 时出现错误。

// Load Carousel on Scroll.
$(window).scroll(function () {
    $loadedContainer = $('.pane-idevels-browse-genres-custom-content-load .pane-content');
    offsetY = $loadedContainer.offset().top;
    cH = $loadedContainer.outerHeight();
    wH = $(window).height();
    bodyScroll = $('body').scrollTop();
    htmlScroll = $('html').scrollTop();
    scrollTop = (bodyScroll == 0) ? htmlScroll : bodyScroll;
    carouselOffset = offsetY + cH;
    windowOffset = wH + scrollTop;
    if (carouselOffset <= windowOffset) {
        loadCarouselGenres($loadedContainer)
    }
})

2 个答案:

答案 0 :(得分:0)

也许jQuery找不到本节中的元素

$('.pane-idevels-browse-genres-custom-content-load .pane-content');

答案 1 :(得分:0)

试试这个:

$(window).scroll(function () {
    $loadedContainer = $('.pane-idevels-browse-genres-custom-content-load .pane-content');

    if($loadedContainer){

       offsetY = $loadedContainer.offset().top;
       cH = $loadedContainer.outerHeight();
       wH = $(window).height();
       bodyScroll = $('body').scrollTop();
       htmlScroll = $('html').scrollTop();
       scrollTop = (bodyScroll == 0) ? htmlScroll : bodyScroll;
       carouselOffset = offsetY + cH;
       windowOffset = wH + scrollTop;
       if (carouselOffset <= windowOffset) {
         loadCarouselGenres($loadedContainer)
       }
    }
});