jQuery 调整大小功能在页面加载时不起作用
如何使该函数不仅在窗口大小调整时运行,而且在初始页面加载时运行?
$(window).resize(function() {
...
});
How do I get this function to not only run on window resize but also on initial page load?
$(window).resize(function() {
...
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
自 jQuery 3.0 起,此解决方案现已弃用:https://api.jquery。 com/bind/#bind-eventType-eventData-handler
您需要使用:
使某些事情在加载时发生。如果你想让某些东西在加载和调整大小时工作,你应该这样做:
This solution is now deprecated since jQuery 3.0: https://api.jquery.com/bind/#bind-eventType-eventData-handler
You'll want to use:
To make something happen onload. If you want something to work onload and onresize, you should do:
我认为最好的解决方案是将其绑定到加载和调整大小事件:
I think the best solution is just to bind it to the load and resize event:
此行为是设计使然。
您应该将代码放入命名函数中,然后调用该函数。
例如:
或者,您可以制作一个插件来自动绑定并执行处理程序:
请注意,如果处理程序使用
event
对象,它将无法正常工作,并且它不会涵盖每个重载bind
方法的。This behavior is by design.
You should put your code into a named function, then call the function.
For example:
Alternatively, you can make a plugin to automatically bind and execute a handler:
Note that it won't work correctly if the handler uses the
event
object, and that it doesn't cover every overload of thebind
method.没有和我一起工作。
尝试
一下。
(我知道这个问题已经很老了,但是谷歌将我发送到这里,所以......)
didn't work with me.
Try
instead.
(I know this question is old, but google sent me here, so...)
另一种方法是,您可以简单地设置一个处理程序,然后自己欺骗一个调整大小事件:
Another approach, you can simply setup a handler, and spoof a resize event yourself: