[英]I have a fixed div that I am using jquery to fade out on scroll. It works fine but if I am half way down and refersh the page the div shows up
I have a landing page with a full height
and width
div and a background image.我有一个带有全
height
和全width
div 以及背景图像的登录页面。 I am floating a fixed div in front and fading it out as the user scrolls up.我在前面浮动一个固定的 div 并在用户向上滚动时将其淡出。 It's great unless someone refreshes the page when they're half way down, then the fixed div shows up again.
除非有人在页面下降一半时刷新页面,然后固定的 div 再次出现,否则这很好。 How do I only show div if it's "above the fold"?
如果它是“首屏”,我如何只显示 div?
<script type="text/javascript"> jQuery(function( $ ){ $(window).on("scroll", function() { var scrollPos = $(window).scrollTop(); if (scrollPos <= 0) { $(".blue_bar").fadeIn(); } else { $(".blue_bar").fadeOut(); } }); }); </script>
.blue_bar { width:75%; max-width:900px; padding: 40px 65px 40px 65px; position:fixed; bottom:95px; right:55px; background-color:rgba(0,105,170,0.8); }
Thats because your code for fading out doesn't get executed if the page reloads.那是因为如果页面重新加载,您的淡出代码不会被执行。 So what you can do is enclose that logic inside a function and call it on page load as well as on scroll.
因此,您可以做的是将该逻辑包含在一个函数中,并在页面加载和滚动时调用它。
HTML HTML
<div class="blue_bar" style="display:none;">
blah
</div>
JS JS
jQuery(function( $ ){
let processFade = function(){
var scrollPos = $(window).scrollTop();
if (scrollPos <= 0) {
$(".blue_bar").fadeIn();
} else {
$(".blue_bar").fadeOut();
}
};
$(window).on("scroll load", function () {
processFade();
});
$(document).ready(function () {
processFade();
});
});
Update: " The div shows up, then quickly disappears again. "更新: “ div 出现了,然后又很快消失了。 ”
The solution is pretty simple, Just start out with a hidden div
:) Updated the code to reflect the same.解决方案非常简单,只需从隐藏的
div
:) 更新代码以反映相同情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.