简体   繁体   中英

How to make my navigation bar turn semitransparent on scroll?

looking to make my navigation bar turn semitransparent when I scroll the page, to do this, I know I'll need to edit the ID header, and turn that to an rgba, however, how do I make it so that when I scroll down the page, my header becomes semitransparent? Some kind of JavaScript or jQuery I presume, anyway, here's my code, can anyone guide me?

        <header id="header">

        <div class="container">
            <div class="main">

                <div class="logo">
                    <a href="index.html"><div class="top-logo"></div></a>
                </div>
                <nav>
                    <ul>
                        <li><a href="index.html" class="active">Home<span class="border"></span></a></li>
                        <li><a href="my-portfolio.html">My Portfolio<span class="border"></span></a></li>
                        <li><a href="about-me.html">About & Contact<span class="border"></span></a></li>
                    </ul>
                </nav>

            </div>
        </div>

        </header>

 #header {
     position: fixed;
     display: block;
     width: 100%;
     top: 0;
     height: 77px;
     z-index: 9;
     min-width: 320px;
     background: #EFEFEF;
 }

Try this script:

$( window ).scroll(function() {
$( "#header" ).css( "background-color", "rgba(55,55,55,0.4" );
});

Flopet17's answer was my first try as well.

However, the background will stay semi-transparent when you stop scrolling. For that, you have to 'reset' the css after scrolling stops. (Or remove the additional class name.) For this 'onscrollstop' functionality, have a look at: jQuery scroll() detect when user stops scrolling

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM