每当用户更改视口大小时调用函数

Call a function whenever the viewport size is changed by user

我正在尝试制作这样的事件侦听器:

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

但是它只在页面加载时触发一次,无论我如何调整 window 的大小都不会再触发。为什么?

我也试过了jQuery:

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

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

他们也没有用。三者本质上都是一样的

您正在执行 console.log 而不是将引用传递给触发事件侦听器的调用。

这会起作用:

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