- 在线时间
- 983 小时
- 专家
- 0
- UID
- 281860
- 注册时间
- 2006-8-7
- 帖子
- 2057
- 精华
- 1
- 积分
- 5861
- 居住地
- 浙江省 杭州市
- 离线
- 8 天
专长: 前端制作,Javascript编程,ASP
- 帖子
- 2057
- 体力
- 5636
- 威望
- 45
- 居住地
- 浙江省 杭州市
|
发表于 2009-7-10 09:14:12
|显示全部楼层
调整了下YAO库文件,然后修来了原来的speed为autoSpeed
这样更直观些吧,完整的代码:
- var YAO = function(){
- var D = document, OA = '[object Array]', FC = "[object Function]", OP = Object.prototype, nt = "nodeType", listeners = [], unloadListeners = [], webkitKeymap = {
- 63232: 38, // up
- 63233: 40, // down
- 63234: 37, // left
- 63235: 39, // right
- 63276: 33, // page up
- 63277: 34, // page down
- 25: 9 // SHIFT-TAB (Safari provides a different key code in
- };
-
- return {
- isArray: function(obj){
- return OP.toString.apply(obj) === OA;
- },
- isString: function(s){
- return typeof s === 'string';
- },
- isBoolean: function(b){
- return typeof b === "boolean";
- },
- isFunction: function(func){
- return OP.toString.apply(func) === FC;
- },
- isNull: function(obj){
- return obj === null;
- },
- isNumber: function(num){
- return typeof num === "number" && isFinite(num);
- },
- isObject: function(str){
- return (str && (typeof str === "object" || this.isFunction(str))) || false;
- },
- isUndefined: function(obj){
- return typeof obj === "undefined";
- },
- hasOwnProperty: function(obj, prper){
- if (OP.hasOwnProperty) {
- return obj.hasOwnProperty(prper);
- }
- return !this.isUndefined(obj[prper]) && obj.constructor.prototype[prper] !== obj[prper];
- },
-
- ua: function(){
- var C = {
- ie: 0,
- opera: 0,
- gecko: 0,
- webkit: 0,
- mobile: null,
- air: 0,
- caja: 0
- }, B = navigator.userAgent, A;
- if ((/KHTML/).test(B)) {
- C.webkit = 1;
- }
- A = B.match(/AppleWebKit\/([^\s]*)/);
- if (A && A[1]) {
- C.webkit = parseFloat(A[1]);
- if (/ Mobile\//.test(B)) {
- C.mobile = "Apple";
- }
- else {
- A = B.match(/NokiaN[^\/]*/);
- if (A) {
- C.mobile = A[0];
- }
- }
- A = B.match(/AdobeAIR\/([^\s]*)/);
- if (A) {
- C.air = A[0];
- }
- }
- if (!C.webkit) {
- A = B.match(/Opera[\s\/]([^\s]*)/);
- if (A && A[1]) {
- C.opera = parseFloat(A[1]);
- A = B.match(/Opera Mini[^;]*/);
- if (A) {
- C.mobile = A[0];
- }
- }
- else {
- A = B.match(/MSIE\s([^;]*)/);
- if (A && A[1]) {
- C.ie = parseFloat(A[1]);
- }
- else {
- A = B.match(/Gecko\/([^\s]*)/);
- if (A) {
- C.gecko = 1;
- A = B.match(/rv:([^\s\)]*)/);
- if (A && A[1]) {
- C.gecko = parseFloat(A[1]);
- }
- }
- }
- }
- }
- A = B.match(/Caja\/([^\s]*)/);
- if (A && A[1]) {
- C.caja = parseFloat(A[1]);
- }
- return C;
- }(),
-
- addListener: function(el, event, func){
- try {
- el.addEventListener(event, func, false);
- }
- catch (e) {
- try {
- el.attachEvent('on' + event, func);
- }
- catch (e) {
- el['on' + event] = func;
- }
- }
- if ("unload" == event) {
- unloadListeners[unloadListeners.length] = [el, event, func];
- return true;
- }
- else {
- listeners[listeners.length] = [el, event, func];
- }
- },
- removeListener: function(el, event, func){
- var i,uli,j,li;
- try {
- el.removeEventListener(event, func, false);
- }
- catch (e) {
- try {
- el.detachEvent('on' + event, func);
- }
- catch (e) {
- el['on' + event] = null;
- }
- }
- if ("unload" == event) {
- for (i = unloadListeners.length - 1; i > -1; i--) {
- uli = unloadListeners[i];
- if (uli && uli[0] === el && uli[1] == event && uli[2] == func) {
- unloadListeners.splice(i, 1);
- return true;
- }
- }
- return false;
- }
- else {
- for (j = listeners.length - 1; j > -1; j--) {
- li = listeners[i];
- if (li && li[0] === el && li[1] == event && li[2] == func) {
- delete listeners[j][2];
- listeners.splice(j, 1);
- return true;
- }
- }
- return false;
- }
- },
- on: function(el, event, func){
- YAO.addListener(el, event, func);
- },
- stopEvent: function(evt){
- this.stopPropagation(evt);
- this.preventDefault(evt);
- },
- stopPropagation: function(evt){
- if (evt.stopPropagation) {
- evt.stopPropagation();
- }
- else {
- evt.cancelBubble = true;
- }
- },
- preventDefault: function(evt){
- if (evt.preventDefault) {
- evt.preventDefault();
- }
- else {
- evt.returnValue = false;
- }
- },
- getEvent: function(e){
- var ev = e || window.event;
-
- if (!ev) {
- var c = this.getEvent.caller;
- while (c) {
- ev = c.arguments[0];
- if (ev && Event == ev.constructor) {
- break;
- }
- c = c.caller;
- }
- }
-
- return ev;
- },
- getCharCode: function(ev){
- var code = ev.keyCode || ev.charCode || 0;
-
- // webkit key normalization
- if (YAO.ua.webkit && (code in webkitKeymap)) {
- code = webkitKeymap[code];
- }
- return code;
- },
- _unload: function(e){
- var i, l, len, index, ul = unloadListeners.slice(), context;
-
- // execute and clear stored unload listeners
- for (i = 0, len = unloadListeners.length; i < len; ++i) {
- l = ul[i];
- if (l) {
- context = window;
- l[2].call(context, YAO.getEvent(e, l[0]));
- ul[i] = null;
- }
- }
-
- l = null;
- context = null;
- unloadListeners = null;
-
- if (listeners) {
- for (j = listeners.length - 1; j > -1; j--) {
- l = listeners[j];
- if (l) {
- YAO.removeListener(l[0], l[1], l[2]);
- }
- }
- l = null;
- }
-
- YAO.removeListener(window, "unload", YAO._unload);
- },
-
- getEl: function(elem, boundEl){
- var elemID, E, m, i, k, length, len;
- if (elem) {
- if (elem[nt] || elem.item) {
- return elem;
- }
- if (YAO.isString(elem)) {
- elemID = elem;
- elem = D.getElementById(elem);
- if (elem && elem.id === elemID) {
- return elem;
- }
- else {
- if (elem && elem.all) {
- elem = null;
- E = D.all[elemID];
- for (i = 0, len = E.length; i < len; i += 1) {
- if (E[i].id === elemID) {
- return E[i];
- }
- }
- }
- }
- return elem;
- }
- else {
- if (elem.DOM_EVENTS) {
- elem = elem.get("element");
- }
- else {
- if (YAO.isArray(elem)) {
- m = [];
- for (k = 0, length = elem.length; k < length; k += 1) {
- m[m.length] = YAO.getEl(elem[k]);
- }
- return m;
- }
- }
- }
- }
- return null;
- },
- hasClass: function(elem, className){
- var has = new RegExp("(?:^|\\s+)" + className + "(?:\\s+|$)");
- return has.test(elem.className);
- },
- addClass: function(elem, className){
- if (YAO.hasClass(elem, className)) {
- return;
- }
- elem.className = [elem.className, className].join(" ");
- },
- removeClass: function(elem, className){
- var replace = new RegExp("(?:^|\\s+)" + className + "(?:\\s+|$)", "g");
- if (!YAO.hasClass(elem, className)) {
- return;
- }
- var o = elem.className;
- elem.className = o.replace(replace, " ");
- if (YAO.hasClass(elem, className)) {
- YAO.removeClass(elem, className);
- }
- },
- replaceClass: function(elem, newClass, oldClass){
- if (newClass === oldClass) {
- return false;
- }
- var has = new RegExp("(?:^|\\s+)" + newClass + "(?:\\s+|$)", "g");
- if (!YAO.hasClass(elem, newClass)) {
- YAO.addClass(elem, oldClass);
- return;
- }
- elem.className = elem.className.replace(has, " " + oldClass + " ");
- if (YAO.hasClass(elem, newClass)) {
- YAO.replaceClass(elem, newClass, oldClass);
- }
- },
- getElByClassName: function(className, tag, rootTag){
- var elems = [], i, tempCnt = YAO.getEl(rootTag).getElementsByTagName(tag), len = tempCnt.length;
- for (i = 0; i < len; i++) {
- if (YAO.hasClass(tempCnt[i], className)) {
- elems.push(tempCnt[i]);
- }
- }
- if (elems.length < 1) {
- return false;
- }
- else {
- return elems;
- }
- },
- setOpacity: function(el, val){
- if (YAO.ua.ie) {
- if (YAO.isString(el.style.filter)) {
- el.style.filter = 'alpha(opacity=' + val * 100 + ')';
- if (!el.currentStyle || !el.currentStyle.hasLayout) {
- el.style.zoom = 1;
- }
- }
- }
- else {
- el.style['opacity'] = val;
- }
- },
- YTabs: function(){
- var j, len = arguments.length, sigleTab = [];
- for (j = 0; j < len; ++j) {
- sigleTab[j] = new YAO.simpleTab(arguments[j]);
- }
- },
- fadeUp: function(elem){
- if (elem) {
- var level = 0, fade = function(){
- var timer = null;
- level += 0.05;
- if (timer) {
- clearTimeout(timer);
- timer = null;
- }
- if (level > 1) {
- YAO.setOpacity(elem, 1);
- return false;
- }
- else {
- YAO.setOpacity(elem, level);
- }
- timer = setTimeout(fade, 50);
- };
- fade();
- }
- },
- zebra: function(){
- var j, length = arguments.length;
- for (j = 0; j < length; ++j) {
- (function(config){
- var root = YAO.getEl(config.rootTag) || (config.root || null), rows = root.getElementsByTagName(config.rowTag) || (config.rows || null), i, len = rows.length, lastClass = '';
- if (root && rows && len > 1) {
- for (var i = 0; i < len; ++i) {
- rows[i].className = i % 2 == 0 ? 'even' : 'odd';
- rows[i].onmouseover = function(){
- lastClass = this.className;
- YAO.replaceClass(this, lastClass, 'hover');
- };
- rows[i].onmouseout = function(){
- YAO.replaceClass(this, 'hover', lastClass);
- };
- }
- }
- else {
- return false;
- }
- })(arguments[j]);
- }
- },
- moveElement: function(element, finalX, finalY, speed){
- var elem = YAO.isString(element) ? YAO.getEl(element) : element, style = null;
- if (elem) {
- if (elem.movement) {
- clearTimeout(elem.movement);
- }
- if (!elem.style.left) {
- elem.style.left = "0";
- }
- if (!elem.style.top) {
- elem.style.top = "0";
- }
- var xpos = parseInt(elem.style.left);
- var ypos = parseInt(elem.style.top);
- if (xpos == finalX && ypos == finalY) {
- return true;
- }
- if (xpos < finalX) {
- var dist = Math.ceil((finalX - xpos) / 10);
- xpos = xpos + dist;
- }
- if (xpos > finalX) {
- var dist = Math.ceil((xpos - finalX) / 10);
- xpos = xpos - dist;
- }
- if (ypos < finalY) {
- var dist = Math.ceil((finalY - ypos) / 10);
- ypos = ypos + dist;
- }
- if (ypos > finalY) {
- var dist = Math.ceil((ypos - finalY) / 10);
- ypos = ypos - dist;
- }
- elem.style.left = xpos + "px";
- elem.style.top = ypos + "px";
- elem.movement = setTimeout(function(){
- YAO.moveElement(element, finalX, finalY, speed);
- }, speed);
- }
- },
- ajaxRequest: function(url, id, sload){
- var oXhr, elem = YAO.isString(id) ? YAO.getEl(id) : id, sLoading = sload || '正在获取数据,请稍后...';
- if (!url || !id) {
- return;
- }
- if (window.XMLHttpRequest) {
- oXhr = new XMLHttpRequest();
- }
- else {
- if (window.ActiveXObject) {
- oXhr = new ActiveXObject("Microsoft.XMLHTTP");
- }
- }
- if (oXhr !== null) {
- try {
- oXhr.open('GET', url, true);
- oXhr.onreadystatechange = function(){
- if (oXhr.readyState == 4) {
- if (oXhr.status == 200 || location.href.indexOf('http') === -1) {
- elem.innerHTML = oXhr.responseText;
- }
- else {
- elem.innerHTML = sLoading;
- }
- }
- };
- oXhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
- oXhr.send(null);
- }
- catch (e) {
- throw new Error(e);
- return false;
- }
- }
- else {
- throw new Error("Your browser does not support XMLHTTP.");
- return false;
- }
- }
- };
-
- YAO.on(window, "unload", YAO._unload);
- }();
- YAO.simpleTab = function(oConfigs){
- this.tabCnt = (oConfigs.tabId) ? YAO.getEl(oConfigs.tabId) : (oConfigs.tabRoot || null);
- this.tabs = (oConfigs.tTag) ? this.tabCnt.getElementsByTagName(oConfigs.tTag) : (oConfigs.tabs || null);
- this.contents = (oConfigs.cTag) ? this.tabCnt.getElementsByTagName(oConfigs.cTag) : (oConfigs.contents || null);
- this.length = this.tabs.length || 0;
- this.defaultIndex = oConfigs.defaultIndex || 0;
- this.lastIndex = this.defaultIndex;
- this.lastTab = this.tabs[this.lastIndex] || null;
- this.lastContent = this.contents[this.lastIndex] || null;
- this.evtName = oConfigs.evt || 'mouseover';
- this.defaultClass = oConfigs.defaultClass || 'current';
- this.previousClass = oConfigs.previousClass || '';
- this.hideAll = oConfigs.hideAll || false;
- this.auto = oConfigs.auto || false;
- this.autoSpeed = oConfigs.autoSpeed || 6000;
- this.fadeUp = oConfigs.fadeUp || false;
- this.scroll = oConfigs.scroll || false;
- this.scrollId = oConfigs.scrollId || null;
- this.scrollSpeed = oConfigs.scrollSpeed || 5;
- this.direction = oConfigs.direction || 'V';
- this.activeTag = oConfigs.activeTag || 'IMG';
- this.ajax = oConfigs.ajax || false;
- this.aPath = oConfigs.aPath || '';
-
- var i, that = this;
- if (this.tabs && this.contents) {
- if (this.auto) {
- this.timer = setTimeout(function(){
- that.autoChange();
- }, that.autoSpeed);
- }
- if (!this.hideAll) {
- YAO.addClass(this.lastTab, this.defaultClass);
- if (!this.ajax) {
- this.lastContent.style.display = 'block';
- }
- else{
- this.ajaxTab(this.lastTab);
- }
- }
- else {
- YAO.removeClass(this.lastTab, this.defaultClass);
- if (!this.scroll) {
- this.lastContent.style.display = 'none';
- }
- }
- for (i = 0; i < this.length; ++i) {
- if (i !== this.defaultIndex && !this.scroll) {
- YAO.removeClass(this.tabs[i], 'current');
- if (!this.ajax) {
- this.contents[i].style.display = 'none';
- }
- }
- YAO.on(this.tabs[i],this.evtName, function(index){
- return function(event){
- var evt = null, currentContent = that.ajax ? that.contents : that.contents[index], currentTab = this;
- that.setCurrent.call(that, this, currentContent, index);
- that.lastIndex = index;
- if (that.auto) {
- that.isPause = true;
- }
- evt = event || window.event;
- YAO.stopEvent(evt);
- }
- }(i));
- YAO.on(this.tabs[i],'mouseout', function(index){
- return function(){
- if (that.hideAll && that.evtName === 'mouseover') {
- if (that.lastTab === this) {
- YAO.removeClass(this, (YAO.hasClass(this, that.defaultClass) ? that.defaultClass : 'current'));
- }
- if (that.previousClassTab) {
- YAO.removeClass(that.previousClassTab, that.previousClass);
- }
- if (!that.scroll && !that.ajax) {
- that.contents[index].style.display = 'none';
- }
- }
- else {
- if (that.auto) {
- that.isPause = false;
- }
- }
- }
- }(i));
- }
- }
- };
- YAO.simpleTab.prototype.timer = null;
- YAO.simpleTab.prototype.isPause = false;
- YAO.simpleTab.prototype = {
- autoChange: function(){
- var that = this;
- if (!this.isPause) {
- var currentContent = null, currentTab = null;
- if (this.timer) {
- clearTimeout(this.timer);
- this.timer = null;
- }
- this.lastIndex = this.lastIndex + 1;
- if (this.lastIndex === this.length) {
- this.lastIndex = 0;
- }
- currentContent = this.ajax ? this.contents : this.contents[this.lastIndex];
- currentTab = this.tabs[this.lastIndex];
- this.setCurrent(currentTab, currentContent, this.lastIndex);
- this.timer = setTimeout(function(){
- that.autoChange();
- }, this.autoSpeed);
- }
- else {
- this.timer = setTimeout(function(){
- that.autoChange()
- }, this.autoSpeed);
- return false;
- }
- },
- setCurrent: function(curTab, curCnt, index){
- var activeObj = null, itemHeight = 0, itemWidth = 0, scrollWidth = 0, scrollHeight = 0;
- YAO.removeClass(this.lastTab, (YAO.hasClass(this.lastTab, this.defaultClass) ? this.defaultClass : 'current'));
- if (curTab === this.tabs[this.defaultIndex]) {
- YAO.addClass(curTab, this.defaultClass);
- }
- else {
- YAO.addClass(curTab, 'current');
- }
- if (this.previousClass) {
- if (this.previousClassTab) {
- YAO.removeClass(this.previousClassTab, this.previousClass);
- }
- if (index !== 0) {
- YAO.addClass(this.tabs[index - 1], this.previousClass);
- if ((index - 1) === this.defaultIndex) {
- YAO.removeClass(this.tabs[index - 1], this.defaultClass);
- }
- this.previousClassTab = (this.tabs[index - 1]);
- }
- }
- if (!this.scroll && !this.ajax) {
- this.lastContent.style.display = "none";
- curCnt.style.display = "block";
- }
-
- if (this.fadeUp) {
- activeObj = (curCnt.tagName.toUpperCase() === 'IMG') ? curCnt : curCnt.getElementsByTagName('img')[0];
- if (this.lastContent !== curCnt) {
- YAO.fadeUp(activeObj);
- }
- }
- else {
- if (this.scroll) {
- activeObj = (curCnt.tagName.toUpperCase() === this.activeTag) ? curCnt : curCnt.getElementsByTagName(this.activeTag)[0];
- if(this.direction === 'V'){
- itemHeight = activeObj.offsetHeight;
- scrollHeight = -(index * itemHeight);
- }
- else{
- itemWidth = activeObj.offsetWidth;
- scrollWidth = -(index * itemWidth);
- }
- YAO.moveElement(this.scrollId, scrollWidth, scrollHeight, this.scrollSpeed);
- }
- }
- if (!this.ajax) {
- this.lastContent = curCnt;
- }
- else {
- this.ajaxTab(curTab);
- }
- this.lastTab = curTab;
- },
- ajaxTab: function(curTab){
- var url = '', ajaxLink = null, cnt = this.contents, uriData = this.aPath.split('/');
- ajaxLink = (curTab.tagName.toUpperCase() === 'A') ? curTab : curTab.getElementsByTagName('a')[0];
- url = uriData[0] + '/' + ajaxLink.rel + uriData[1] + uriData[2] + ajaxLink.rel;
- YAO.ajaxRequest(url, cnt, cnt.innerHTML);
- }
- };
复制代码
[ 本帖最后由 yaohaixiao 于 2009-7-10 09:19 编辑 ] |
|