Advertisement
Guest User

Untitled

a guest
Apr 25th, 2015
886
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(function() {
  2.    
  3.     var highlighted = null,         // Keep track of previously selected
  4.         $menuItems  = $('#menu a'); // Grab all menu items once
  5.  
  6.     // Which items at which points
  7.     var breakpoints = {
  8.         '#menu-item-25': 590,
  9.         '#menu-item-26': 1380,
  10.         '#menu-item-22': 2545,
  11.         '#menu-item-23': 2969,
  12.         '#menu-item-24': 99999
  13.     };
  14.  
  15.     $(window).scroll(function() {
  16.  
  17.             var scroll = $(window).scrollTop(),
  18.             selected = '#menu-item-24';  // default
  19.  
  20.         // Loop through breakpoints to find the current one that should be selected
  21.         for (id in breakpoints) {
  22.             if (scroll <= breakpoints[id]) {
  23.                 selected = id;
  24.                 break;
  25.             }
  26.         }
  27.  
  28.         // If the right one is already highlighted, do nothing
  29.         if (highlighted == id) {
  30.             return;
  31.         }
  32.        
  33.         // Remove all
  34.         $menuItems.removeClass('blue');
  35.         // Add it to the one you need
  36.         $(id).addClass('blue');
  37.  
  38.     });
  39. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement