I am trying to change the logo image src if not window scrollTop and backwards with a fadeIn and fadeOut effect, but even if you scroll once, fadeIn & Out happens multiple times.
I figured out with console.log
that even if you scroll once (mousewheel or arrows), it logs many times the message (like 8,10 or 15 times). That means that the logo flashes always 10+ times before changing the src to the other image.
jQuery:
$(window).scroll(function () {
if ($(window).scrollTop()) {
$.when($('#navbar').css('padding', '0px')).done(function () {
$('.logo img').fadeOut(400, function () {
$('.logo img').attr('src', 'images/small-logo.png');
console.log('bot');
}).fadeIn();
});
} else {
$.when($('#navbar').css('padding', '20px 0px')).done(function () {
$('.logo img').fadeOut(400, function () {
$('.logo img').attr('src', 'images/top-logo.png');
console.log('top');
}).fadeIn();
});
}
});
HTML:
<div class="col-lg-12">
<div class="col-lg-3 logo no-padding-left">
<a href="index.php">
<img src="images/logo.png"/>
</a>
</div>
<div class="col-lg-9 no-padding-right">
<div class="navbar-header">
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
.
.
.
</ul>
</div>
</div>
</div>
</div>
I've found and tried many solutions (like .stop()
, if animated etc), but nothing seemed to work for me and i don't know why.
Thank you in advance.