【发布时间】:2013-06-03 08:25:54
【问题描述】:
您好,请看一下我的网站,有问题的代码 sn-p 我必须将图像居中,因为我从来没有使用 css-html 方法。有时它会居中加载,有时则不会,请告诉我如何在页面加载时将图像居中。
<script type="text/javascript"> //Centering Script
$(document).ready(function () {
updateContainer();
$(window).resize(function() {
updateContainer();
});
});
function updateContainer() {
(function ($) {
$.fn.vAlign = function() {
return this.each(function(i){
var h = $(this).height();
var oh = $(this).outerHeight();
var mt = (h + (oh - h)) / 2;
$(this).css("margin-top", "-" + mt + "px");
$(this).css("top", "50%");
$(this).css("position", "absolute");
});
};
})(jQuery);
(function ($) {
$.fn.hAlign = function() {
return this.each(function(i){
var w = $(this).width();
var ow = $(this).outerWidth();
var ml = (w + (ow - w)) / 2;
$(this).css("margin-left", "-" + ml + "px");
$(this).css("left", "50%");
$(this).css("position", "absolute");
});
};
})(jQuery);
【问题讨论】:
-
StackOverflow 的重点在于它旨在成为一个问答社区。一旦您对您的问题有了答案,建议将其留给其他人用作基础(尽可能),不要删除它或用胡言乱语替换它。
标签: javascript jquery html css centering