jquery - Triggering navigation while scrolling -
i'm trying modify navigation links depending on section scrolled on user.
i have following html
<div class="navigation"> <a href="#resume">resume</a> <a href="#design">design</a> <a href="#" class="contact">contact</a> </div> and following structure
<section id="resume">my content</section> <section id="design">.... in order achieve effect i'm using tutorial presented here works smooth scrolling doesn't add class navigation links.
would able understand why?
thanks lot, julien
you can use this:
css
.navigation { position:fixed; top:0; left:0; width:100%; background-color:#88f; } .navigation { margin:2em; color:white; text-decoration:none; } .navigation a.active {text-decoration:underline; color:red;} #resume { margin-top:50px; } section { margin:2em; height:500px; background-color:#eee; } jquery
$(function(){ var links = $('.navigation>a'); //.not('.contact'); var sections = $('section'); links.click(function(e){ e.preventdefault(); var p = $(this.getattribute('href')).position().top; $("html, body").stop().animate( { scrolltop: (p-30)+'px' }, 500); }); $(window).on('scroll', function(e){ var i=0; sections.each(function(j){ var br = this.getboundingclientrect(); if (br.top<150) { // distance top of screen = j; } }); var scrollbottom = $(document).height() - $(window).height() - $(window).scrolltop(); if (scrollbottom < 40) { // highlight last section = sections.length-1; } links.removeclass('active'); var active = sections.eq(i).attr('id'); links.filter('[href="#'+active+'"]').addclass('active'); }).trigger('scroll'); }); jsfiddle: http://jsfiddle.net/grcjc/3/ http://jsfiddle.net/grcjc/3/show
Comments
Post a Comment