如何在导航栏上更改突出显示颜色,将单个网页向下滚动到不同的部分

时间:2017-07-02 15:21:43

标签: html css

我正在尝试构建包含不同部分(主页,简历,联系人等)的练习组合/简历网页。我的固定导航栏已经突出显示您所在网页的哪个部分,但我想更改突出显示颜色。我已经尝试了很多不同的解决方案来解决这个问题,但由于某些原因,它们都没有起作用。

目前我的代码:

<!DOCTYPE html>
<html>
<head>
  <title>Webpage</title>
  <meta charset="utf-8">
  <meta name="description" content="A portfolio of my work.">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <html lang="en-US">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <link href="https://fonts.googleapis.com/css?family=Oswald" rel="stylesheet" type="text/css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <style>
  body {
    position: relative;
  }

  nav{
    background-color: black;
    color: red;
  }

  ul{
    height: 70px;
  }

  li>a{
    text-align: center;
    height: 71px;
    width: 150px;
    color: white;
    font-family: bookman;
    font-size: 20px;
  }

  }

  .navbar-inverse {
    background-color: black !important;
  }

  .nav > li > a:hover,

  .nav > li > a:focus {
    background: #B22222;
    color: white;
  }

  #home {padding-top:80px;height:675px;color: #fff; background-color: #00bcd4;}
  #about {padding-top:80px;height:675px;color: #fff; background-color: #673ab7;}
  #portfolio {padding-top:80px;height:675px;color: #fff; background-color: #ff9800;}
  #resume {padding-top:80px;height:675px;color: #fff; background-color: #00bcd4;}
  #contact {padding-top:80px;height:675px;color: #fff; background-color: #00bcd4;}
  </style>
</head>
<body data-spy="scroll" data-target=".navbar" data-offset="50">

<nav class="navbar navbar-inverse navbar-fixed-top">
  <div class="container-fluid">
    <p class="navbar-text" style="color: white; padding-top: 7px; font-family: Oswald; font-size: 22px;">Name</p>
    <div>
      <div class="collapse navbar-collapse" id="myNavbar">
        <ul class="nav navbar-nav navbar-right">
          <li><a href="#home" class="active">Home</a></li>
          <li><a href="#about">About</a></li>
          <li><a href="#portfolio">Portfolio</a></li>
          <li><a href="#resume">Resume</a></li>
          <li><a href="#contact">Contact</a></li>
        </ul>
      </div>
    </div>
  </div>
</nav>

<div id="home" class="container-fluid">
</div>
</div>
<div id="about" class="container-fluid">
  <h1>Section 2</h1>
  <p><a href="#">My resume can be found here</a></p>
  <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
  <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
</div>
<div id="portfolio" class="container-fluid">
  <h1>Section 3</h1>
  <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
  <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
</div>
<div id="resume" class="container-fluid">
  <h1>Section 4 Submenu 1</h1>
  <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
  <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
</div>
<div id="contact" class="container-fluid">
  <h1>Section 4 Submenu 2</h1>
  <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
  <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
</div>

</body>
</html>

3 个答案:

答案 0 :(得分:1)

参考此jsfiddle

auth <mykey>

简单示例试试这个

&#13;
&#13;
jQuery(document).ready(function($) {
  $(window).scroll(function() {
    var scrollPos = $(window).scrollTop(),
        navbar = $('.navbar-default');

    if (scrollPos > 20) {
      navbar.addClass('change-color');
    } else {
      navbar.removeClass('change-color');
    }
  });
});
&#13;
$(document).ready(function () {
    $(document).on("scroll", onScroll);
    
    //smoothscroll
    $('a[href^="#"]').on('click', function (e) {
        e.preventDefault();
        $(document).off("scroll");
        
        $('a').each(function () {
            $(this).removeClass('active');
        })
        $(this).addClass('active');
      
        var target = this.hash,
            menu = target;
        $target = $(target);
        $('html, body').stop().animate({
            'scrollTop': $target.offset().top+2
        }, 500, 'swing', function () {
            window.location.hash = target;
            $(document).on("scroll", onScroll);
        });
    });
});

function onScroll(event){
    var scrollPos = $(document).scrollTop();
    $('#menu-center a').each(function () {
        var currLink = $(this);
        var refElement = $(currLink.attr("href"));
        if (refElement.position().top <= scrollPos && refElement.position().top + refElement.height() > scrollPos) {
            $('#menu-center ul li a').removeClass("active");
            currLink.addClass("active");
        }
        else{
            currLink.removeClass("active");
        }
    });
}
&#13;
body, html {
    margin: 0;
    padding: 0;
    height: 100%;
    width: 100%;
}
.menu {
    width: 100%;
    height: 75px;
    background-color: rgba(0, 0, 0, 1);
    position: fixed;
    background-color:rgba(4, 180, 49, 0.6);
    -webkit-transition: all 0.3s ease;
    -moz-transition: all 0.3s ease;
    -o-transition: all 0.3s ease;
    transition: all 0.3s ease;
}
.light-menu {
    width: 100%;
    height: 75px;
    background-color: rgba(255, 255, 255, 1);
    position: fixed;
    background-color:rgba(4, 180, 49, 0.6);
    -webkit-transition: all 0.3s ease;
    -moz-transition: all 0.3s ease;
    -o-transition: all 0.3s ease;
    transition: all 0.3s ease;
}
#menu-center {
    width: 980px;
    height: 75px;
    margin: 0 auto;
}
#menu-center ul {
    margin: 15px 0 0 0;
}
#menu-center ul li {
    list-style: none;
    margin: 0 30px 0 0;
    display: inline;
}
.active {
    font-family:'Droid Sans', serif;
    font-size: 14px;
    color: #fff;
    text-decoration: none;
    line-height: 50px;
}
a {
    font-family:'Droid Sans', serif;
    font-size: 14px;
    color: black;
    text-decoration: none;
    line-height: 50px;
}
#home {
    background-color: grey;
    height: 100%;
    width: 100%;
    overflow: hidden;
    background-image: url(images/home-bg2.png);
}
#resume {
    background-image: url(images/portfolio-bg.png);
    height: 100%;
    width: 100%;
}
#about {
    background-color: blue;
    height: 100%;
    width: 100%;
}
#contact {
    background-color: red;
    height: 100%;
    width: 100%;
}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您没有提供有用的资源,我可以根据这些资源确切地告诉您该怎么做。但基于此模板,您需要更改以下CSS类的属性

.navbar-default .nav>li.active>a, .navbar-default .nav>li.active>a:focus {     
    color: #fdcc52!important;
    background-color: transparent;
}

答案 2 :(得分:0)

CSS标记的末尾添加此style。将颜色red更改为您想要的颜色。

.navbar-inverse .navbar-nav>.active>a,
.navbar-inverse .navbar-nav>.active>a:focus,
.navbar-inverse .navbar-nav>.active>a:hover {
    background-color: red;
}