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.
- var adistance = $('article.about-me').offset().top -90,
-
- $window = $(window);
- $window.scroll(function() {
- if ( $window.scrollTop() >= adistance ) {
- $("li.active").removeClass("active");
- $('li[href$="about-me"]').addClass("active");
- }
- });
- var edistance = $('article.education').offset().top -90,
-
- $window = $(window);
- $window.scroll(function() {
- if ( $window.scrollTop() >= edistance ) {
- $("li.active").removeClass("active");
- $('li[href$="education"]').addClass("active");
- }
- });
- var exdistance = $('article.experience').offset().top -90,
-
- $window = $(window);
- $window.scroll(function() {
- if ( $window.scrollTop() >= exdistance ) {
- $("li.active").removeClass("active");
- $('li[href$="experience"]').addClass("active");
- }
- });
- var pdistance = $('article.portfolio').offset().top -90,
-
- $window = $(window);
- $window.scroll(function() {
- if ( $window.scrollTop() >= pdistance ) {
- $("li.active").removeClass("active");
- $('li[href$="portfolio"]').addClass("active");
- }
- });
I am using the function on
chibuezeokechukw.comto switch the active classes automatically.