Copy codeThe Code is as follows:
/**
* JQuery rposition
* Fix: the element to be located.
* Rel: relative positioning Element
* Options: {}, align: Left-right alignment; vlign: vertical alignment; xleft: horizontal replenishment value; xleft: vertical replenishment value; adjust: whether the current window is automatically adjusted; rwindow: locates the center vertical or horizontally relative to the window (when align & vlign is center );
*/
(Function (win, $ ){
Win. rposition = function (fix, rel, options ){
Var rectLeft, rectTop, rectH = fix. outerHeight (), rectW = fix. outerWidth (), wh = $ (window ). height (), ww = $ (window ). width (),
ST = $ (document). scrollTop (), sL = $ (document). scrollLeft (),
Defaults = {
Align: "left ",
Vlign: "top ",
Xleft: 0,
Xtop: 0,
Adjust: true,
Rwindow: false
},
Options = $. extend (defaults, options );
Var rectRel = {
L: $ (rel). offset (). left,
T: $ (rel). offset (). top,
W: $ (rel). outerWidth (),
H: $ (rel). outerHeight ()
};
Switch (options. align ){
Case "left ":
RectLeft = rectRel. l; break;
Case "right ":
RectLeft = rectRel. l + rectRel. w; break;
Case "center ":
RectLeft = rectRel. l + rectRel. w/2; break;
Case "rleft ":
RectLeft = rectRel. l-rectW; break;
Default:
RectLeft = rectRel. l;
};
Switch (options. vlign ){
Case "top ":
RectTop = rectRel. t; break;
Case "center ":
RectTop = rectRel. t + rectRel. h/2; break;
Case "vbottom ":
RectTop = rectRel. t-rectH; break;
Case "bottom ":
Default:
RectTop = rectRel. t + rectRel. h;
};
If (options. rwindow ){
If (options. align = "center") rectLeft = (ww-rectW)/2 + sL;
If (options. vlign = "center") rectTop = (wh-rectH)/2 + sT;
};
If (options. adjust ){
If (rectLeft + rectW> ww + sL) {rectLeft-= (rectLeft + rectW)-(ww + sL )}
If (rectTop + rectH> wh + sT) {rectTop = rectRel. t-rectH ;}
};
Optional (fix).css ({"left": rectLeft + options. xleft, "top": rectTop + options. xtop });
}
}) (Window, jQuery)