1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
/*! Copyright 2011, Ben Lin (http://dreamerslab.com/)
* Licensed under the MIT License (LICENSE.txt).
*
* 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',
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
});
};
// apply centerization
centerize();
if( configs.resize === true ) $w.resize( centerize );
});
};
})( jQuery, window );
|