[英]scrollTop() shows result only after page refresh
Hii I am working on jquery but my code working in a weird manner.嗨,我正在研究 jquery,但我的代码以一种奇怪的方式工作。 Here is my code这是我的代码
$(document).ready(function() {
if($(window).scrollTop() >= 50) {
$("h1").fadeIn("slow");
};
})
I want my text to fadein when scroll.我希望我的文字在滚动时淡入淡出。 But my text appear ony after I refresh page after scrolling.但是我的文字只有在我滚动后刷新页面后才会出现。 I dont know Why what is wrong with my code.我不知道为什么我的代码有什么问题。
As I wrote above in the comment, you need to wrap your logic in a window scrolling event.正如我在上面的评论中所写,您需要将您的逻辑包装在 window 滚动事件中。 Like this:像这样:
$(window).scroll(function() {
if($(window).scrollTop() >= 50) {
$("h1").fadeIn("slow");
}
})
I made an example for you by specifying the position: sticky
for the h1
tag so that you can visually understand how the code works.我通过为h1
标签指定position: sticky
为您做了一个示例,以便您可以直观地了解代码的工作原理。
$(document).ready(function() { $(window).scroll(function() { if($(window).scrollTop() >= 50) { $("h1").fadeIn("slow"); } }) })
body { height: 3000px; } h1 { display: none; position: sticky; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <h1>Test_Text</h1>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.