var CSS = { getStyle:function(elem,name){//获取样式 if(elem.style[name]){ return elem.style[name]; } else if(elem.currentStyle){ return elem.currentStyle[name]; } else if(document.defaultView && document.defaultView.getComputedStyle){ name = name.replace(/([A-Z])/g,"-$1"); name = name.toLowerCase(); var s = document.defaultView.getComputedStyle(elem,""); return s && s.getPropertyValue(name); } else{ return null; } }, getPageX:function(elem){//元素相对于整个文档的X位置 return elem.offsetParent? elem.offsetLeft + CSS.getPageX(elem.offsetParent) : elem.offsetLeft; }, getPageY:function(elem){//元素相对于整个文档的Y位置 return elem.offsetParent? elem.offsetTop + CSS.getPageY(elem.offsetParent) : elem.offsetTop; }, getParentX:function(elem){//元素相对于父亲的X位置 return elem.parentNode === elem.offsetParent ? elem.offsetLeft : CSS.getPageX(elem) - CSS.getPageX(elem.parentNode); }, getParentY:function(elem){//元素相对于父亲的Y位置 return elem.parentNode === elem.offsetParent ? elem.offsetTop : CSS.getPageY(elem) - CSS.getPageY(elem.parentNode); }, getPosX:function(elem){//元素相对于它的CSS容器的X位置 return parseInt(CSS.getStyle(elem,"left")); }, getPosY:function(elem){//元素相对于它的CSS容器的Y位置 return parseInt(CSS.getStyle(elem,"top")); }, setOpacity:function(elem,level){ if(elem.filters){ elem.style.filters = ‘alpha(opacity=‘+level+‘)‘; }else{ elem.style.opacity = level/100; } } };
获取样式的简单的函数封装
时间: 2024-10-13 09:52:04