首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在jquery脚本中添加.fadeIn特效?

如何在jquery脚本中添加.fadeIn特效?
EN

Stack Overflow用户
提问于 2017-05-02 21:02:03
回答 1查看 45关注 0票数 0

我曾尝试在脚本中添加.fadeIn效果,而不是.animate,但它不起作用。基本上,当您滚动到元素时,我会尝试使元素淡入。这是脚本和HTML:

代码语言:javascript
复制
$(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'
        }, 1500);

      }
    });
  });
});
代码语言:javascript
复制
.hideme {
  opacity: 0;
  height: 300px;
}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="hideme">
  <p>This is some text.</p>
</div>
<div class="hideme">
  <p>This is some text.</p>
</div>
<div class="hideme">
  <p>This is some text.</p>
</div>

EN

回答 1

Stack Overflow用户

发布于 2017-05-02 21:34:18

代码语言:javascript
复制
$(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).fadeIn(400, function(){
					$(this).css('opacity', 1)
				});
			}
		});
	});
});
代码语言:javascript
复制
.hideme {
  opacity: 0;
  height: 300px;
}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<div class="hideme">
  <p>This is some text.</p>
</div>
<div class="hideme">
  <p>This is some text.</p>
</div>
<div class="hideme">
  <p>This is some text.</p>
</div>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43738610

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档