BlekStena
BlekStena

Reputation: 345

make an element fixed if scrolled more than 140px from top

As it says in the Q, I want to make an element fixed if I scroll the page down more than 14px.

I tried the following but for some reason it is not working.

$(window).unbind('scroll').scroll(function () {
    if ($('body').scrollTop > 140) {
        $('div.top-logo-main').css('position', 'fixed');
    } else {
        $('div.top-logo-main').css('position', 'static');
    }
});

Upvotes: 0

Views: 140

Answers (2)

Pratik Joshi
Pratik Joshi

Reputation: 11693

Use scrollTop() as its functtion

$(window).unbind('scroll').scroll(function () {
    if ($('body').scrollTop() > 140) {
        $('div.top-logo-main').css('position', 'static');
    } else {
        $('div.top-logo-main').css('position', 'fixed');
    }
});

demo http://jsfiddle.net/djgNG/

Its all right I got what you mean, check this new fiddle http://jsfiddle.net/djgNG/2/

Upvotes: 3

PurkkaKoodari
PurkkaKoodari

Reputation: 6809

scrollTop is a function. Call it (note the parenthesis):

if ($('body').scrollTop() > 140) {

Upvotes: 1

Related Questions