jQuery.fn.center = function(f) {
return this.each(function(){
var p = f===false?document.body:this.parentNode;
if ( p.nodeName.toLowerCase()!= "body" && jQuery.css(p,"position") == 'static' )
p.style.position = 'relative';
var s = this.style;
s.position = 'absolute';
if(p.nodeName.toLowerCase() == "body")
var w=$(window);
if(!f || f == "horizontal") {
s.left = "0px";
if(p.nodeName.toLowerCase() == "body") {
var clientLeft = w.scrollLeft() - 10 + (w.width() - parseInt(jQuery.css(this,"width")))/2;
s.left = Math.max(clientLeft,0) + "px";
}else if(((parseInt(jQuery.css(p,"width")) - parseInt(jQuery.css(this,"width")))/2) > 0)
s.left = ((parseInt(jQuery.css(p,"width")) - parseInt(jQuery.css(this,"width")))/2) + "px";
}
if(!f || f == "vertical") {
s.top = "0px";
if(p.nodeName.toLowerCase() == "body") {
var clientHeight = w.scrollTop() - 10 + (w.height() - parseInt(jQuery.css(this,"height")))/2;
s.top = Math.max(clientHeight,0) + "px";
}else if(((parseInt(jQuery.css(p,"height")) - parseInt(jQuery.css(this,"height")))/2) > 0)
s.top = ((parseInt(jQuery.css(p,"height")) - parseInt(jQuery.css(this,"height")))/2) + "px";
}
});
};
jQuery 中的center居中方法
jQuery居中插件解析
最新推荐文章于 2019-02-01 18:32:00 发布
本文介绍了一个基于jQuery的自定义函数,该函数能够使元素在其父容器内水平和垂直居中显示。通过修改样式属性实现居中效果,并考虑了不同浏览器环境下的兼容性问题。

342

被折叠的 条评论
为什么被折叠?



