js常用函数

var Element = {
create : function(){

},
hasClass:function(obj,name){
return (' '+(obj.className || '')+' ').indexOf(' '+name+' ') > -1 ? true : false;
},
addClass : function(obj,name){
if(this.hasClass(obj,name)) return;
obj.className += ' ' + name;
},
removeClass : function(obj,name){
obj.className = obj.className.replace(new RegExp('(^|\\s)' +name+ '(?:\\s|$)'),'$1').replace(/\s{1,}/g,' ');
},
getStyle : function(obj,style){

var result;
if(style == 'padding' || style=='margin'){
result = '';
for(var key in {top:0,right:0,bottom:0,left:0}){
result += Element.getStyle(obj,style+'-'+key) + ' ';
}
result = result.replace(/\s$/,'');
return result;
}
function getComStyle(property){
if(obj.currentStyle) return obj.currentStyle[property.camelCase()];
var computed = window.getComputedStyle(obj, null);
return (computed) ? computed.getPropertyValue(property.hyphenate()) : null;
}
if(style == 'opacity'){
if(window.ActiveXObject && navigator.userAgent.indexOf('MSIE 10.0') == -1){
result = getComStyle('filter').replace(/[^0-9\.]/g,'');
result = result== '' ? 1 : parseInt(result*100)/10000;
return result;
}
result = parseFloat(getComStyle(style));
result = !result && result != 0 ? 1 : result;

return result;
}
style = style.camelCase();

result = obj.style[style];

if(!result&&result!==0){
result = getComStyle(style);
}
if(result){
//if(/rgb/.test(style)){
// resutl = result.rgbtoHex();
//}
if(/^(width)|(height)$/.test(style)){
var path = style == 'width' ? ['left','right'] : ['top','bottom'],
size =0;
size = (parseInt(this.getStyle(obj,'padding-'+path[0])) || 0) + (parseInt(this.getStyle(obj,'padding-'+path[1])) || 0) +
(parseInt(this.getStyle(obj,'border-'+path[0]+'-width')) || 0 ) + (parseInt(this.getStyle(obj,'border-'+path[1]+'-width')) || 0);
result = obj['offset'+style.replace(/\b[a-z]/,function(m){return m.toUpperCase();})]-size;
return result;
}
if(result == 'auto' && style == 'zIndex'){
result = 0;
return result;
}
}
return result;
},
setStyle : function(obj,values){
var str = ';';
for(var key in values){
if(values.hasOwnProperty(key)){
if(key == 'opacity'){
str += key + ':' + values[key] + ';filter:alpha(opacity='+ values[key]*100 +');';
continue;
}
if(/(rgb)|(#)/i.test(values[key])){
str += key +':'+ values[key] + ';';
continue;
}
str += key +':'+ Math.round(values[key]) + 'px;';
}
}
obj.style.cssText += str;
str = null;
return ;
},
getPosition:function(obj){
var o = typeof obj === 'string' ? document.getElementById(obj) : obj,
x=0,
y=0;
while(o){
x+=o.offsetLeft;
y+=o.offsetTop;
o = o.offsetParent;
}
return {x:x,y:y}
},
getChild:function(obj,node){
var o = typeof obj === 'string' ? document.getElementById(obj) : obj,
list = o.childNodes,
nodes = [];
for(var i=0,l=list.length;i<l;i++){
if(node){
if(list[i].nodeName == node.toUpperCase()){
nodes.push(list[i]);
}
}else{
if(list[i].nodeType == 1) nodes.push(list[i])
}
}
o=null;list=null;
return nodes;
}
}


/*
* Event
*/
var Event = {
add : (function(){
if(document.addEventListener){
return function(obj,type,fn){ obj.addEventListener(type,fn,false)}
}
return function(obj,type,fn){ obj.attachEvent('on'+type,fn)}
})(),
remove : (function(){
if(document.removeEventListener){
return function(obj,type,fn){ obj.removeEventListener(type,fn,false)}
}
return function(obj,type,fn){ obj.detachEvent('on'+type,fn)}
})(),
stop:function(e){
if(e&&e.stopPropagation){
e.stopPropagation();
e.preventDefault();
}else{
window.event.cancelBubble = true;
window.event.returnValue = false;
}
}
}

来源:http://p4.yokacdn.com/pic/div/2012/products/index/yokaIndex.js

posted on 2013-04-24 15:21  liuyong1855  阅读(138)  评论(0编辑  收藏  举报

导航