jquery Div Center Technique Application Introduction _jquery

Source: Internet
Author: User
Very short version:
[HTML]
Copy Code code as follows:

$ (' #myDiv '). CSS ({top: ' 50% ', left: ' 50% ', margin: '-' + ($ (' #myDiv '). Height ()/2) + ' px 0 0-' + (' #myDiv '). Width ()/2) + ' px ' });
$ (' #myDiv '). CSS ({top: ' 50% ', left: ' 50% ', margin: '-' + ($ (' #myDiv '). Height ()/2) + ' px 0 0-' + (' #myDiv '). Width ()/2) + ' px ' });

Short version:
[HTML]
Copy Code code 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;
$ (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;
$ (this). css ({position: ' absolute ', margin:0, Top: (Top > 0? top:0) + ' px ', left: (Left > 0? left:0) + ' px '});
});
}
});
}) (JQuery);

Activated by this code:
Copy Code code as follows:

$ (' #mainDiv '). Center ();

[JavaScript]
Copy Code code 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 $ (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 $ (this). CSS (props);
return $ (this);
});
}
});
}) (JQuery);

PLUGIN VERSION
[JavaScript]
Copy Code code 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 $ (this). CSS (props);
return $ (this);
});
}
});
}) (JQuery);

Copy Code code 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 $ (this). CSS (props);
return $ (this);
});
}
});
}) (JQuery);

Activated by this code:
Copy Code code as follows:

$ (document). Ready (function () {
$ (' #mainDiv '). Center ();
$ (window). Bind (' Resize ', function () {
$ (' #mainDiv '). Center ({transition:300});
});
);

View this person JS code, let the person is breathtaking.
Concise and clear. but also extrapolate.

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.