Skip to content

Commit

Permalink
release 1.1.1, [bug fixed] center against window failed
Browse files Browse the repository at this point in the history
  • Loading branch information
Fred Chu committed Oct 31, 2012
1 parent 1cf91c4 commit 72408e8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
6 changes: 6 additions & 0 deletions 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
Expand Down
33 changes: 24 additions & 9 deletions 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',
Expand All @@ -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
});
};

Expand Down
4 changes: 2 additions & 2 deletions jquery.center.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 72408e8

Please sign in to comment.