Permalink
Browse files

release 1.1.1, [bug fixed] center against window failed

  • Loading branch information...
1 parent 1cf91c4 commit 72408e8ae31ba533f26c976f8a1baca1912adfa4 Fred Chu committed Oct 31, 2012
Showing with 32 additions and 11 deletions.
  1. +6 −0 CHANGELOG.md
  2. +24 −9 jquery.center.js
  3. +2 −2 jquery.center.min.js
View
6 CHANGELOG.md
@@ -1,5 +1,11 @@
# jQuery Center Plugin CHANGELOG
+## 1.1.1/ 2012-10-31
+
+- [bug fix] center against window failed
+
+
+
## 1.1.0
* Add center on window resize option, default to true
View
33 jquery.center.js
@@ -1,17 +1,29 @@
/*! Copyright 2011, Ben Lin (http://dreamerslab.com/)
* Licensed under the MIT License (LICENSE.txt).
*
-* Version: 1.1.0
+* Version: 1.1.1
*
* Requires: jQuery 1.2.6+
*/
;( function( $, window ){
+ var get_win_size = function (){
+ if( window.innerWidth != undefined ){
+ return [ window.innerWidth, window.innerHeight ];
+ }else{
+ var B = document.body;
+ var D = document.documentElement;
+
+ return [ Math.max( D.clientWidth, B.clientWidth ), Math.max( D.clientHeight, B.clientHeight )];
+ }
+ }
+
$.fn.center = function( opt ){
var $w = $( window ); // cache gobal
var scrollTop = $w.scrollTop();
return this.each( function(){
var $this = $( this ); // cache $( this )
+
// merge user options with default configs
var configs = $.extend({
against : 'window',
@@ -22,26 +34,29 @@
var centerize = function(){
var against = configs.against;
+ var against_w_n_h;
var $against;
if( against === 'window' ){
- $against = $w;
+ against_w_n_h = get_win_size();
}else if( against === 'parent' ){
- $against = $this.parent();
- scrollTop = 0;
+ $against = $this.parent();
+ against_w_n_h = [ $against.width(), $against.height()];
+ scrollTop = 0;
}else{
- $against = $this.parents( against );
- scrollTop = 0;
+ $against = $this.parents( against );
+ against_w_n_h = [ $against.width(), $against.height()];
+ scrollTop = 0;
}
- var x = (( $against.width()) - ( $this.outerWidth())) * 0.5;
- var y = (( $against.height()) - ( $this.outerHeight())) * configs.topPercentage + scrollTop;
+ var x = (( against_w_n_h[ 0 ]) - ( $this.outerWidth())) * 0.5;
+ var y = (( against_w_n_h[ 1 ]) - ( $this.outerHeight())) * configs.topPercentage + scrollTop;
if( configs.top ) y = configs.top + scrollTop;
$this.css({
'left' : x,
- 'top' : y
+ 'top' : y
});
};
View
4 jquery.center.min.js
@@ -1,8 +1,8 @@
/*! Copyright 2011, Ben Lin (http://dreamerslab.com/)
* Licensed under the MIT License (LICENSE.txt).
*
-* Version: 1.1.0
+* Version: 1.1.1
*
* Requires: jQuery 1.2.6+
*/
-;(function(b,a){b.fn.center=function(c){var d=b(a);var e=d.scrollTop();return this.each(function(){var h=b(this);var g=b.extend({against:"window",top:false,topPercentage:0.5,resize:true},c);var f=function(){var k=g.against;var j;if(k==="window"){j=d;}else{if(k==="parent"){j=h.parent();e=0;}else{j=h.parents(k);e=0;}}var i=((j.width())-(h.outerWidth()))*0.5;var l=((j.height())-(h.outerHeight()))*g.topPercentage+e;if(g.top){l=g.top+e;}h.css({left:i,top:l});};f();if(g.resize===true){d.resize(f);}});};})(jQuery,window);
+;(function($,window){var get_win_size=function(){if(window.innerWidth!=undefined)return[window.innerWidth,window.innerHeight];else{var B=document.body;var D=document.documentElement;return[Math.max(D.clientWidth,B.clientWidth),Math.max(D.clientHeight,B.clientHeight)]}};$.fn.center=function(opt){var $w=$(window);var scrollTop=$w.scrollTop();return this.each(function(){var $this=$(this);var configs=$.extend({against:"window",top:false,topPercentage:0.5,resize:true},opt);var centerize=function(){var against=configs.against;var against_w_n_h;var $against;if(against==="window")against_w_n_h=get_win_size();else if(against==="parent"){$against=$this.parent();against_w_n_h=[$against.width(),$against.height()];scrollTop=0}else{$against=$this.parents(against);against_w_n_h=[$against.width(),$against.height()];scrollTop=0}var x=(against_w_n_h[0]-$this.outerWidth())*0.5;var y=(against_w_n_h[1]-$this.outerHeight())*configs.topPercentage+scrollTop;if(configs.top)y=configs.top+scrollTop;$this.css({"left":x,"top":y})};centerize();if(configs.resize===true)$w.resize(centerize)})}})(jQuery,window);

0 comments on commit 72408e8

Please sign in to comment.