Some problems have been encountered in the project practice. For example, the question of how jquery centralizes div has been bothering me, so I have sought some methods to share with you very short version:
[Html]
The Code is as follows:
Detail ('{mydiv'}.css ({top: '000000', left: '000000', margin: '-' + ($ ('# myDiv '). height ()/2) + 'px 0 0-'+ ($ (' # myDiv '). width ()/2) + 'px '});
Detail ('{mydiv'}.css ({top: '000000', left: '000000', margin: '-' + ($ ('# myDiv '). height ()/2) + 'px 0 0-'+ ($ (' # myDiv '). width ()/2) + 'px '});
Short version:
[Html]
The Code is as follows:
(Function ($ ){
$. Fn. extend ({
Center: function (){
Return this. each (function (){
Var top = ($ (window). height ()-$ (this). outerHeight ()/2;
Var left = ($ (window). width ()-$ (this). outerWidth ()/2;
Vertex (this).css ({position: 'absolute ', margin: 0, top: (top> 0? Top: 0) + 'px ', left: (left> 0? Left: 0) + 'px '});
});
}
});
}) (JQuery );
(Function ($ ){
$. Fn. extend ({
Center: function (){
Return this. each (function (){
Var top = ($ (window). height ()-$ (this). outerHeight ()/2;
Var left = ($ (window). width ()-$ (this). outerWidth ()/2;
Vertex (this).css ({position: 'absolute ', margin: 0, top: (top> 0? Top: 0) + 'px ', left: (left> 0? Left: 0) + 'px '});
});
}
});
}) (JQuery );
Activated by this code:
The Code is as follows:
$ ('# MainDiv'). center ();
[Javascript]
The Code is as follows:
(Function ($ ){
$. Fn. extend ({
Center: function (options ){
Var options = $. extend ({// Default values
Inside: window, // element, center into window
Transition: 0, // millisecond, transition time
MinX: 0, // pixel, minimum left element value
MinY: 0, // pixel, minimum top element value
WithScrolling: true, // booleen, take care of the scrollbar (scrollTop)
Vertical: true, // booleen, center vertical
Horizontal: true // booleen, center horizontal
}, 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). outerWidth ()/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 metrics (this).css (props );
Return $ (this );
});
}
});
}) (JQuery );
[Code]
(Function ($ ){
$. Fn. extend ({
Center: function (options ){
Var options = $. extend ({// Default values
Inside: window, // element, center into window
Transition: 0, // millisecond, transition time
MinX: 0, // pixel, minimum left element value
MinY: 0, // pixel, minimum top element value
WithScrolling: true, // booleen, take care of the scrollbar (scrollTop)
Vertical: true, // booleen, center vertical
Horizontal: true // booleen, center horizontal
}, 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). outerWidth ()/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 metrics (this).css (props );
Return $ (this );
});
}
});
}) (JQuery );
PLUGIN VERSION
[Javascript]
The Code is as follows:
(Function ($ ){
$. Fn. extend ({
Center: function (options ){
Var options = $. extend ({// Default values
Inside: window, // element, center into window
Transition: 0, // millisecond, transition time
MinX: 0, // pixel, minimum left element value
MinY: 0, // pixel, minimum top element value
WithScrolling: true, // booleen, take care of the scrollbar (scrollTop)
Vertical: true, // booleen, center vertical
Horizontal: true // booleen, center horizontal
}, 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). outerWidth ()/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 metrics (this).css (props );
Return $ (this );
});
}
});
}) (JQuery );
The Code is as follows:
(Function ($ ){
$. Fn. extend ({
Center: function (options ){
Var options = $. extend ({// Default values
Inside: window, // element, center into window
Transition: 0, // millisecond, transition time
MinX: 0, // pixel, minimum left element value
MinY: 0, // pixel, minimum top element value
WithScrolling: true, // booleen, take care of the scrollbar (scrollTop)
Vertical: true, // booleen, center vertical
Horizontal: true // booleen, center horizontal
}, 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). outerWidth ()/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 metrics (this).css (props );
Return $ (this );
});
}
});
}) (JQuery );
Activated by this code:
The Code is as follows:
$ (Document). ready (function (){
$ ('# MainDiv'). center ();
$ (Window). bind ('resize', function (){
$ ('# MainDiv'). center ({transition: 300 });
});
);
This person's JS Code is amazing.
Concise and clear. However.