jquery 元素相对定位
2010-10-13 11:22
302 查看
/** *jQuery rposition *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)
相关文章推荐
- jQuery学习总结之元素的相对定位和选择器----持续更新中
- jquery的相对父元素和相对文档定位示例代码
- jQuery学习总结之元素的相对定位和选择器(持续更新)
- jquery的相对父元素和相对文档定位总结
- jquery的相对父元素和相对文档定位示例代码
- jquery的相对父元素和相对文档定位示例代码
- jQuery获得页面元素的绝对/相对位置
- 相对定位实际上被看做普通流定位模型的一部分,因为元素的位置相对与它在普通流中的位置
- jQuery中scrollTop获取匹配元素相对滚动条顶部的偏移(此方法对可见和隐藏元素均有效。)
- jQuery获取页面元素绝对与相对位置的方法
- CSS中position的absolute如何相对于父元素的位置进行定位
- javascriptBom中元素的绝对定位以及相对于body的x轴位置和y轴位置定位
- 元素的相对定位与绝对定位
- css基础 绝对定位 position:absolute 相对于document元素进行定位 (无定位的祖先元素)
- jQuery获得页面元素的绝对/相对位置即绝对X,Y坐标
- asp.net-元素定位、显示设置、jQuery监听事件和显示效果
- jquery 设置元素相对于另一个元素的top值(实例代码)
- position属性之相对父元素定位
- appium使用相对坐标定位元素