Problème avec la fontion animation de jquery

Par Eryquien durden, il y a 10 ans


Bonjour, j'ai un souci un peu bizarre: Je veux réaliser un menu de navigation fixé en haut de la page avec une opacité à 0.3 en rgba et qui devient opaque (1) lorsque l'on scroll à 500px et invèrsement. J'ai donc pondu ce code mais le problème est qu'il y a un énorme temps de latence avant que l'animation ne s'enclenche et ce qui est encore plus bizarre, c'est que lorsque je modifie le temps de l'animation de 500ms à 1ms, tout va bien mais du coup, c'est moche ^^

Je vous met le code:
<nav id="MP">
<ul>
<li><a href="#">Section 1</a></li>
<li><a href="#">Section 2</a></li>
<li><a href="#">Section 3</a></li>
</ul>
</nav>

MP{

position: fixed;
width: 100%;
background-color: rgba(0,0,0,0.3);
}

MP ul li{

display: inline-block;
margin-right: 50px;
}

MP ul li a{

text-decoration: none;
outline: none;
color: rgba(255,255,255,0.3);
font-size: 1.5em;
font-family: impact;
}

$(window).ready(function() {

$(window).scroll(function(){
    if ($(window).scrollTop() > 500) {
        $('#MP').animate({backgroundColor:'rgba(0,0,0,1)'},500);
    }
    else {
        $('#MP').animate({backgroundColor:'rgba(0,0,0,0.3)'},500);                            
    }
});

});

Merci d'éclairer ma lanterne :)

4 réponses

Eryquien durden, il y a 10 ans

ça fonctionne impec! merci!
J'ai bricolé ça entre temps:

var now = 0;
var before = 0;
$(window).ready(function() {
$(window).scroll(function(){
before = now;
if ($(window).scrollTop() > 500)
now = 1;
else
now = 0;
if (now != before) {
if ($(window).scrollTop() > 500) {
is_animate = true;
$('#MP').animate({backgroundColor:'rgba(0,0,0,1)'},300, function() {
is_animate = false;
});
}
else {
is_animate = true;
$('#MP').animate({backgroundColor:'rgba(0,0,0,0.3)'},300, function() {
is_animate = false;
});
}
}
});
});

C'est propre?

betaWeb, il y a 10 ans

Non ce n'est pas propre du tout. Tu n'es pas obligé de passer ni par jQuery, ni par la fonction animate(). Prends exemple sur ce que j'ai fait ('vanilla' JS) ;)

Eryquien durden, il y a 10 ans

Voilà ce que ça fais de vouloir apprendre jquery avant js...
Je te remercie betaWeb :)