If I understand correctly, then you need to use .off()
several times as follows :
function slidefader(){
var slideindex = mySwiper.activeIndex;
$('title').html(slideindex);
// First slide
if (slideindex == 0) {
$('.fade').clearQueue().delay(600).fadeIn(200);
$('.fade2').fadeOut(100);
$('#leftbar').off('mouseenter').off('mouseleave');
}
// Other slides
if (slideindex >= 1) {
$('.fade').clearQueue().fadeOut(200);
$('.fade2').fadeIn();
$('#leftbar').off('mouseenter').on('mouseenter', function() {
$('.fade').stop().fadeIn(200);
}).off('mouseleave').on('mouseleave', function(){
$('.fade').stop().fadeOut(200);
});
}
}
For slide #0, any currently attached mouseenter/mouseleave handlers will be removed.
For slide #1, any currently attached mouseenter/mouseleave effect will be removed (to prevent an accumulation of effects) and fresh handlers will be attached.
Although you didn't ask for it, I have also included .stop()
. This will stop dead any effect(s) that are currently running before initiating the desired effect. Without stop()
, the desired animation will run after any current effect(s) finish. This is important to cater for a quick mouseenter-mouseleave sequence (or worse, eg. mouseenter-mouseleave-mouseenter-mouseleave-mouseenter-mouseleave).
Note also :
- use of
.on(...)
instead of.mouseenter()
and.mouseleave()
is optional. It's maybe slighly clearer to use.on()
when also using.off()
- method chaining improves the efficiency of the code.