js 让窗口居中

直接上代码

$(function() {


	function center_pos(){	
		var width = $('.dialog').width();
		var height = $('.dialog').height();

		var top = (getInner().height - height) / 2 + getScroll().top;
		var left = (getInner().width - width) / 2 +getScroll().left;

		$('.dialog').css({
			'top': top,
			'left': left
		});
	}

	$(window).bind('load',center_pos);
	$(window).bind('resize',center_pos);

	
});

 封装之后:

/***
 * make dialog in center
 */
com.whuang.hsj.centerJQueryPos = function ($div22, isApplyVertical) {
	var width = $div22.width();
	var height = $div22.height();

	var top = (getInner().height - height) / 2 + com.whuang.hsj.getScroll().top;
	var left = (getInner().width - width) / 2 + com.whuang.hsj.getScroll().left;
	var param = {'left': left};
	if (arguments.length === 1 || isApplyVertical) {//Vertical direction
		param['top'] = top;
	}
	$div22.css(param);
};//centerJQueryPos

 

依赖的js方法:

//Cross browser gets the size of Visual area window,Have nothing to do with scroll bars
var getInner=(function() {
	// alert(typeof window.innerWidth !== 'undefined');
	if (typeof window.innerWidth !== 'undefined') {//Notice:'undefined' is right
		return function(){
			return {
				width : window.innerWidth,
				height : window.innerHeight
			}
		}
	} else {
		return function(){
			return {
				width : document.documentElement.clientWidth,
				height : document.documentElement.clientHeight
			}
		}
	}
})();
//Cross browser gets the position of scroll
com.whuang.hsj.getScroll=function(){
    return {
        top:document.documentElement.scrollTop || document.body.scrollTop,
        left:document.documentElement.scrollLeft || document.body.scrollLeft,
        height:document.documentElement.scrollHeight ||document.body.scrollHeight
    };
};

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值