简体   繁体   English

滚动到所需的div时,如何使导航栏的颜色改变?

[英]How do you make a sticky nav bar change colour when you scroll to a desired div?

I am curious as to how you would make your sticky navigation bar fade into a colour as you scroll down to a certain point or DIV on the page. 我很好奇,当您向下滚动到页面上的某个点或DIV时,如何使粘性导航栏变成一种颜色。

I would like to have this so I can see the white over the white background when I scroll to the end of my div or at the beginning of the div. 我想要这个,所以当我滚动到div的末尾或div的开头时,可以在白色背景上看到白色。

Thank you! 谢谢!

HTML 的HTML

<!DOCTYPE html>

<html>
<head>
<meta charset="utf-8">
<title>Aesthetic Media</title>
<link href="styles.css" rel="stylesheet" type="text/css"/>
<link href='http://fonts.googleapis.com/css?family=Raleway:400,700,500' rel='stylesheet' type='text/css'>
<script type="text/javascript" src="styles.js"></script>
</head>

<body>

<header>

    <a class="logo" href="#">Aesthetic</a>

    <nav>
        <li><a href="#">Home</a></li>
        <li><a href="#">About</a></li>
        <li><a href="#">Gallery</a></li>
        <li><a href="#">Contact</a></li>
    </nav

    <div class="clears"></div>

    <div class="maintext">

        <h1>We're Aesthetic</h1>
        <h2>A Visual Agency from Niagara</h2>

    </div>

</header>

<main>

<h2 class="whatwedo">Expertise</h2>
<div class="whatwedobox one"><div class="icon"></div><p>Lorem ipsum dolor sit  amet.</p></div>
<div class="whatwedobox two"></div>
<div class="whatwedobox three"></div>

</main>

<footer></footer>

<script   src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js">          </script>
</body>
</html>

CSS 的CSS

* {
box-sizing: border-box;
margin: 0px;
padding: 0px;
}

body {
background: white;
}

header {
width: 100%;
height: 550px;
background: white;
background: url('images/main.jpg') center center;
background-size: cover;
padding: 30px 30px 30px 30px;
position: relative;
}

.logo {
position: fixed;
top: 33px;
left: 30px;
width: 200px;
height: 80px;
display: block;
float: left;
z-index: 30;
font-size: 1.8em;
font-weight: 800;
text-decoration: none;
color: #FFFFFF;
font-family: 'Raleway' , sans-serif, Helvetica, Verdana;
text-transform: uppercase;
font-weight: bold;
letter-spacing: 5px;  
}

.logo a {
font-size: 1.8em;
font-weight: 800;
text-decoration: none;
color: #FFFFFF;
font-family: 'Raleway' , sans-serif, Helvetica, Verdana;
text-transform: uppercase;
font-weight: bold;
letter-spacing: 5px;
}

nav {
float: right;
z-index: 30;
position: fixed;
top: 20px;
right: 0px;
letter-spacing: .2em;
}

nav li {
float: left;
list-style: none;
padding: 10px 15px 10px 15px;
}

nav li:last-child {
border-right: none;
}

nav li a {
padding: 5px 10px 5px 10px;
color: white;
display: block;
font-weight: 400;
text-decoration: none;
color: #FFFFFF;
font-family: 'Raleway' , sans-serif, Helvetica, Verdana;
text-transform: uppercase;
-webkit-font-smoothing: antialiased;
}

nav li a:hover {
background-color: rgba(40, 40, 40, 0.5);
-moz-transition: all .3s ease;
-o-transition: all .3s ease;
-ms-transition: all .3s ease;
-transition: all .3s ease;
-webkit-transition: all .3s ease;
-webkit-font-smoothing: antialiased;
}

.clears {
clear: both;
}

.maintext {
margin: auto;
width: 600px;
text-align: center;
margin-top: 200px;
color: white;
z-index: 30;
position: relative;
}

.maintext h1 {
font-weight: 600;
font-size: 1.8em;
line-height: 1.2em;
text-transform: uppercase;
letter-spacing: .1em;
color: #FFFFFF;
font-family: 'Raleway' , sans-serif, Helvetica, Verdana;
padding-bottom: 15px;
}

.maintext h2 {
font-weight: 400;
font-size: 1.7em;
line-height: 1.2em;
text-transform: uppercase;
letter-spacing: .1em;
text-align: center;
color: #FFFFFF;
font-family: 'Raleway' , sans-serif, Helvetica, Verdana;
padding-bottom: 15px;
}

main {
max-width: 1180px;
margin: auto;
margin-top: 20px;
overflow: hidden;
}

h2.whatwedo {
text-align: center;
font-weight: 400;
font-size: 1.7em;
line-height: 1.2em;
text-transform: uppercase;
letter-spacing: .1em;
color: #000000;
font-family: 'Raleway' , sans-serif, Helvetica, Verdana;
-webkit-font-smoothing: antialiased;
}

.whatwedobox {
width: 30%;
height: 300px;
border: 1px solid blue;
float: left;
margin-right: 5%;
padding: 20px;
margin-top: 20px;
}

.whatwedobox:last-child {
margin-right: 0%;
}

.icon {
width: 100%;
height: 100px;
background: url(#) no-repeat center center;
}

JS JS

$(document).ready(function(){       
var scroll_start = 0;
var startchange = $(".maintext");
var offset = startchange.offset();

 if (startchange.length){
 $(document).scroll(function() { 
  scroll_start = $(this).scrollTop();
  if(scroll_start > offset.top) {
      $("header").css('background-color', '#B5B5B5');
   } else {
      $('header').css('background-color', 'transparent');
   }
});
}
});

You can compare $(window).scrollTop() with offset().top of that div. 您可以将$(window).scrollTop()与该div的offset().top进行比较。 offset() is a method , not a property . offset()是一种method ,而不是property Hence, calling offset() will return both top and left . 因此,调用offset()将同时返回topleft

if($(window).scrollTop()>=$("that_div").offset().top) {
   //do something here
}

In your code, try replacing the line 在您的代码中,尝试替换该行

var offset = startchange.offset;

with

var offset = startchange.offset();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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