简体   繁体   中英

problems with “Change header background colour when page scrolls”

I read this post on this from Feb. but I am having a problem getting it to work. I set up an index.html file, copied code from fiddle: http://jsfiddle.net/634d6vgq/2/ but am still having problems getting it to work. I am overlooking something but am not sure what. I followed everything that was posted in the answer. Here is the code I set up:

<style>

* {margin:0;padding:0}

html {
    background: lightgray;
    height: 5000px;
}

.header {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    padding: 0;
    z-index: 10000;
    transition: all 0.2s ease-in-out;
    height: auto;
    background-color:transparent;  

    text-align: center;
    line-height: 40px;
}

.active { background-color: #fff}

.header.active {
    background: #fff;
    -webkit-box-shadow: 0 1px 5px rgba(0, 0, 0, 0.25);
    -moz-box-shadow: 0 1px 5px rgba(0, 0, 0, 0.25);
    box-shadow: 0 1px 5px rgba(0, 0, 0, 0.25);
}

</style>

</head>
<body>

<div class="header">the header</div>

<script>

$(function() {
    $(window).on("scroll", function() {
        if($(window).scrollTop() > 50) {
            $(".header").addClass("active");
        } else {
           $(".header").removeClass("active");
        }
    });
});

</script>


</body>

You have forgotten to include the link to jQuery itself. You need to either link it like so

  <script src="jquery-1.11.2.min.js"></script>

Or else download jQuery and link it as such.

To understand the problem more you should take a look at this link. http://www.w3schools.com/jquery/jquery_get_started.asp

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