调整窗口大小时调用函数

每当调整浏览器窗口大小时,如何调用此(或任何)JS函数?

 function setEqualHeight(e) { var t = 0; e.each(function () { currentHeight = $(this).height(); if (currentHeight > t) { t = currentHeight } }); e.height(t) } $(document).ready(function () { setEqualHeight($(".border")) })  

您可以使用窗口onresize事件:

 window.onresize = setEqualHeight; 

您可以订阅window.onresize事件( 请参阅此处 )

 window.onresize = setEqualHeight; 

要么

 window.addEventListener('resize', setEqualHeight); 

您使用jquery,因此使用.resize()方法绑定它。

 $(window).resize(function () { setEqualHeight( $('#border') ); }); 

这段代码将添加一个定时器,在调整窗口大小200毫秒后调用resize函数。 这将减少方法的调用。

 var globalResizeTimer = null; $(window).resize(function() { if(globalResizeTimer != null) window.clearTimeout(globalResizeTimer); globalResizeTimer = window.setTimeout(function() { setEqualHeight(); }, 200); });