简体   繁体   中英

Why doesn't this JS work? (window.scrollTop)

The class 'stuck-sm' is added, but 'stuck-md' is not.

if ($(window).scrollTop() >= 285) {
  $('.something').addClass('stuck-sm');
} else if ($(window).scrollTop() >= 430) {
  $('.something').addClass('stuck-md');
} else {
  $('.something').removeClass('stuck-sm','stuck-md');
}

else if is reachable only if the value is less than 285 which means second else if block won't get execute. Below is the correct solution.

if ($(window).scrollTop() >= 430) {
   $('.something').addClass('stuck-md');
} else if ($(window).scrollTop() >= 285) {
   $('.something').addClass('stuck-sm');
} else {
   $('.something').removeClass('stuck-sm','stuck-md');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM