2016-05-16 3 views
1

現在、私のサイトの一部のコンテナのスクロールでフェードインをアニメーション化するために次の方法を使用していますが、コンテナの一部が高さが高いため、変更する方法がありますウィンドウの底がコンテナの底ではなくコンテナの中心にあるときに消えますか?あるいは、ピクセルで指定する方法はありますか?コンテナのスクロールセンターでフェードインする

$(window).scroll(function() { 

    /* Check the location of each desired element */ 
    $('.container').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

このようなものはありますか?

$(window).scroll(function() { 

    /* Check the location of each desired element */ 
    $('.container').each(function (i) { 

     var top_of_object = $(this).offset().top; 
     var middel_of_window = $(window).scrollTop() + ($(window).height()/2); 

     /* If the object is completely visible in the window, fade it in */ 
     if (middel_of_window > (top_of_object + 300)) { 

      $(this).animate({ 
       'opacity': '1' 
      }, 500); 

     } 
    }); 
}); 

希望します。

+0

それは私のコンテナのカップルのためにまだそれほど正しくないが、ありがとうございます。 ピクセル単位で指定する方法はありますか?例えばウィンドウの下部がコンテナの中に300ピクセル入ったときにフェードインしますか? –

+0

@MikeRainsbury [ドキュメントでは戻り値がピクセルであると表示されています](http://www.w3schools.com/jquery/css_offset.asp)、300を追加すると、アニメーション化する前に中間300pxをobjに挿入します。 –

関連する問題