Salim Salim - 4 years ago 141
Javascript Question

Sticky div dont stick at top at scroll

I want that my div called: top will stick on the top if i scroll down, but right now it don't stick at the top.

I am using jQuery with this code:

$(window).scroll(function(){
if ($(this).scrollTop() > 5) {
$('#top').addClass('.fixed').FadeIn(200);
} else {
$('#top').removeClass('.fixed');
}
});


Does someone know where the problem is?

Answer Source

Do not add . for the class names inside addClass(). Try this:

 $(window).scroll(function() {
   if ($(this).scrollTop() > 5) {
     $('#top').addClass('fixed').FadeIn(200);
   } else {
     $('#top').removeClass('fixed');
   }
 });
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download