如何使元素在页面滚动时淡入?
我的页面中有以下元素:
<div id="banner" class="disappear">Hello</div>
该元素的 CSS 是:
#banner {position:fixed;}
.disappear {opacity:0;}
.appear {opacity:1;}`
我想知道如何使用 jQuery 将类从 .disappear
更改为 .appear
页面滚动到某个点,然后当页面返回到原始位置时返回到 .disappear
。
I have the following element in my page:
<div id="banner" class="disappear">Hello</div>
The CSS for this element is:
#banner {position:fixed;}
.disappear {opacity:0;}
.appear {opacity:1;}`
I'd like to know how to change the class from .disappear
to .appear
with jQuery when the page scrolls to a certain point, and then back to .disappear
when the page returns to original position.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看 jQuery Waypoints。
使用这个插件,你可以做这样的事情:
和你的 jQuery:
Check out jQuery Waypoints.
Using this plugin, you could do something like this:
And your jQuery:
我没有制作完整的工作示例。但是每次窗口滚动时,您都需要使用
$(window).scroll(function(){
来执行。然后在该函数中您需要检查$(window)。 来确定用户滚动了多远。
scrollTop() a>
然后基于此值,您将添加和删除类或
fadeIn
和fadeOut
标题说您想要“淡入淡出”,在这种情况下您将需要使用fadeIn
。 code> 和fadeOut
如果您只想添加和删除类,请使用addClass
和removeClass
。I didn't make a full working example. But you'll need to use
$(window).scroll(function(){
to execute every time the window scrolls. Then in that function you will need to check$(window).scrollTop()
to determine how far the user has scrolled.http://jsfiddle.net/s8dhy/
Then based on that value, you will add and remove classes or
fadeIn
andfadeOut
. The title says you want "fade" in which case you'll want to usefadeIn
andfadeOut
. If you do just want to add and remove a class, then useaddClass
andremoveClass
.