I have a class ‘active’ that is constantly being removed and added to other divs. But I don’t want it removed if it is applied to a first or last child…
JSFiddle: http://jsfiddle.net/YuDDK/1/
HTML:
<div id="container">
<div class="item active"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
JS:
$('#container').mousewheel(function(event, delta) {
var active = $('.active');
if (delta > 0) {
if (active.not(":first-child")) {
active.removeClass('active').prev().addClass('active');
}
} else {
if (active.not(":last-child")) {
active.removeClass('active').next().addClass('active');
}
}
$(this).scrollTo( $('.active'),100);
return false;
});
active.not(":first-child")returns a jQuery object which is alwaystrue, you can useismethod instead that returns a boolean value.