简体   繁体   English

我有一个固定的 div,我正在使用 jquery 在滚动时淡出。 它工作正常,但如果我半途而废并引用页面,div 就会显示出来

[英]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.

相关问题 我的主页上有这个“div”,希望它在我滚动时随我移动,顺便说一句,我正在为网站使用 react - I have this `div` on my home page and want it to move with me when I scroll, I am using react for the website btw 为什么我无法向下滚动页面? - Why am I not able to scroll down a page? 使用jQuery加载页面后,如何淡入div? - How can I fade a div after the page is loaded using jQuery? 我已经为外部div编写了代码,我可以在其中上下移动,但不能左右移动 - I have written code for outer div, where I am able to move up and down but unable to move left right 如何使向下滚动页面上每个 div 的文本淡入 animation? - How do I make a text fade in animation for each div on scroll down page? 向下滚动certian amont时,我试图使div出现? - I am trying to make a div appear when i scroll down a certian amont ? 在固定位置div上使用jquery淡入/淡出功能时,防止页面自动向上滚动 - prevent automatically page scroll up when using jquery fadein/out founction on fixed position div 固定的div在滚动时无法检测到页面上的位置 - Fixed div is not detecting where I am on page when I'm scrolling 我如何才能对必须消失的div施加淡入淡出效果,对使用JQuery替换它的div施加淡入效果? - How can I apply a fadout effect to a div that have to disappear and a fade in effect to a div that replaces it using JQuery? 如何在父div中使用Jquery而不影响子div的情况下淡入和淡出背景图像? - How can I fade are background image in and out using Jquery in a parent div without having it affect the child div?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM