如何在JavaScript中触发窗口大小调整事件? [英] How to trigger the window resize event in JavaScript?
问题描述
我发现 window.resizeTo()
可以触发函数,但是还有其他的解决方案吗?
不用担心触发该事件。我想象你有这样的东西:
window.onresize = function(){
//做一些东西
};
或类似的东西。
将做一些东西变成一个这样的函数:
window.onresize = doALoadOfStuff;
函数doALoadOfStuff(){
//做一些东西
}
这样,而不是尝试触发resize事件,你可以调用doALoadOfStuff函数。
你可以使用jQuery来触发事件 - 尽管如果你想运行代码,最好是直接执行它,而不需要将事件联系起来(如果你改变了事件 - 那么你的触发器不会执行你的期望,而如果你调用这个函数你没有耦合到事件)...
$(window).trigger('调整');
您可以使用简单的JavaScript
var handler = window.onresize;
handler();
//或者如果你需要传递参数...
handler.apply(window,['param','another']);
演示: http://jsfiddle.net/RT4ve/
I have registered a trigger on window resize. I want to know how I can trigger the event to be called. For example, when hide a div, I want my trigger function to be called.
I found window.resizeTo()
can trigger the function, but is there any other solution?
Don't worry about triggering that event. I imagine you have something like:
window.onresize = function() {
// do a load of stuff
};
Or something similar.
So strip out the "do a load of stuff" into a function like this:
window.onresize = doALoadOfStuff;
function doALoadOfStuff() {
//do a load of stuff
}
That way, rather than trying to trigger the resize event, you can just call the doALoadOfStuff function.
You can use jQuery to trigger the event - although if you want to run the code, it is better to do it directly without coupling yourself to the events (what if you changed the event - then your trigger wouldn't do what you expect, whereas if you call the function you aren't coupled to the event)...
$(window).trigger('resize');
And you could just do it in plain JavaScript
var handler = window.onresize;
handler();
// or if you need to pass parameters...
handler.apply(window, ['param', 'another']);
Demo: http://jsfiddle.net/RT4ve/
这篇关于如何在JavaScript中触发窗口大小调整事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!