Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. (function () {
  2. var speed = 500;
  3. var moving_frequency = 15; // Affects performance !
  4. var links = document.getElementsByTagName('a');
  5. var href;
  6. for (var i = 0; i < links.length; i++) {
  7. href = (links[i].attributes.href === undefined) ? null : links[i].attributes.href.nodeValue.toString();
  8. if (href !== null && href.length > 1 && href.substr(0, 1) == '#') {
  9. links[i].onclick = function () {
  10. var element;
  11. var href = this.attributes.href.nodeValue.toString();
  12. if (element = document.getElementById(href.substr(1))) {
  13. var hop_count = speed / moving_frequency
  14. var getScrollTopDocumentAtBegin = getScrollTopDocument();
  15. var gap = (getScrollTopElement(element) - getScrollTopDocumentAtBegin) / hop_count;
  16.  
  17. for (var i = 1; i <= hop_count; i++) {
  18. (function () {
  19. var hop_top_position = gap * i;
  20. setTimeout(function () {
  21. window.scrollTo(0, hop_top_position + getScrollTopDocumentAtBegin);
  22. }, moving_frequency * i);
  23. })();
  24. }
  25. }
  26.  
  27. return false;
  28. };
  29. }
  30. }
  31.  
  32. var getScrollTopElement = function (e) {
  33. var top = 0;
  34.  
  35. while (e.offsetParent != undefined && e.offsetParent != null) {
  36. top += e.offsetTop + (e.clientTop != null ? e.clientTop : 0);
  37. e = e.offsetParent;
  38. }
  39.  
  40. return top;
  41. };
  42.  
  43. var getScrollTopDocument = function () {
  44. return document.documentElement.scrollTop + document.body.scrollTop;
  45. };
  46. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement