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. };  

 

相关文章
|
14天前
|
JavaScript 前端开发 UED
JS:如何获取浏览器窗口尺寸?
JS:如何获取浏览器窗口尺寸?
23 1
|
4月前
|
JavaScript
js实现跨浏览器tab选项卡页通信、传参,监听localStorage.变量的实时变化,实现打开多个浏览器页面窗口相互可以传参通信
js实现跨浏览器tab选项卡页通信、传参,监听localStorage.变量的实时变化,实现打开多个浏览器页面窗口相互可以传参通信
|
4月前
|
JavaScript 前端开发
js怎样获取浏览器窗口尺寸
js怎样获取浏览器窗口尺寸
27 1
|
5月前
|
JavaScript 前端开发
js怎样获取浏览器窗口尺寸
js怎样获取浏览器窗口尺寸
31 0
|
8月前
|
前端开发 JavaScript 安全
使用原生JavaScript对网页或窗口进行截图
要使用原生 JavaScript 对整个网页或窗口进行截图,你可以使用 html2canvas 库。 html2canvas 是一个强大的 JavaScript 库,可以将网页的可见部分渲染为 <canvas> 元素,并且可以保存为图像。
216 0
|
10月前
|
JavaScript 前端开发
获取屏幕、窗口和网页尺寸, 用JavaScript
本教程介绍了如何使用JavaScript获取屏幕、窗口和网页的尺寸。通过对不同尺寸的定义和访问方法进行解释,帮助读者更好地理解各种尺寸的含义和用途。
233 0
|
10月前
|
JavaScript 前端开发
JavaScript窗口(window)对象介绍
JavaScript窗口(window)对象介绍
87 0
|
JavaScript 容器
Vue2.js使用Echarts绘制图表并随窗口容器变化自适应
Vue2.js使用Echarts绘制图表并随窗口容器变化自适应
104 0
|
JavaScript 前端开发 安全
JavaScript怎么实现两个窗口之间的通信?
JavaScript怎么实现两个窗口之间的通信?
242 0
JavaScript怎么实现两个窗口之间的通信?
|
JavaScript 前端开发 数据安全/隐私保护
JS实现关闭图片窗口
通过事件的绑定来实现,关闭二维码的效果。
127 0
JS实现关闭图片窗口