javascript - Scroll when linked to id on same page -



javascript - Scroll when linked to id on same page -

note: i'm new webdev , i'm building site bootstrap

i'm building 1 page site , navbar @ top directs user relevant id. problem i'm having want scroll id when clicked (see http://www.blastprocessor.co.uk/). want set class active when user scrolls past specific id.

i have no thought start i'll post html code here.

<nav class="navbar navbar-default" data-spy="affix" data-offset-top="34"> <div class="container"> <div class="navbar-collapse collapse navbar-responsive-collapse" id="main-menu"> <ul class="nav navbar-nav"> <li class="active"><a href="#whatissm">what are</a></li> <li class=""><a href="#whyusesm">why us</a></li> <li class=""><a href="#whatdoessmeoffer">what offer</a></li> <li class=""><a href="#contactus">contact us</a></li> </ul> <ul class="nav navbar-nav pull-right"> <li><a href="#">right link</a></li> </ul> </div> </div> </nav>

you can utilize next function create scroll animation. regarding adding class 'active' past id can follow reply of @ahb. need added var reduce in function help cut down 50px actuall offset.

$(function() { $('a[href*=#]:not([href=#])').click(function() { var cut down = 50; if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) { var target = $(this.hash); target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); if (target.length) { $('html,body').animate({ scrolltop: target.offset().top - cut down }, 1000); homecoming false; } } }); });

original source

demo

javascript jquery html css twitter-bootstrap

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -