# 工具方法

//获取节点的 style 属性值
Layui.prototype.getStyle = function(node, name){
  var style = node.currentStyle ? node.currentStyle : win.getComputedStyle(node, null);
  return style[style.getPropertyValue ? 'getPropertyValue' : 'getAttribute'](name);
};
//图片预加载
Layui.prototype.img = function(url, callback, error) {   
  var img = new Image();
  img.src = url; 
  if(img.complete){
    return callback(img);
  }
  img.onload = function(){
    img.onload = null;
    typeof callback === 'function' && callback(img);
  };
  img.onerror = function(e){
    img.onerror = null;
    typeof error === 'function' && error(e);
  };  
};
//遍历
Layui.prototype.each = function(obj, fn){
  var key
  ,that = this
  ,callFn = function(key, obj){ //回调
    return fn.call(obj[key], key, obj[key])
  };
  
  if(typeof fn !== 'function') return that;
  obj = obj || [];
  
  //优先处理数组结构
  if(that.isArray(obj)){
    for(key = 0; key < obj.length; key++){
      if(callFn(key, obj)) break;
    }
  } else {
    for(key in obj){
      if(callFn(key, obj)) break;
    }
  }
  
  return that;
}