Search code examples
javascriptscroll

scroll function for multiple id's


I have the following code to have a div stick to the top upon scroll:

$(window).scroll(function() {
//After scrolling 100px from the top...
if ( $(window).scrollTop() >= 72 || (w > 980)) {
    $('#div1').css('top', '0px');

//Otherwise remove inline styles and thereby revert to original stying
} else {
    $('#div1').attr('style', '');

}
});

Now I want two divs on one page to have the same behaviour. I tried:

 $('#div1' || '#div2')

but that does not seem to work. How could I achieve this?


Solution

  • Either comma separate them like $('#div1, #div2') or just add a class instead of using id's like $('.stickyDiv') and then

    <div class="stickyDiv" id="div1"></div>
    <div class="stickyDiv" id="div2"></div>