Share a good way to center your div using jq

Source: Internet
Author: User

Very short version:
Copy codeThe Code is as follows:
Detail ('{mydiv'}.css ({top: '000000', left: '000000', margin: '-' + ($ ('# myDiv '). height ()/2) + 'px 0 0-'+ ($ (' # myDiv '). width ()/2) + 'px '});

Short version:

Copy codeThe 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 );

Activated by this code:
$ ('# MainDiv'). center ();
 

PLUGIN VERSION

Copy codeThe 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:
Copy codeThe Code is as follows:
$ (Document). ready (function (){
$ ('# MainDiv'). center ();
$ (Window). bind ('resize', function (){
$ ('# MainDiv'). center ({transition: 300 });
});
);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.