简体   繁体   中英

Call a function whenever the viewport size is changed by user

I'm trying to make an event listener like this:

window.addEventListener("resize", console.log("You just resized the browser window."));

But it only fires once when page is loaded, and never fires again no matter how I resize the window in whatever way. Why?

I've also tried jQuery:

$(window).on("resize", console.log("You just resized the browser window."));

and

$(window).resize(console.log("You just resized the browser window."));

They didn't work either. The three are essentially all the same.

You are executing console.log instead of passing a reference to the call for the event listener to fire.

This will work:

notify = function() { console.log("You just resized the browser window.")}
window.addEventListener("resize", notify)

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