我想在加载和调整大小时调用该函数。
有没有更好的方法来更紧凑地重写它?
$('.content .right').width($(window).width() - (480));
$(window).resize(function(e) {
$('.content .right').width($(window).width() - (480));
});
最佳答案
您可以单独绑定(bind)到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()
调用将在加载时运行此代码。
关于jquery - 结合 onload 和 onresize (jQuery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1974788/