JQuery在滚动问题上添加类

JQuery Add Class On Scroll Issue

本文关键字:添加 问题 滚动 JQuery      更新时间:2024-03-16

我在滚动代码上的add类有问题。。。它不是在滚动过元素后添加类,而是在用户开始滚动时立即将类添加到每个元素中。

http://sandbox.viaphase.com/ajs-presentation/

$(document).ready(function() {
    $(window).scroll( function(){
        /* Check the location of each desired element */
        $('.animscroll').each( function(i){
            var bottom_of_object = $(this).offset().top + $(this).outerHeight();
            var bottom_of_window = $(window).scrollTop() + $(window).height();
            /* If the object is completely visible in the window, fade it it */
            if( bottom_of_window > bottom_of_object ){
                $(this).addClass('SlideUp'); //Adds animation class to element
            }
        }); 
    });
});

问题不在于您的代码,$(window).height()报告的与$(document).height();相同

这是因为您的html中没有DOCTYPE。

添加:

<!DOCTYPE html>到页面顶部,您的代码应该可以正常工作。