Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide an element when a certain amount of scrolling has occured

I'd just like to hide an element on my page, after N number of pixels have been scrolled.

$(window).scroll(function(){
  if($(document).scrollTop() > 200){
    $('.fixedelement').css({'display': 'none'});
  }
});

I thought this might work, and after 200px of scrolling the .fixedelement would vanish. Alas, it doesn't work. Any thoughts?

like image 762
Fuego DeBassi Avatar asked Jul 25 '11 18:07

Fuego DeBassi


2 Answers

Try this.

$(window).scroll(function(){
  if($(document).scrollTop() > 200){//Here 200 may be not be exactly 200px
    $('.fixedelement').hide();
  }
});
like image 27
ShankarSangoli Avatar answered Sep 19 '22 12:09

ShankarSangoli


It seems to work fine here: http://jsfiddle.net/maniator/yDVXY/

$(window).scroll(function() {
    if ($(this).scrollTop() > 200) { //use `this`, not `document`
        $('.fixedelement').css({
            'display': 'none'
        });
    }
});
like image 180
Naftali Avatar answered Sep 21 '22 12:09

Naftali