滚动时菜单背景颜色的更改

5

我尝试实现的是当用户到达网站的下一部分时,将菜单的背景从透明变为浅灰色。我已经搜遍了各种脚本,并在这里尝试了多个脚本,但似乎都不起作用。

非常感谢您的帮助。

谢谢

HTML

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Upbeat Designs | Freelance Web Design</title>

        <link rel="stylesheet" href="style.css" type="text/css">
        <link href='http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>
    </head>

    <script type="text/javascript">
$(window).scroll(function() {    
    var scroll = $(window).scrollTop();

    if (scroll >= 500) {
        $(".header").addClass("scrolling");
    } else {
        $(".header").removeClass("scrolling");
    }
});
        </script>

    <body>
        <!-- header area -->
        <div class="header">
            <div id="logo">
                <a href="#"><img src="images/logo.png" /></a>
            </div>
            <nav>
                <input id="nav" type="checkbox" />
                <label for="nav">
                    <strong>Menu</strong>
                    <b><i></i><i></i><i></i></b>
                </label>
                <menu>
                    <li><a href="#">Home</a></li>
                    <li><a href="#">About</a></li>
                    <li><a href="#">Services</a></li>
                    <li><a href="#">Portfolio</a></li>
                    <li><a href="#">Contact</a></li>
                </menu>
            </nav>
        </div>
        <!-- end header area -->

        <div id="container-1">
        <!-- landing page -->
            <div class="hero">
            <div class="page">
                <h1>I'm <span style="color:#fff;font-weight:600">Trisha</span>, a web designer &amp; developer from Illinois.</h1>
                <p>I believe every single detail matters. My goal is to perfect the web one pixel at a time, and provide solutions through thoughtful ideas that translate into designs that last.</p>
            <div class="next">
                <a href="#">Let's Go.</a>
            </div>
            </div>
        </div>


        <!-- end landing page -->
        <!-- about page -->
            <div class="about">
            <div class="page">
            <h1>About Me</h1>
            <p>About Me Goes here.</p>
            </div>
        </div></div>


    </body>
</html>

CSS

    * {
  margin: 0;
  padding: 0;
}
html, body {
    height: 100%;
-webkit-font-smoothing: antialiased;

}
body {
  background: url(images/home-bg.jpg) no-repeat center center fixed;
  background-size:cover;
    color: #fff;
    font: 300 1em "Open Sans", Helvetica, Arial, sans-serif;
    display: table;
    margin: 0 auto;
}

/*----- NAVIGATION -----*/
#logo {
    position: absolute;
    top: 20px;
    left: 20px;
}
.header {
    display: block;
    position: fixed;
    right: 0;
    left: 0;
    margin: 0;
    width: 100%;
    padding: 30px;
    background-color:transparent;
    box-sizing: border-box;
}
.header.scrolling {
    background: #ccc;
}
#logo img {
    width: 75%;
}
nav input {
  display: none;
}
nav label {
  position: absolute;
  top: 50px;
  right: 10px;
  top: 10px;
  z-index: 999;
  cursor: pointer;
}
nav label strong {
  padding: 0 10px;
  width: 40px;
  height: 40px;
  line-height: 40px!important;
  font: 600 1em "Open Sans", helvetica, sans-serif;
  color: #fff;
  text-transform: uppercase;
  position: absolute;
  right: 40px;
  top: 0px;
  transition: color 250ms ease;
  -webkit-transition: color 250ms ease;
}
nav label b {
  width: 40px;
  height: 40px;
  display: block;
  position: relative;
  right: 0;
  top: 0;
  cursor: pointer;
  transition: background 500ms ease;
  -webkit-transition: background 500ms ease;
}
nav label b i {
  display: block;
  background: #fff;
  width: 24px;
  height: 4px;
  position: absolute;
  left: 8px;
  top: 11px;

  transform-origin: 20px 4px;
  -webkit-transform-origin: 20px 4px;
  transition: transform 500ms ease, opacity 500ms ease;
  -webkit-transition: transform 500ms ease, opacity 500ms ease;
}
nav label b i:nth-child(2) {
  top: 18px;
}
nav label b i:nth-child(3) {
  transform-origin: 26px 4px;
  -webkit-transform-origin: 26px 4px;
  top: 25px;
}
nav menu {
  background: none;
  width: 150px;
  height: 50%;
  opacity: 0;
  transition: opacity 500ms ease;
  -webkit-transition: opacity 500ms ease;
  position: absolute;
  z-index: 998;
  top: 0;
  right: 0;
}
nav menu li {
  margin: 0;
  list-style: none;
  width: 100%;
}
nav menu li:first-child {
  margin-top: 60px;
}
nav menu li:first-child a {
  border-top: rgba(255, 255, 255, 0.2) 1px solid;
}
nav menu li a {
  padding: 20px;
  font: 100 1em "Open Sans", helvetica, sans-serif;
  text-transform: uppercase;
  text-decoration: none;
  text-align: right;
  color: #fff;
  display: block;
}
nav menu li a:hover {
  background: rgba(0, 0, 0, 0.1);
}
nav #nav:checked ~ label strong {
  color: rgba(255, 255, 255, 0.25);
}
nav #nav:checked ~ label b {

}
nav #nav:checked ~ label b i:nth-child(1) {
  transform: rotate(45deg) translate(6px, 11px);
  -webkit-transform: rotate(45deg) translate(6px, 11px);
  transition: all 500ms ease;
  -webkit-transition: all 500ms ease;
}
nav #nav:checked ~ label b i:nth-child(2) {
  opacity: 0;
}
nav #nav:checked ~ label b i:nth-child(3) {
  transform: rotate(-45deg) translate(10px, -14px);
  -webkit-transform: rotate(-45deg) translate(10px, -14px);
    transition: all 500ms ease;
  -webkit-transition: all 500ms ease;
}
nav #nav:checked ~ menu {
  opacity: 1;
}

/*------------------------*/

#container-1 {
    width: 100%;
    height: 100%;
    display: table-cell;
    vertical-align: baseline;
}
.hero {
  margin: auto;
  height: 100%;
  width: 65%;
  padding: 20% 0 0;
}
.hero h1 {
    font-size: 3em;
    font-weight: 300;
    text-align: center;
    color: #ddd;
}
.hero p {
    color: #ddd;
    font-size: 1.5em;
    font-weight: 300;
    text-align: center;
    margin-top: 20px;
    padding-top: 20px;
    border-top: 1px solid rgba(255,255,255,0.2);
}
.next {
    margin: 80px auto;
    width: 150px;
    height: auto;
    border: 2px solid rgba(255,255,255,0.2);
        transition: background 500ms ease;
    -webkit-transition: background 500ms ease;
    text-align: center;
}
.next:hover {
    background: rgba(255,255,255,0.5);
    border: 2px solid rgba(255,255,255,0.0);
    transition: background 500ms ease;
    -webkit-transition: background 500ms ease;
}
.next a {
    text-align: center;
    line-height: 3em;
    text-transform: uppercase;
    letter-spacing: 0.15em;
    color: #fff;
    text-decoration: none;
}
.about {
    background: #fff;   
    width: 100%;
    height: 100%;
}
.about h1 {
    font-size: 3em;
    font-weight: 300;
    text-align: left;
    color: #777;
}
.about p {
    color: #777;
    font-size: 1.5em;
    font-weight: 300;
    text-align: left;
    margin-top: 20px;
    padding: 20px;
    border-top: 1px solid rgba(255,255,255,0.2);
}

/*----- MEDIA QUERIES -----*/
@media only screen and (max-width: 480px) {
    .hero {
        width: 80%;
        margin-top: 20%;
    }
}
@media only screen and (max-width: 768px) {
    .hero {
        width: 80%;
    }
}

@JayBlanchard 我的意思是我看了很多脚本,试图让它们工作...但我不想回去找它们了。例如,这个:https://dev59.com/w17Va4cB1Zd3GeqPO_tM,我用 .header 替换了 #menu,但什么也没发生。 - Trisha
啊,所以你在寻找一个脚本或插件?如果是的话,那很可能会导致你的问题被关闭。 - Jay Blanchard
因为这是 SO 的触发项之一 - “要求我们推荐或寻找工具、库或喜爱的离线资源的问题在 Stack Overflow 上是不适合的,因为它们往往会吸引带有个人观点的答案和垃圾邮件。相反,请描述问题以及已经采取的解决方法。” - Jay Blanchard
这里有一个示例,展示如何在特定的滚动位置为元素添加类。显然,你可以将其从颜色调整为透明度或其他任何东西。此外,在你的代码中,应该将标题设置为固定位置,而不是标题内部的元素。示例链接:http://jsfiddle.net/4xSe4/ - Justin
1
谢谢Jay,我会记住这个方法以备下次使用。我的初衷并不是只是要求一个脚本,而是请求帮助。@Justin,我已经将此添加到我的代码中,但仍然无法正常工作。http://upbeatdesigns.net/ud/ 上有新的代码。 - Trisha
显示剩余6条评论
3个回答

13

这里是一个示例代码片段,演示如何在特定滚动位置向元素添加类(一种基本方法)。显然,您可以将其从颜色调整为不透明度或任何其他内容。请注意,此操作需要jQuery

但是,你不能让它起作用是因为你的CSS设置方式不对。你把内容放在了头部内,并使用fixed定位,而不是将.header本身设为position:fixed的元素。内部内容不应该是固定的。


请再次检查代码,第一次更改后它没有通过ftp上传,但现在已经修复了。 - Trisha
哇.. 真不敢相信我没想到那个。今天真是漫长的一天。感谢你的帮助,它起作用了.. 我已经将其他类添加到脚本中,这样我就可以更改不仅仅是 .header 类! - Trisha
感谢为我工作。 - Hani Abu khurma

2

我认为对于你的要求,https://github.com/Prinzhorn/skrollr 是一个不错的选择。它是一个易于实现的库,依赖于css过渡效果,不需要jquery。

它可以实现颜色、视差、动画等功能。


1
将滚动条绑定到窗口是一个不好的想法。
如果您想在网站的每个部分触发更改,请查看jQuery Waypoints。

http://imakewebthings.com/jquery-waypoints/

    $('.section').waypoint({handler: function() {
            $('.menu').css('background', '#ff3366');
        },
        offset: '50%'
    });

希望这能有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接