Debian Codegraph @

jquery.center.js

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*! Copyright 2011, Ben Lin (http://dreamerslab.com/)
2 * Licensed under the MIT License (LICENSE.txt).
3 *
•4 * Version: 1.1.1
5 *
6 * Requires: jQuery 1.2.6+
7 */
8 ;( function( $, window ){
•9 var get_win_size = function (){
10 if( window.innerWidth != undefined ){
11 return [ window.innerWidth, window.innerHeight ];
12 }else{
13 var B = document.body;
14 var D = document.documentElement;
15
16 return [ Math.max( D.clientWidth, B.clientWidth ), Math.max( D.clientHeight, B.clientHeight )];
17 }
18 }
19
20 $.fn.center = function( opt ){
21 var $w = $( window ); // cache gobal
22 var scrollTop = $w.scrollTop();
23
24 return this.each( function(){
25 var $this = $( this ); // cache $( this )
•26
27 // merge user options with default configs
28 var configs = $.extend({
29 against : 'window',
30 top : false,
31 topPercentage : 0.5,
32 resize : true
33 }, opt );
34
35 var centerize = function(){
36 var against = configs.against;
•37 var against_w_n_h;
38 var $against;
39
40 if( against === 'window' ){
•41 against_w_n_h = get_win_size();
42 }else if( against === 'parent' ){
•43 $against = $this.parent();
44 against_w_n_h = [ $against.width(), $against.height()];
45 scrollTop = 0;
46 }else{
•47 $against = $this.parents( against );
48 against_w_n_h = [ $against.width(), $against.height()];
49 scrollTop = 0;
50 }
51
•52 var x = (( against_w_n_h[ 0 ]) - ( $this.outerWidth())) * 0.5;
53 var y = (( against_w_n_h[ 1 ]) - ( $this.outerHeight())) * configs.topPercentage + scrollTop;
54
55 if( configs.top ) y = configs.top + scrollTop;
56
57 $this.css({
58 'left' : x,
•59 'top' : y
60 });
61 };
62
63 // apply centerization
64 centerize();
65 if( configs.resize === true ) $w.resize( centerize );
66 });
67 };
68 })( jQuery, window );