滚动

时间:2016-01-14 13:05:47

标签: javascript jquery html css

我需要帮助。我有一个带按钮的导航栏。我希望他们在点击时以及滚动条点击右侧锚点时更改颜色(这是一个单页网站)。但是脚本不起作用,只有第一个按钮在开始时变为白色但滚动时......没有任何反应。请帮助,我是一辆笨卡车: - )



// Cache selectors
var lastId,
  topMenu = $("#nav"),
  topMenuHeight = topMenu.outerHeight() + 15,
  // All list items
  menuItems = topMenu.find("a"),
  // Anchors corresponding to menu items
  scrollItems = menuItems.map(function() {
    var item = $($(this).attr("href"));
    if (item.length) {
      return item;
    }
  });

// Bind click handler to menu items
// so we can get a fancy scroll animation
menuItems.click(function(e) {
  var href = $(this).attr("href"),
    offsetTop = href === "#" ? 0 : $(href).offset().top - topMenuHeight + 1;
  $('html, body').stop().animate({
    scrollTop: offsetTop
  }, 300);
  e.preventDefault();
});

// Bind to scroll
$(window).scroll(function() {
  // Get container scroll position
  var fromTop = $(this).scrollTop() + topMenuHeight;

  // Get id of current scroll item
  var cur = scrollItems.map(function() {
    if ($(this).offset().top < fromTop)
      return this;
  });
  // Get the id of the current element
  cur = cur[cur.length - 1];
  var id = cur && cur.length ? cur[0].id : "";

  if (lastId !== id) {
    lastId = id;
    // Set/remove active class
    menuItems
      .parent().removeClass("active")
      .end().filter("[href=#" + id + "]").parent().addClass("active");
  }
});
&#13;
#nav {
  width: 100%;
  height: 20px;
  background-image: url(photos/BG_header2.png);
  background-repeat: repeat-x;
  display: block;
  margin-top: 25px;
  margin-left: 390px;
  line-height: 17px;
  white-space: nowrap;
  z-index: 1;
}
#nav2 {
  padding-left: 20px;
}
#nav li {
  display: inline-block;
  margin-top: 0px;
  margin-right: 4px;
  margin-left: 4px;
  margin-bottom: 0px;
  text-indent: 3px;
}
#nav a {
  color: #006EBE;
  width: auto;
  text-decoration: none;
  font-weight: bold;
  font-size: 13px;
  font-family: Arial, Helvetica, sans-serif;
  text-transform: uppercase;
  letter-spacing: 1px;
  vertical-align: middle;
  -webkit-transition: .5s all ease-out;
  -moz-transition: .5s all ease-out;
  transition: .5s all ease-out;
}
#nav a:hover {
  color: #fff;
}
#nav li.active a {
  color: #fff;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<ul id="nav">
  <ul id="nav2">
    <li class="active"><a href="#">Accueil</a>
    </li>
    <li class="trait"></li>
    <li><a href="#anchor1">L'entreprise</a>
    </li>
    <li class="trait"></li>
    <li><a href="#anchor2">Services</a>
    </li>
    <li class="trait"></li>
    <li><a href="#anchor3">Jobs</a>
    </li>
    <li class="trait"></li>
    <li><a href="#anchor4">Contact</a>
    </li>
  </ul>
</ul>
</header>
<a id="anchor1"></a>
<a id="anchor2"></a>
<a id="anchor3"></a>
<a id="anchor4"></a>
&#13;
&#13;
&#13;

0 个答案:

没有答案