js 让窗口居中

简介:

直接上代码

Js代码   收藏代码
  1. $(function() {  
  2.   
  3.   
  4.     function center_pos(){    
  5.         var width = $('.dialog').width();  
  6.         var height = $('.dialog').height();  
  7.   
  8.         var top = (getInner().height - height) / 2 + getScroll().top;  
  9.         var left = (getInner().width - width) / 2 +getScroll().left;  
  10.   
  11.         $('.dialog').css({  
  12.             'top': top,  
  13.             'left': left  
  14.         });  
  15.     }  
  16.   
  17.     $(window).bind('load',center_pos);  
  18.     $(window).bind('resize',center_pos);  
  19.   
  20.       
  21. });  

 封装之后:

Js代码   收藏代码
  1. /*** 
  2.  * make dialog in center 
  3.  */  
  4. com.whuang.hsj.centerJQueryPos = function ($div22, isApplyVertical) {  
  5.     var width = $div22.width();  
  6.     var height = $div22.height();  
  7.   
  8.     var top = (getInner().height - height) / 2 + com.whuang.hsj.getScroll().top;  
  9.     var left = (getInner().width - width) / 2 + com.whuang.hsj.getScroll().left;  
  10.     var param = {'left': left};  
  11.     if (arguments.length === 1 || isApplyVertical) {//Vertical direction  
  12.         param['top'] = top;  
  13.     }  
  14.     $div22.css(param);  
  15. };//centerJQueryPos  

 

依赖的js方法:

Js代码   收藏代码
  1. //Cross browser gets the size of Visual area window,Have nothing to do with scroll bars  
  2. var getInner=(function() {  
  3.     // alert(typeof window.innerWidth !== 'undefined');  
  4.     if (typeof window.innerWidth !== 'undefined') {//Notice:'undefined' is right  
  5.         return function(){  
  6.             return {  
  7.                 width : window.innerWidth,  
  8.                 height : window.innerHeight  
  9.             }  
  10.         }  
  11.     } else {  
  12.         return function(){  
  13.             return {  
  14.                 width : document.documentElement.clientWidth,  
  15.                 height : document.documentElement.clientHeight  
  16.             }  
  17.         }  
  18.     }  
  19. })();  
  20. //Cross browser gets the position of scroll  
  21. com.whuang.hsj.getScroll=function(){  
  22.     return {  
  23.         top:document.documentElement.scrollTop || document.body.scrollTop,  
  24.         left:document.documentElement.scrollLeft || document.body.scrollLeft,  
  25.         height:document.documentElement.scrollHeight ||document.body.scrollHeight  
  26.     };  
  27. };  

 

相关文章
|
5月前
|
JavaScript 前端开发 UED
JS:如何获取浏览器窗口尺寸?
JS:如何获取浏览器窗口尺寸?
232 1
|
4月前
|
JavaScript 前端开发 API
JavaScript基础-BOM与窗口交互
【6月更文挑战第12天】本文介绍了BOM(浏览器对象模型),它是JavaScript与浏览器交互的API。核心对象包括顶级对象window、document、location、navigator和history。常见问题涉及window全局作用域、location.href编码、history使用和navigator.userAgent检测。提供了代码示例,如设置页面标题、页面跳转及利用history实现无刷新跳转。掌握BOM基础和最佳实践对前端开发至关重要。
35 5
|
4月前
|
JavaScript 前端开发
js怎样获取浏览器窗口尺寸
js怎样获取浏览器窗口尺寸
|
5月前
|
JavaScript 前端开发
JavaScript 窗口
JavaScript 窗口
|
5月前
|
JavaScript 前端开发
js怎样获取浏览器窗口尺寸
js怎样获取浏览器窗口尺寸
55 1
|
5月前
|
JavaScript
js实现跨浏览器tab选项卡页通信、传参,监听localStorage.变量的实时变化,实现打开多个浏览器页面窗口相互可以传参通信
js实现跨浏览器tab选项卡页通信、传参,监听localStorage.变量的实时变化,实现打开多个浏览器页面窗口相互可以传参通信
|
11月前
|
JavaScript 前端开发
JavaScript打开新窗口的几种方式
JavaScript打开新窗口的几种方式
|
11月前
|
JavaScript 前端开发
js怎样获取浏览器窗口尺寸
js怎样获取浏览器窗口尺寸
49 0
|
前端开发 JavaScript 安全
使用原生JavaScript对网页或窗口进行截图
要使用原生 JavaScript 对整个网页或窗口进行截图,你可以使用 html2canvas 库。 html2canvas 是一个强大的 JavaScript 库,可以将网页的可见部分渲染为 <canvas> 元素,并且可以保存为图像。
484 0
|
JSON JavaScript 前端开发
JavaScript iframe 多窗口通信实战
JavaScript iframe 多窗口通信实战
111 0