我计算元素的填充并在调整大小时添加它。现在,我想在页面加载时添加它。我怎么能这样做?
$(window).resize(function () {
var widthWindow = $(window).width();
var widthContainer = $("#container").width();
var calculatePadding = (widthWindow - widthContainer) / 2;
$("#container").css({
"padding-top":calculatePadding,
"padding-bottom":calculatePadding
});
}).resize();
答案 0 :(得分:0)
你可以这样做:
<body onload="resize()">
在Javascript中:
function resize(){
//write your stuff here
}
(或)jQuery方式
$(document).ready(function(){
resize();
});
答案 1 :(得分:0)
只需将处理程序绑定到load事件:
var handler = function () {
var widthWindow = $(window).width();
var widthContainer = $("#container").width();
var calculatePadding = (widthWindow - widthContainer) / 2;
$("#container").css({
"padding-top":calculatePadding,
"padding-bottom":calculatePadding
});
};
$(window).resize(handler)
$(window).load(handler)
或者在我看来更优雅一点:
$(window).on('load, resize', handler)