我想在加载和调整大小时调用该函数。
有没有更好的方法来更紧凑地重写它?
$('.content .right').width($(window).width() - (480));
$(window).resize(function(e) {
$('.content .right').width($(window).width() - (480));
});发布于 2009-12-29 22:07:44
您可以单独绑定resize事件,在加载时自动触发该事件:
// Bind to the resize event of the window object
$(window).on("resize", function () {
// Set .right's width to the window width minus 480 pixels
$(".content .right").width( $(this).width() - 480 );
// Invoke the resize event immediately
}).resize();最后一次.resize()调用将在加载时运行此代码。
发布于 2014-07-13 18:37:00
我认为最好的解决方案就是将它也绑定到load事件:
$(window).on('load resize', function () {
$('.content .right').width( $(this).width() - 480 );
});发布于 2009-12-29 22:08:01
发现重复的逻辑并将其分解到一个函数中是很好的:
function sizing() {
$('.content .right').width($(window).width() - 480);
}
$(document).ready(sizing);
$(window).resize(sizing);https://stackoverflow.com/questions/1974788
复制相似问题