*fix:要定位的元素
*rel:相对定位的元素
*options:{},align:左右对齐;vlign:垂直对齐;xleft:水平补给值;xleft:垂直补给值;adjust:是否相对窗口自动调整;rwindow:定位相对于窗口(align&vlign为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;}
};
$(fix).css教程({"left":rectleft+options.xleft,"top":recttop+options.xtop});
}
})(window,jquery)