Daan
Daan

Reputation: 2799

Trigger an event that triggers on scroll only on page load

I want to trigger a function that only trigger on scroll, is that possible?

$(document).ready(function() 
    $(document).scroll(function() {
        // do something here
    });

    // here I want to trigger the scroll function (without scrolling)
});

I tried $(document).scroll(); without success:

$(document).ready(function() 
    $(document).scroll(function() {
        // do something here
    });
    $(document).scroll();
});

Upvotes: 0

Views: 533

Answers (1)

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

Change it to use a named function:

$(document).ready(function() 
    $(document).scroll(onScroll);

    onScroll();
});

function onScroll() {
}

Now that the function is named you can freely execute that code whenever you want.

Upvotes: 2

Related Questions