//跨浏览器添加事件 /* function addEvent(obj, type, fn){ if(typeof obj.addEventListener != ‘undefined‘){ obj.addEventListener(type, fn, false); }else if(typeof obj.attachEvent != ‘undefined‘){ obj.attachEvent(‘on‘ + type, function(){ fn.call(obj,window.event); }); } } */ //浏览器监测 (function(){ window.sys = {}; //外部访问浏览器信息对象; var ua = navigator.userAgent.toLowerCase(); //浏览器信息字符串 var s; //浏览器名称加版本 (s = ua.match(/msie ([\d.]+)/)) ? sys.ie = s[1] : (s = ua.match(/firefox\/([\d.]+)/)) ? sys.firefox = s[1] : (s = ua.match(/chrome\/([\d.]+)/)) ? sys.chrome = s[1] : (s = ua.match(/opera\/.*version\/([\d.]+)/)) ? sys.opera = s[1] : (s = ua.match(/version\/([\d.]+).*safari/)) ? sys.safari = s[1] : 0 ; if(/webkit/.test(ua)) sys.webkit = ua.match(/webkit\/([\d.]+)/)[1]; //alert(sys.webkit); })(); //dom加载 function addDomLoaded(fn){ var isReady = false; var timer = null; function doReady(){ if(timer){ clearInterval(timer); } if(isReady){ return; } isReady = true; fn(); } if((sys.opera && sys.opera < 9) || (sys.firefox < 3) || (sys.webkit && sys.webkit < 525)){ timer = setInterval(function(){ if(document && document.getElementById && document.getElementsByTagName && document.body){ doReady(); } },1); }else if(document.addEventListener){ addEvent(document, ‘DOMContentLoaded‘, function(){ fn(); removeEvent(document, ‘DOMContentLoaded‘, arguments.callee); }); }else if(sys.ie && sys.ie < 9){ var timer = null; timer = setInterval(function(){ try{ document.documentElement.doScroll(‘left‘); doReady(); }catch(e){}; },1); } } function addEvent(obj, type, fn){ if(typeof obj.addEventListener != ‘undefined‘){ obj.addEventListener(type, fn, false); }else{ if(!obj.events)obj.events = {}; if(!obj.events[type]){ obj.events[type]=[]; if(obj[‘on‘ + type]) obj.events[type][0] = fn; }else{ if(addEvent.equal(obj.events[type], fn)) return false; } obj.events[type][addEvent.ID++] = fn; obj[‘on‘ + type] = addEvent.exec; } } addEvent.ID = 1; addEvent.exec = function (event) { var e = event || addEvent.fixEvent(window.event); var es = this.events[e.type]; for(var i in es){ es[i].call(this, e); }; } addEvent.equal = function(es, fn){ for(var i in es){ if(es[i] == fn)return true } return false; } addEvent.fixEvent = function(event){ event.preventDefault = addEvent.fixEvent.preventDefault; event.stopPropagation = addEvent.fixEvent.stopPropagation; event.target = event.srcElement; return event; } addEvent.fixEvent.preventDefault = function(){ this.returnValue = false; } addEvent.fixEvent.stopPropagation = function(){ this.cancelBubble = true; } //跨浏览器删除事件 /* function removeEvent(obj, type, fn){ if(typeof obj.removeEventListener != ‘undefined‘){ obj.removeEventListener(type, fn, false); }else if(typeof obj.detachEvent != ‘undefined‘){ obj.detachEvent(‘on‘+type, fn); } } */ function removeEvent(obj, type, fn){ if(typeof obj.removeEventListener != ‘undefined‘){ obj.removeEventListener(type, fn, false); }else{ if(obj.events){ for(var i in obj.events[type]){ if(obj.events[type][i] == fn){ delete obj.events[type][i]; } } } } } //跨浏览器获取视口大小 function getInner(){ if(typeof window.innerWidth != ‘undefined‘){ return { width:window.innerWidth, height:window.innerHeight } }else{ return { width:document.documentElement.clientWidth, height:document.documentElement.clientHeight } } } function getScroll(){ return { top:document.documentElement.scrollTop || document.body.scrollTop, left:document.documentElement.scrollLeft || document.body.scrollLeft } } //获取STYLE function getStyle(element, attr){ var value; if(typeof window.getComputedStyle != ‘undefined‘){ value = window.getComputedStyle(element,null)[attr]; }else if(typeof element.currentStyle != ‘undefined‘){ value = element.currentStyle[attr]; } return value; } //判断class是否存在 function hasClass(element, className){ return element.className.match(new RegExp(‘(\\s|^)‘+className+‘(\\s|$)‘)) } //添加LINK规则 function insertRule(sheet, selectorText,cssText, postion){ if(typeof sheet.insertRule != ‘undefined‘){ sheet.insertRule(selectorText + ‘{‘ + cssText + ‘}‘, 0); }else if(typeof sheet.addRule != ‘undefined‘){ sheet.addRule(selectorText,cssText, postion); } } function deleteRule(sheet, index){ if(typeof sheet.deleteRule != ‘undefined‘){ sheet.deleteRule(index); }else if(typeof sheet.removeRule !=‘undefined‘){ sheet.removeRule(index); } } function getInnerText(element){ return (typeof element.textContent == ‘string‘) ? element.textContent : element.innerText; } function setInnerText(element, text){ if(typeof element.textContent == ‘string‘){ element.textContent = text; }else{ element.innerText = text; } } function offsetTop(element){ var top = element.offsetTop; var parent = element.offsetParent; while(parent != null){ top += parent.offsetTop; parent = parent.offsetParent; } return top; } //删除左右空格 function trim(str){ return str.replace(/(^\s*)|(\s*$)/g, ‘‘); } function inArray(array, value){ for(var i in array){ if(array[i] === value){ return true; } } return false; } function predef(e){ e.preventDefault(); } function prevIndex(current, parent){ var length = parent.children.length; if(current == 0) return length-1; return parseInt(current) - 1; } function nextIndex(current, parent){ var length = parent.children.length; if(current == length - 1) return 0; return parseInt(current) + 1; } function fixedScroll(){ //setTimeout(function(){ window.scrollTo(fixedScroll.left, fixedScroll.top); //},100); } /* function scrollTop(){ document.documentElement.scrollTop = 0; document.body.scrollTop = 0; }*/ //阻止默认行为 function predef(e) { e.preventDefault(); } //创建cookie function setCookie(name, value, expires, path, domain, secure) { var cookieText = encodeURIComponent(name) + ‘=‘ + encodeURIComponent(value); if (expires instanceof Date) { cookieText += ‘; expires=‘ + expires; } if (path) { cookieText += ‘; expires=‘ + expires; } if (domain) { cookieText += ‘; domain=‘ + domain; } if (secure) { cookieText += ‘; secure‘; } document.cookie = cookieText; } //获取cookie function getCookie(name) { var cookieName = encodeURIComponent(name) + ‘=‘; var cookieStart = document.cookie.indexOf(cookieName); var cookieValue = null; if (cookieStart > -1) { var cookieEnd = document.cookie.indexOf(‘;‘, cookieStart); if (cookieEnd == -1) { cookieEnd = document.cookie.length; } cookieValue = decodeURIComponent(document.cookie.substring(cookieStart + cookieName.length, cookieEnd)); } return cookieValue; } //删除cookie function unsetCookie(name) { document.cookie = name + "= ; expires=" + new Date(0); }
时间: 2024-10-23 18:10:17