Java脚本滚动突出显示不起作用

时间:2016-08-05 00:10:29

标签: javascript jquery html css navigation

所以我正在尝试构建我的网站,并且我有一个非常好的粘性标题,我试图得到一种滚动间谍效果,不使用bootstrap。 如果有意义的话,我希望查看导航的当前部分用活动类突出显示。

问题是,我有0个jquery知识,因此我从youtube视频中复制了一些代码,但我无法有效地实现它。有人可以更正此查询代码,使其在导航栏中突出显示当前正在查看的部分,并在用户滚动到另一部分时更新,因此该部分在导航栏中突出显示?

Ps - 我再说一遍,我不想与bootstrap xD

有任何关系

这是我的代码笔链接http://codepen.io/anon/pen/bZmRZN

Any help would be amazing!

1 个答案:

答案 0 :(得分:0)

你忘了在$之前调用(function() {来调用jQuery。虽然它在片段上没有显示但是我在我的服务器上试了它并且工作正常〜在你的服务器上尝试它:)

$(function() {
	var navLinks = $('nav ul li a'),
			navH = $('nav').height(),
			section = $('section'),
			documentEl = $(document);

	documentEl.on('scroll', function() {
			var currentScrollPos = documentEl.scrollTop();

			section.each(function() {
					var self = $(this);
					if ( self.offset().top < (currentScrollPos + navH) && (currentScrollPos + navH) < (self.offset().top + self.outerHeight()) ) {
							var targetClass = '.' + self.attr('class') + '-marker';
							navLinks.removeClass('active');
							$(targetClass).addClass('active');
					}
			});

	});
})();
@charset "utf-8";
* {
  /*reset*/
  background: none repeat scroll 0 0 transparent;
  border: medium none;
  border-spacing: 0;
  font-size: 16px;
  font-weight: normal;
  line-height: 1.42rem;
  list-style: none outside none;
  margin: 0;
  padding: 0;
  text-align: left;
  text-decoration: none;
  text-indent: 0;
  box-sizing: border-box;
}

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
	margin: 0;
	padding: 0;
	border: 0;
	font-size: 100%;
	font: inherit;
	vertical-align: baseline;
}

/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
	display: block;
}
body {
	line-height: 1;
}
ol, ul {
	list-style: none;
}
blockquote, q {
	quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
	content: '';
	content: none;
}
table {
	border-collapse: collapse;
	border-spacing: 0;
}
button{
  -webkit-appearance: none !important;
}
a{
  -webkit-appearance: none;
}
/*End Reset*/

/*Fonts*/
@font-face {
  font-family: 'bebas_neuebold';
  src:  url('../fonts/bebasneue_bold-webfont.woff2') format('woff2'),
        url('../fonts/bebasneue_bold-webfont.woff') format('woff');
}
@font-face {
  font-family: 'bebas_neuebook';
  src:  url('../fonts/bebasneue_book-webfont.woff2') format('woff2'),
        url('../fonts/bebasneue_book-webfont.woff') format('woff');
}
@font-face {
  font-family: 'bebas_neuelight';
  src:  url('../fonts/bebasneue_light-webfont.woff2') format('woff2'),
        url('../fonts/bebasneue_light-webfont.woff') format('woff');
}
@font-face {
  font-family: 'bebas_neue_regularregular';
  src:  url('../fonts/bebasneue_regular-webfont.woff2') format('woff2'),
        url('../fonts/bebasneue_regular-webfont.woff') format('woff');
}
@font-face {
  font-family: 'bebas_neuethin';
  src:  url('../fonts/bebasneue_thin-webfont.woff2') format('woff2'),
        url('../fonts/bebasneue_thin-webfont.woff') format('woff');
}
@font-face {
  font-family: 'bebas_neueregular';
  src:  url('../fonts/bebasneue-webfont.woff2') format('woff2'),
        url('../fonts/bebasneue-webfont.woff') format('woff');
}
.bebas-bold{
	font-family: bebas_neuebold !important;
  letter-spacing: 0.05em;
}
.bebas-book{
	font-family: bebas_neuebook !important;
}
.bebas-light{
	font-family: bebas_neuelight !important;
  letter-spacing: 0.05em;
}
.bebas-thin{
  font-family: bebas_neuethin !important;
  letter-spacing: 0.05em;
}
.bebas-reg{
	font-family: bebas_neue_regularregular !important;
  letter-spacing: 0.05em;
}
/*End fonts*/
body {
  font-family: "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif;
  font-weight: 300;
  background-color: #505050;
  padding: 0px;
  margin: 0px;
}

/*Header*/
#navwrapper{
  width: 100%;
  position: fixed;
  z-index: 1;
  top: 0;
  left: 0;
  right: 0;
}
li img {
	height:95px;
	width:95px;
}

ul{
  display: table;
  margin: 0 auto;
}

ul.topnav {
  padding: 0;
  overflow: hidden;
}

.toggleNavButton {
  transition-duration: 0.5s;
  width: 40px;
  height: 2px;
  background-color: black;
  position: absolute;
  /*left: 50%;*/
  top: 25px;
  margin-left: 20px;
  border-radius: 2px;
}
.toggleNavButton:before,
.toggleNavButton:after {
  border-radius: 2px;
  transition-duration: 0.5s;
  content: "";
  position: absolute;
  top: 10px;
  left: 0;
  background-color: black;
  width: 40px;
  height: 2px;
}
.toggleNavButton:after {
  top: 20px;
}
.toggleNavButton.active {
  width: 0;
}
.toggleNavButton.active:after {
  top: 10px;
  transform: rotate(45deg);
  -webkit-transform: rotate(45deg);
}
.toggleNavButton.active:before {
  transform: rotate(-45deg);
  -webkit-transform: rotate(-45deg);
}
/* Float the list items side by side */
nav ul.topnav li {
  float: left;
}

.padnav{
  padding: 0px 35px 0px 35px;
  margin-top:35px;
}

/* Style the links inside the list items */
nav ul li a {
  display: block;
  color: #1b1b1b;
  text-align: center;
  text-decoration: none;
  transition: 0.5s;
  font-size: 1.5em;
  padding: 5px;
}

/* Change background color of links on hover */
nav ul li.padnav a:hover {
  -webkit-transition: 0.2s; /* Safari */
  transition: 0.2s;
  border-bottom: 5px solid #2098D1;

}

.active{
  -webkit-transition: 0.1s; /* Safari */
  transition: 0.1s;;
  border-bottom: 5px solid #2098D1;
}

.hide{
  display: none;
  width: 0px;
  height: 0px;
}

nav{
  position: fixed;
}

/* Hide the list item that contains the link that should open and close the topnav on small screens */
ul.topnav li.icon {
  display: none;
}

/* When the screen is less than 680 (changed to 770 to accomodate) pixels wide, hide all list items, except for the first one ("Home"). Show the list item that contains the link to open and close the topnav (li.icon) */
@media screen and (max-width:767px) {
  ul.topnav li:not(:first-child) {
    display: none;
  }

  ul.topnav li:nth-child(1){
    display: inline-block !important;
    width: auto !important;
    height: auto !important;
  }

  ul.topnav li.icon {
    float: right;
    display: inline-block;
    width: 60px;
  }
  ul.topnav li{
    padding: 0px !important;
  }
  ul.topnav a{
    margin-top: 25px !important;
    margin-right: 25px !important;
    padding: 0px !important;
  }

  li img {
    display: none;
  }

  .topnav{
    width: 100% !important;
  }

  #navhit{
    height: 50px;
    width: 80px;
    float: right;
    display: inline-block;
    margin-top: -8px;
  }

  li.padnav{
    padding: 0px !important;
    margin-top: 0px !important;
  }

  ul.topnav li.padnav a:hover {
    border: none !important;
  }

  #navwrapper{
    padding-bottom: 20px;
  }
}
/* The "responsive" class is added to the topnav with JavaScript when the user clicks on the icon. This class makes the topnav look good on small screens */
@media screen and (max-width:767px) {
  ul.topnav.responsive {position: relative;}
  ul.topnav.responsive li.icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  ul.topnav.responsive li {
    float: none;
    display: inline;
  }
  ul.topnav.responsive li a {
    display: block;
    text-align: left;
  }
}
/*sections*/
section {
  width: 100%;
  padding: 0 7%;
  display: table;
  margin: 0;
  max-width: none;
  height: 100vh;
}

section.landing{
  background-color: #f39c12;
}

section.about{
  background-color: #9b59b6;
}

section.project{
  background-color: #2ecc71;
}

section.blog{
  background-color: #95a5a6;
}

section.contact{
  background-color: #f1c40f;
}

.landing{
  height: 95vh;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="navwrapper">
	<nav class="navbar bebas-reg container">
		<ul class="topnav" id="myTopnav">
			<li class="padnav"><a href="about.html" class="about-marker active">About</a></li>
			<li class="padnav"><a href="projects.html" class="proj-marker">Projects</a></li>
			<li class="logo"><img src="img/logo-black.png"></img></li>
			<li class="padnav"><a href="contact" class="contact-marker">Contact</a></li>
			<li class="padnav"><a href="blog" class="blog-marker">Blog</a></li>
		</ul>
	</nav>
</div>


<section class="landing">
	<h1> landing </h1>
</section>

<section class="about">
	<h1> about </h1>
</section>

<section class="proj">
	<h1> proj </h1>
</section>

<section class="contact">
	<h1> contact </h1>
</section>

<section class="blog">
	<h1> blog </h1>
</section>