js实现使元素在屏幕里上下左右居中

来源:互联网 发布:oracle dmp更新数据库 编辑:程序博客网 时间:2024/05/22 14:47
<script type="text/javascript" src="js/jquery-1.4.2.js"></script>

<script type="text/javascript">
$(document).ready(function() {
jQuery.fn.center = function() {
this.css("position", "absolute");
this.css("top", ($(window).height() - this.height()) / 2 + $(window).scrollTop() + "px");
this.css("left", ($(window).width() - this.width()) / 2 + $(window).scrollLeft() + "px");
return this;
}
$("#id").center();
});
</script>


//ID为要在屏幕上下左右居中的元素ID,而不是class
原创粉丝点击