jquery - Change CSS based on scroll position -- Refactoring bad code -
i have written jquery function changes css value of nav menu item based on if reference section viewable in window.
$(window).scroll(function() { var scroll = $(window).scrolltop(); if (scroll <= 590) { $("#menu-item-25 a").addclass('blue'); $("#menu-item-26 a").removeclass('blue'); $("#menu-item-22 a").removeclass('blue'); $("#menu-item-23 a").removeclass('blue'); $("#menu-item-24 a").removeclass('blue'); } else if (scroll >= 591 && scroll <= 1380) { $("#menu-item-26 a").addclass('blue'); $("#menu-item-25 a").removeclass('blue'); $("#menu-item-22 a").removeclass('blue'); $("#menu-item-23 a").removeclass('blue'); $("#menu-item-24 a").removeclass('blue'); } else if (scroll >= 1381 && scroll <= 2545) { $("#menu-item-22 a").addclass('blue'); $("#menu-item-25 a").removeclass('blue'); $("#menu-item-26 a").removeclass('blue'); $("#menu-item-23 a").removeclass('blue'); $("#menu-item-24 a").removeclass('blue'); } else if (scroll >= 2546 && scroll <= 2969) { $("#menu-item-23 a").addclass('blue'); $("#menu-item-25 a").removeclass('blue'); $("#menu-item-26 a").removeclass('blue'); $("#menu-item-22 a").removeclass('blue'); $("#menu-item-24 a").removeclass('blue'); } else if (scroll >= 2970) { $("#menu-item-24 a").addclass('blue'); $("#menu-item-25 a").removeclass('blue'); $("#menu-item-26 a").removeclass('blue'); $("#menu-item-22 a").removeclass('blue'); $("#menu-item-23 a").removeclass('blue'); } });
it looks awfully ugly. there better way achieve goal?
all previous answers work fine, because have multiple ways make better making changes css selectors, if calculations in scroll event, should read john resign post how deal scroll event, specially part:
it’s very, very, bad idea attach handlers window scroll event. depending upon browser scroll event can fire lot , putting code in scroll callback slow down attempts scroll page (not idea). performance degradation in scroll handler(s) result compound performance of scrolling overall. instead it’s better use form of timer check every x milliseconds or attach scroll event , run code after delay (or after given number of executions – , delay).
- john resign
so, in case go this:
html:
<div class="menu"> <a id="menu-item-22">link 1</a> <a id="menu-item-23">link 2</a> <a id="menu-item-24">link 3</a> <a id="menu-item-25">link 4</a> <a id="menu-item-26">link 5</a> </div>
jquery:
var didscroll = false; $(window).scroll(function() { didscroll = true; }); setinterval(function() { if ( didscroll ) { didscroll = false; var $el; //same if else statements switch (true) { case (scroll >= 591 && scroll <= 1380): $el = $("#menu-item-26 a"); break; case (scroll >= 1381 && scroll <= 2545): $el = $("#menu-item-22 a"); break; case (scroll >= 2546 && scroll <= 2969): $el = $("#menu-item-23 a"); break; case (scroll >= 2970): $el = $("#menu-item-24 a"); break; default: //scroll<=590 $el = $("#menu-item-25 a"); } //removing blue class links $('.menu a').removeclass('blue'); //adding blue class matched element $el.addclass('blue'); } }, 50);
Comments
Post a Comment