Advertisement
ten80snowboarder

jQuery goToByScroll()

Jan 26th, 2012
2,127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. JAVASCRIPT
  2. ========================================================================
  3. <script>
  4. function goToByScroll(id){
  5. $('html,body').animate({scrollTop: $("#"+id).offset().top},'slow');
  6. }
  7. </script>
  8.  
  9. HTML - Must be inside the BODY, which is correctly nested inside the HTML tag
  10. ========================================================================
  11. <ul>
  12. <li><a href="javascript:void(0)" onClick="goToByScroll('1')">Go to anchor 1</a></li>
  13. <li><a href="javascript:void(0)" onClick="goToByScroll('2')">Go to anchor 2</a></li>
  14. <li><a href="javascript:void(0)" onClick="goToByScroll('3')">Go to anchor 3</a></li>
  15. <li><a href="javascript:void(0)" onClick="goToByScroll('4')">Go to anchor 4</a></li>
  16. </ul>
  17. <div>
  18. <div id="1">
  19. <h1>1</h1>
  20. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  21. </div>
  22. <div id="2">
  23. <h1>2</h1>
  24. Proin orci erat, gravida vitae auctor sed, fringilla sit amet sem.
  25. </div>
  26. <div id="3">
  27. <h1>3</h1>
  28. Etiam scelerisque, ligula ac scelerisque egestas, sem arcu commodo ligula, in imperdiet arcu ipsum nec magna.
  29. </div>
  30. <div id="4">
  31. <h1>4</h1>
  32. Proin purus tellus, dictum at volutpat tempor, ultrices ut arcu.
  33. </div>
  34. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement