为什么我的Javascript运行缓慢?

时间:2018-03-09 12:38:53

标签: javascript

有人可以告诉我为什么我的JS可能会跑得超慢吗?

我有什么办法可以加快速度吗?

谢谢!

$(document).ready(function() {

/* Every time the window is scrolled ... */
$(window).scroll( function(){

    /* Check the location of each desired element */
    $('.hideme').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).animate({'opacity':'1'},500);

        }

    }); 

});

});

1 个答案:

答案 0 :(得分:-2)

我无法帮助,但请注意您如何拨打$()来引用thiswindow。这是一个次要的表现建议:



$(document).ready(function() {

$(window).scroll( function(){

    $('.hideme').each( function(i){

        var $el = $(this),
            $window = $(window),
            bottom_of_object = $el.offset().top + $el.outerHeight(),
            bottom_of_window = $window.scrollTop() + $window.height();
            
        if( bottom_of_window > bottom_of_object )
           $el.animate({'opacity':'1'},500);

    }); 

});

});