www.gusucode.com > 叶子美容网图文下拉导航菜单源码程序 > 叶子美容网图文下拉导航菜单/js/tools.js
/** * * * */ if(typeof console === 'undefined'){ console = { log : function(){} }; } ;(function(){ Function.prototype.method = function(name,func){ if(!this.prototype[name]){ this.prototype[name] = func; } }; if( !window.XMLHttpRequest && window.ActiveXObject){ try{ document.execCommand('BackgroundImageCache', false, true); }catch (e){ }; } /* * String */ var str = { rgbtoHex : function(){ var s = this.match(/\d{1,3}/g); if(!s) return null; if(s.length == 4 && s[3]==0) return 'transparent'; var result = []; for(var i=0,l=s.length;i<l;i++){ s[i] = (s[i]-0).toString(16); result.push(s[i].length==1 ? '0'+s[i] : s[i]); } return '#'+result.join(''); }, camelCase : function(){ return this.replace(/-\D/g,function(m){return m.charAt(1).toUpperCase()}) }, hyphenate : function(){ return this.replace(/[A-Z]/g,function(m){return '-'+m.charAt(0).toLowerCase()}) } }; for(var key in str){ if(str.hasOwnProperty(key)){ String.method(key,str[key]); } } /* * Array */ })(); /* * Element */ 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){ 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]) || !parseInt(values[key]) || /(scroll)|(index)/i.test(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; } } } /* * Anima */ function Anima(id,options){ var opts, obj, step, timer, transition, begin, current, end, style = { name : [], from : [], to : [] }, complete; function init(opt){ opts = opt || {}; obj = typeof id === 'string' ? document.getElementById(id) : id; step = parseInt((opts.time || 500)); timer = null; transition = opts.trans || '1'; begin = 0; current = 0; end = 0; style = { name : [], from : [], to : [] }; } init(options); function start(opt){ stop(); style = { name : [], from : [], to : [] }; for(var key in opt){ style.name.push(key.hyphenate()); if(typeof opt[key] === 'object'){ style.from.push(parseFloat(opt[key][0])); style.to.push(parseFloat(opt[key][1])); continue; } var result = Element.getStyle(obj,key); result = typeof result === 'undefind' ? opt[key] : result; style.from.push(result); style.to.push(opt[key]); result = null; } begin = getTime(); current = getTime(); end = begin + step; play(); } function play(){ var m = 0; function move(){ current = getTime(); m = (current - begin)/step; if(m>=1){ m=1; } var str = {}, n=''; for(var i=0,l=style.name.length;i<l;i++){ if(/(rgb)|(#)/i.test(style.from[i])){ var froms = setColor(style.from[i]), tos = setColor(style.to[i]), results = []; for(var j=0,k=froms.length;j<k;j++){ results.push( Math.round(trans((froms[j]-0),(tos[j]-0),m))) } n = results.join(',').rgbtoHex(); }else{ n = parseFloat(trans( parseFloat(style.from[i]),parseFloat(style.to[i]),m)); } str[style.name[i]] = n; } Element.setStyle(obj,str); if(m==1){ stop(); onComplete(); return; } } timer = setInterval(move,15); function trans(f,t,a){ return f + (t-f)*transFunc(a); } function setColor(value){ var result; if(value.indexOf('#')>-1){ value = value.replace(/#/,''); if(value.length==3){ value = value.replace(/(\w)(\w)(\w)/,'$1$1$2$2$3$3'); } result = value.replace(/\w{2}/g,function(m){return parseInt(m.replace(/^0{1}/g,''),16)+','}).replace(/\,$/g,'').split(','); return result; } if(value.indexOf('rgb')>-1){ result = value.match(/\d{1,3}/g); } return result; } } function trans(s){ switch(s){ case '0': transFunc = function(m){return m}; break; case '2': transFunc = function(m){return Math.pow(m, 2) * (2.618 * m - 1.618)}; break; case '3' : transFunc = function(m){ return (m<=0.5) ? Math.pow(m, 2) * (2.618 * m - 1.618) : (1 - Math.pow((1-m),2)*(2.618 * (1-m) - 1.618)); } break; case '1' : default : transFunc = function (m){ return (1-Math.cos(Math.PI*m))/2 }; } } trans(transition); function pause(){ stop(); } function reStart(){ var fix = current - begin; current = getTime(); begin = current - fix; end = begin + step; play(); } function stop(){ if(timer){ clearInterval(timer); timer = null; } } function getTime(){ return (new Date()).getTime(); } function cancel(fn){ stop(); fn && fn(); } function onComplete(){ if(complete){ complete(); complete = null; } } function setComplete(fn){ if(fn) complete = fn; } return { start : start, cancel : cancel, stop : stop, pause : pause, reStart : reStart, complete : setComplete } }; /* * async loader * dom ready **/ (function(){ var loaded = {}, loadingFiles = {}, loadList = {}, mods = {}, isReady = false, readyList = [], f = document.getElementsByTagName('script')[0], y; function load(url,charset,callback){ if(loaded[url]){ loadingFiles[url] = false; callback && callback(url); return; } if(loadingFiles[url]){ setTimeout(function(){ load(url,charset,callback); },10); return; } loadingFiles[url] = true; var n, done = function(){ loaded[url] = 1; callback && callback(url); callback = null; }, t; t = url.toLowerCase().indexOf('.css') > -1 ? 'css' : 'js'; if(t === 'css'){ n = document.createElement('link'); n.setAttribute('rel','stylesheet'); n.setAttribute('type','text/css'); n.setAttribute('href',url); var img = new Image(); img.onerror = function(){ try{ done(); }catch(e){} img.onerror = null; img = null; } img.src = url; }else{ n = document.createElement('script'); n.setAttribute('type','text/javascript'); n.src = url; n.async = true; n.onerror = function(){ console.log(url+' is load fail;'); try{ done(); }catch(e){} n.onerror = null; } } if(charset){ n.charset = charset; } n.readyState ? n.onreadystatechange = function(){ if( /loaded|complete/.test(n.readyState)){ done(); n.onreadystatechange = null; } } : n.onload = function(){ done(); n.onload = null; } f.parentNode.insertBefore(n,f); } function loadDeps(deps,callback){ var mod, len = deps.length, id = deps.join(''); if(loadList[id]){ callback && callback(); return; } function complete(){ if(!--len){ loadList[id] = 1; callback && callback(); } } for(var i=0,l=deps.length;i<l;i++){ if(typeof deps[i] === 'string' ){ mod = (mods[deps[i]]) ? mods[deps[i]] : {path : deps[i]}; }else{ mod = deps[i]; } if(mod.requires){ loadDeps(mod.requires,(function(mod,key){ return function(){ load(mod.path,mod.charset,function(){ if(mod.callback){ mod.callback(); mods[key].callback = null; } complete(); }); } })(mod,deps[i])) }else{ load(mod.path,mod.charset,(function(mod,key){ return function(){ if(mod.callback){ mod.callback(); mods[key].callback = null; } complete(); } })(mod,deps[i])); } } } var loader = function(){ var args = [].slice.call(arguments), fn, id; if(typeof args[args.length-1] === 'function'){ fn = args.pop(); } if( args.length === 0 ){ fn && fn(); return; } id = args.join(''); if(loadList[id]){ fn && fn(); return; } loadDeps(args,function(){ fn && fn(); }) } loader.add = function(name,value){ if(!name || !value){ return; } var _mod = []; if(name === 'mods'){ for(var key in value){ if(value.hasOwnProperty(key) && value[key].path){ mods[key] = value[key]; _mod.push(key); } } }else if( value.path ){ mods[name] = value; _mod.push(name); } loadDeps(_mod); _mod = null; } window['Y'] = loader; /*! * contentloaded.js * Author: Diego Perini (diego.perini at gmail.com) */ function contentLoaded(fn) { var done = false, top = true, win = window, doc = win.document, root = doc.documentElement, add = doc.addEventListener ? 'addEventListener' : 'attachEvent', rem = doc.addEventListener ? 'removeEventListener' : 'detachEvent', pre = doc.addEventListener ? '' : 'on', init = function(e) { if (e.type == 'readystatechange' && doc.readyState != 'complete') return; (e.type == 'load' ? win : doc)[rem](pre + e.type, init, false); if (!done && (done = true)) fn.call(win, e.type || e); }, poll = function() { try { root.doScroll('left'); } catch(e) { setTimeout(poll, 50); return; } init('poll'); }; if (doc.readyState == 'complete') fn.call(win, 'lazy'); else { if (doc.createEventObject && root.doScroll) { try { top = !win.frameElement; } catch(e) { } if (top) poll(); } doc[add](pre + 'DOMContentLoaded', init, false); doc[add](pre + 'readystatechange', init, false); win[add](pre + 'load', init, false); } } contentLoaded(function(){ isReady = true; fireReadyList(); }); function fireReadyList(){ var i=0,len=readyList.length; if(len){ for( ; readyList[i]; i++){ readyList[i](); } } } window['Domready'] = function(fn){ if(isReady){ fn && fn(); return; } readyList.push(fn); } })()