设定div始终居中显示
2015-03-17 10:28
211 查看
<script type="text/javascript"> (function($){ $.fn.extend({ center:function(options){ //center插件 var options=$.extend({ //默认属性值 inside:window, //元素居于窗体中心 transition:0, //元素居中移动时的时间,单位是millisecond minX:0, //元素最小X方向距边值 minY:0, //元素最小Y方向距边值 withScrolling:true, //是否支持滚动条 vertical:true, //是否支持垂直居中 horizontal:true //是否支持水平居中 },options); return this.each(function(){ //通过计算窗口尺寸与元素尺寸将元素居中显示 var props={position:'absolute'}; if(options.vertical){ var top=($(options.inside).height()-$(this).outerHeight())/2; if(options.withScrolling) top+=$(options.inside).scrollTop()||0; top=(top>options.minY?top:options.minY); $.extend(props,{top:top+'px'}); } if (options.horizontal) { var left=($(options.inside).width()-$(this).outerHeight())/2; if(options.withScrolling) left+=$(options.inside).scrollLeft()||0; left=(left>options.minX?left:options.minX); $.extend(props,{left:left+'px'}); } if(options.transition>0)$(this).animate(props,options.transition); else $(this).css(props); return $(this); }); } }); })(jQuery); </script> <script type="text/javascript"> $(document).ready(function(){ $("#centerDiv").center(); $(window).bind('resize',function(){ $("#centerDiv").center({transition:500}); }); }); </script> </head> <body> <div id="centerDiv" style="height: 100px;width: 200px;background: #ccf">设定div始终居中显示</div> </body>
相关文章推荐
- 控制div始终居中显示
- 如何让网页div图层居中显示
- 居中显示的div
- div内容居中显示
- 如何始终在屏幕中间显示Div
- 始终在屏幕中间显示Div的代码(css+js)
- 让2个DIV居中并排显示
- div固定高度中图片垂直居中显示
- 如何让div容器水平居中显示在父容器中
- 关于动态控制table的位置,使其不受分辨率的限制,始终居中显示。
- 使弹出div的文职居中显示
- DIV居中,火狐下无法正常显示解决方法
- 用CSS 怎么样让div居中显示
- 让一个div绝对居中显示的跨浏览器js
- DIV水平垂直居中显示
- 让div中的文字居中显示
- div水平垂直居中显示
- div水平垂直居中显示
- DIV始终居中的半透明弹出层
- css+div使网页居中显示