简体   繁体   中英

Run a function when a div is scrolled and also on page load?

I have a function that runs when you scroll a div

this.$something.scroll(function () {
    // Do soemthing
});

How can I run a function when you scroll a div and also on page load? Ive done it with the following but there must be a cleaner way to write it?

this.$something.scroll(function () {
    myFunc();
});

$(document).ready(function () {
    myFunc();
});

You can pass the function name as the callback to the event handlers. When the event is triggered the callback function is called with the event object as parameter.

this.$something.scroll(myFunc);
//                     ^^^^^^

$(document).ready(myFunc);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM