Advertisement
Guest User

Untitled

a guest
May 25th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. <nav>
  2. <span id="section_1">
  3. Secção 1
  4. </span>
  5. <span id="section_2">
  6. Secção 2
  7. </span>
  8. <span id="section_3">
  9. Secção 3
  10. </span>
  11. <span id="section_4">
  12. Secção 4
  13. </span>
  14. </nav>
  15.  
  16. <div class="section" data-section="section_1">
  17.  
  18. </div>
  19. <div class="section" data-section="section_2">
  20.  
  21. </div>
  22. <div class="section" data-section="section_3">
  23.  
  24. </div>
  25. <div class="section" data-section="section_4">
  26.  
  27. </div>
  28.  
  29. $('span').on('click', function() {
  30. $('span').removeClass('active');
  31. $(this).addClass('active');
  32. var spanId = $(this).prop('id');
  33. $("html, body").animate({
  34. scrollTop: $('div[data-section="' +spanId+ '"]').offset().top
  35. });
  36. });
  37.  
  38. var alturas = [];
  39. var countSections = $('.section').length;
  40.  
  41. for(var i = 0; i < countSections; i++) {
  42. alturas.push($('.section').eq(i).offset().top);
  43. }
  44.  
  45. console.log(alturas);
  46.  
  47. $(window).on('scroll', function() {
  48.  
  49. countAlturas = alturas.length;
  50.  
  51. scrollTopPx = $(window).scrollTop();
  52.  
  53. for(var i = 0; i < countAlturas; i++) {
  54. if(scrollTopPx >= (alturas[i])) {
  55. $('span').removeClass('active');
  56. $('span').eq(i).addClass('active');
  57. }
  58. }
  59.  
  60. });
  61.  
  62. (function ($) {
  63. //Add current view's highlighting to the navigation
  64.  
  65. /** helper for highlighting */
  66. function highlightNav(navLinks,id)
  67. {
  68. navLinks.filter('[href="/#'+id+'"]').addClass("active");
  69. }
  70.  
  71. $(window).scroll(function() {
  72. //console.log("They see me scrollin, they hatin");
  73.  
  74. //clear highlighting
  75. var navLinks = $('.site-navigation a');
  76. navLinks.removeClass("active");
  77.  
  78. //calc current viewport
  79. var viewTop = $(window).scrollTop();
  80. var viewBottom = viewTop + $(window).height();
  81.  
  82. //for all h1 and h2 elements, check if they are visible
  83. //performance tweak: stop each() after the first element is found to be behind view
  84. var previous = "";
  85. var foundOne = false;
  86. var fallback = "";
  87. $('h1, h2').each(function(i,e) {
  88. //get element position;
  89. var eTop = $(e).offset().top;
  90. var eBottom = eTop + $(e).height();
  91. var id=e.id;
  92. id = id.replace("_title", "");
  93.  
  94. if (eTop >= viewTop) {
  95. //if we are passed the view and no heading was highlighted yet, store previous one as fallback
  96. if (! foundOne) {
  97. fallback=previous;
  98. }
  99. if (eBottom <= viewBottom) {
  100. highlightNav(navLinks, id);
  101. foundOne = true;
  102. } else {
  103. return false; //break the each(), the rest is below
  104. }
  105. }
  106. previous=id;
  107. });
  108. //no h1/h2 is in the viewport, so highlight the last one above
  109. if (! foundOne) {
  110. highlightNav(navLinks, fallback);
  111. }
  112. });
  113. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement