設定div始終置中顯示,設定div置中顯示
<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>