繁体   English   中英

当元素进入视口时jQuery FadeIn()

[英]JQuery fadeIn() when element comes into viewport

当向下滚动页面时,我想在它们进入视口视图时立即淡入淡出不同的元素。

但是,我的代码一次出现在所有DIV(具有“ .fadethis”类)中,而不是仅出现在该特定元素中时:

$(window).scroll(function() {
  $(".fadethis").each(function() {
  var top_of_element = $(this).offset().top;
  var bottom_of_element = $(this).offset().top + $(this).outerHeight();
  var bottom_of_screen = $(window).scrollTop() + window.innerHeight;
  var top_of_screen = $(window).scrollTop();

  if((bottom_of_screen > top_of_element) && (top_of_screen < bottom_of_element)){
   $(this).fadeIn(10000);
  }

 });
});

的CSS

.fadethis{
display:none;
}

导入jQuery, animate.lessWaypoint

Javascript:

$(document).ready(function(){
    $('.fromLeft').waypoint(function()
    {
      $(this.element).css('opacity',1);
      $(this.element).addClass("bounceInLeft");
    },
    {offset:'100%'});
    $('.fromRight').waypoint(function()
    {
      $(this.element).css('opacity',1);
      $(this.element).addClass("bounceInRight");
    },{offset:'100%'});
    $('.appear').waypoint(function()
    {
      $(this.element).css('opacity',1);
      $(this.element).addClass("fadeIn");
    },{offset:'100%'});
});

和HTML

<div class="animated onView fromLeft">
appears from left
</div>

在这里您可以看到我做的一个例子

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM