qwerty
qwerty

Reputation: 229

Fix a div after scrolling

I would like to fix the div#order in this page http://www.spiaggiati.it/antani/ with this code

$(document).ready(function () {
    $("#order").data("top", $("#order").offset().top);
    $(window).scroll(fixDiv("order"));
})

The function fixDiv is:

function fixDiv(div_id) {
    var $div = $("#"+div_id);
    if ($(window).scrollTop() > $div.data("top")) { 
        $div.css({'position': 'fixed', 'top': '0', 'width': '100%'}); 
    }
    else {
        $div.css({'position': 'static', 'top': 'auto', 'width': '100%'});
    }
}

I cannot make it work, it seems that scroll() is not catching the event. Where is it wrong?

Thank you.

Upvotes: 1

Views: 201

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337560

When passing a function as a reference you cannot supply a parameter. Try changing this:

$(window).scroll(fixDiv("order"));

To this:

$(window).scroll(function() {
    fixDiv("order"));
});

Upvotes: 1

Related Questions