How to combine Multiple functions that does the same thing.

How to combine Multiple functions that does the same thing.

I am trying to combine the scroll functions below, but to no avail. E.G I tried defining $window only once but it breaks. my code looks like below.

  1. var adistance = $('article.about-me').offset().top -90,
  2.  
  3.     $window = $(window);
  4.     $window.scroll(function() {
  5.     if ( $window.scrollTop() >= adistance ) {
  6.          $("li.active").removeClass("active");    
  7.          $('li[href$="about-me"]').addClass("active");
  8.     }
  9.    });
  10.  var edistance = $('article.education').offset().top -90,
  11.  
  12.     $window = $(window);
  13.     $window.scroll(function() {
  14.     if ( $window.scrollTop() >= edistance ) {
  15.          $("li.active").removeClass("active");    
  16.          $('li[href$="education"]').addClass("active");
  17.     }
  18.    });
  19.    var exdistance = $('article.experience').offset().top -90,
  20.  
  21.     $window = $(window);
  22.     $window.scroll(function() {
  23.     if ( $window.scrollTop() >= exdistance ) {
  24.          $("li.active").removeClass("active");    
  25.          $('li[href$="experience"]').addClass("active");
  26.     }
  27.    });
  28.   var pdistance = $('article.portfolio').offset().top -90,
  29.  
  30.     $window = $(window);
  31.     $window.scroll(function() {
  32.     if ( $window.scrollTop() >= pdistance ) {
  33.          $("li.active").removeClass("active");    
  34.          $('li[href$="portfolio"]').addClass("active");
  35.     }
  36.    });

I am using the function on chibuezeokechukw.comto switch the active classes automatically.