I've been trying to implement this particular example on my website, but I seem to be struggling with it.
Here are the lines that I added:
<script src="jQuery/jquery-2.1.1.min.js" type="text/javascript"> </script>
<script src="jQuery/navigation.js" type="text/javascript"> </script> <!--original script-->
I placed these lines between the header tags of my HTML file and made adjustments to my navigation bar as follows:
<nav class="floatingMenu" id="floatingMenu">
<ul id="#top-menu">
<li class="active"><a href="#">Top</a></li>
<li><a class="button" href="#one">one</a></li>
<li><a class="button" href="#two">two</a></li>
<li><a class="button" href="#three">three</a></li>
<li><a class="button" href="#four">four</a></li>
<li><a class="button" href="#five">five</a></li>
<li><a class="button" href="#six">six</a></li>
</ul>
</nav>
Furthermore, I have defined a CSS class specifying the style attributes for the menu elements.
.active {font-size:30px;}
Can someone identify where I may have gone wrong in this process?
Thank you!
Note: The following is the content of my jQuery/navigation.js:
// Cache selectors
var lastId,
topMenu = $("#floatingMenu"),
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");
}
});