Skip to content

Instantly share code, notes, and snippets.

@omurphy27
Created February 25, 2013 03:48
Show Gist options
  • Save omurphy27/5027363 to your computer and use it in GitHub Desktop.
Save omurphy27/5027363 to your computer and use it in GitHub Desktop.
JQUERY - Simple smooth scrolling effect
// Simple smooth-scrolling effect in Jquery
$('#links li').click(function(){
var liIndex = $(this).index();
var contentPosTop = $('.content').eq(liIndex).position().top;
$('html, body').stop().animate({
scrollTop: contentPosTop // can add subtract numbers here for the exact position I want
}, 1500);
});
// http://jsbin.com/inixoz/2/edit for more details
// here's some of the mark up
<div id="links">
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
</ul>
</div>
<div class="content">
<h2>A</h2>
Lorem ipsum dolor sit amet,
</div>
<div class="content">
<h2>B</h2>
Lorem ipsum dolor sit amet,
</div>
// usw
// basically it matches the order of the li with the order of the div, and that's how it works
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment