如何使元素在页面滚动时淡入?

发布于 2024-12-18 05:29:23 字数 348 浏览 3 评论 0原文

我的页面中有以下元素:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

溺孤伤于心 2024-12-25 05:29:23

查看 jQuery Waypoints

使用这个插件,你可以做这样的事情:

<div id="banner">Hello</div>

和你的 jQuery:

$("#banner").waypoint(function(){
    $(this).fadeIn(750);
},{
    offset: 'bottom-in-view'
});

Check out jQuery Waypoints.

Using this plugin, you could do something like this:

<div id="banner">Hello</div>

And your jQuery:

$("#banner").waypoint(function(){
    $(this).fadeIn(750);
},{
    offset: 'bottom-in-view'
});
紫瑟鸿黎 2024-12-25 05:29:23

我没有制作完整的工作示例。但是每次窗口滚动时,您都需要使用 $(window).scroll(function(){ 来执行。然后在该函数中您需要检查 $(window)。 来确定用户滚动了多远。

scrollTop() a>

然后基于此值,您将添加和删除类或 fadeInfadeOut 标题说您想要“淡入淡出”,在这种情况下您将需要使用 fadeIn。 code> 和 fadeOut 如果您只想添加和删除类,请使用 addClassremoveClass

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 and fadeOut. The title says you want "fade" in which case you'll want to use fadeIn and fadeOut. If you do just want to add and remove a class, then use addClass and removeClass.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文