- 在线时间
- 983 小时
- 专家
- 0
- UID
- 281860
- 注册时间
- 2006-8-7
- 帖子
- 2057
- 精华
- 1
- 积分
- 5862
- 居住地
- 浙江省 杭州市
- 离线
- 20 天
专长: 前端制作,Javascript编程,ASP
- 帖子
- 2057
- 体力
- 5637
- 威望
- 45
- 居住地
- 浙江省 杭州市
|
回复 94# liuli80 的帖子
这个问题已经修复了,而且把AJAX的导航也添加进去了
我对AJAX加载路径的处理
- <div id="YNormal-Tabs">
- <div id="YN-Tabs">
- <ul>
- <li class="current"><a href="js/dom" rel="dom"><span>DOM 编程艺术</span></a></li>
- <li><a href="js/jsd" rel="jsd"><span>Javascript 设计模式</span></a></li>
- <li><a href="js/jgp" rel="jgp"><span>Javascript 语言精粹</span></a></li>
- <li><a href="css/cdg" rel="cdg"><span>CSS 权威指南</span></a></li>
- <li><a href="css/cll" rel="cll"><span>CSS 布局实录</span></a></li>
- </ul>
- </div>
- <div id="YN-Cnt">
- <ul>
- <li><strong>1.技术版面请勿灌水。(5楼之后单纯顶贴的,视为灌水。)</strong></li>
- <li>2.原则上不代劳商业应用(江湖救急除外),不解答考试及作业题(作弊可耻,偷懒有罪)。</li>
- <li>3.如需要收藏主题,请使用主题右上角的链接;对论坛使用有疑问时请查看[论坛规则]</li>
- <li>4.如需要收藏主题,请使用主题右上角的链接;对论坛使用有疑问时请查看[论坛规则]</li>
- <li>5.本版严重鄙视以下类型的行业内滥竽充数者:自己有脑,不肯思考;发狠提问,呲牙咧嘴;</li>
- <li>6.本版严重鄙视以下类型的行业内滥竽充数者:自己有脑,不肯思考;发狠提问,呲牙咧嘴;</li>
- <li>7.技术版面请勿灌水。(5楼之后单纯顶贴的,视为灌水。)</li>
- <li>8.原则上不代劳商业应用(江湖救急除外),不解答考试及作业题(作弊可耻,偷懒有罪)。</li>
- <li>9.如需要收藏主题,请使用主题右上角的链接;对论坛使用有疑问时请查看[论坛规则]</li>
- <li>10.如需要收藏主题,请使用主题右上角的链接;对论坛使用有疑问时请查看[论坛规则]</li>
- </ul>
- </div>
- </div>
复制代码
这里的
是为了确保页面不支持JS,用户仍然可以访问的这个栏目的地址或者某个页面的地址
这个则是你AJAX加载的文件的名字或则访问地址的参数,而完整的地址是这么获得的
- {
- aPath: 'samples/.htm/?id=' // 配置的一个属性
- }
- 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);
- }
复制代码
通过uriData = this.aPath.split('/');,获得需要的参数uriData[0] = samples,实际的AJAX文件的目录路径,ajaxLink.rel = dom文件名,uriData[1] = .htm 文件的扩展名 uriData[2] = ?id= 参数名称,ajaxLink.rel同时又是参数参数
所以完整的路径url = 'samples/dom.htm?id=dom' 如果你的是php的,那么可能就是'samples/dom.php?id=dom'我想这样的文件路径形式,基本可以适应通常的开发了,如果你的文件路径确实很特殊,那我只好说,你需要发挥你的聪明才智,自己去组合了。
OK,到今天,我的YTabs的基本功能就全部定下来了
完整代码:
- var YAO = function(){
- var A = '[object Array]', D = document, IE = navigator.userAgent.match(/MSIE\s([^;]*)/), nt = "nodeType", OP = Object.prototype;
-
- return {
- isArray: function(s){
- return OP.toString.apply(s) === A;
- },
- isString: function(s){
- return typeof s === 'string';
- },
- 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;
- }
- },
- get: function(elem){
- 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.get(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 (this.hasClass(elem, className)) {
- return;
- }
- elem.className = [elem.className, className].join(" ");
- },
- removeClass: function(elem, className){
- var replace = new RegExp("(?:^|\\s+)" + className + "(?:\\s+|$)", "g");
- if (!this.hasClass(elem, className)) {
- return;
- }
- var o = elem.className;
- elem.className = o.replace(replace, " ");
- if (this.hasClass(elem, className)) {
- this.removeClass(elem, className);
- }
- },
- replaceClass: function(m, k, j){
- if (k === j) {
- return false;
- }
- var l = new RegExp("(?:^|\\s+)" + k + "(?:\\s+|$)", "g");
- if (!this.hasClass(m, k)) {
- this.addClass(m, j);
- return;
- }
- m.className = m.className.replace(l, " " + j + " ");
- if (this.hasClass(m, k)) {
- this.replaceClass(m, k, j);
- }
- },
- getByClassName: function(className, tag, rootTag){
- var elems = [], i, tempCnt = D.getElementById(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 (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.get(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.get(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 - finalY) / 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.get(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.simpleTab = function(oConfigs){
- this.tabCnt = (oConfigs.tabId) ? YAO.get(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.isPause = false;
- this.speed = oConfigs.speed || 6000;
- this.fadeUp = oConfigs.fadeUp || false;
- this.scroll = oConfigs.scroll || false;
- this.scrollId = oConfigs.scrollId || null;
- this.scrollSpeed = oConfigs.scrollSpeed || 5;
- this.ajax = oConfigs.ajax || false;
- this.aPath = oConfigs.aPath || '';
-
- var i, that = this;
- if (this.auto) {
- this.timer = setTimeout(function(){
- that.autoChange();
- }, that.speed);
- }
- if (this.tabs && this.contents) {
- 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';
- }
- }
- this.tabs[i]['on' + 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);
- this.tabs[i]['onmouseout'] = 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.speed);
- }
- else {
- this.timer = setTimeout(function(){
- that.autoChange()
- }, this.speed);
- return false;
- }
- },
- setCurrent: function(curTab, curCnt, index){
- var currentImage = null, itemHeight = 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";
- }
- currentImage = (curCnt.tagName.toUpperCase() === 'IMG') ? curCnt : curCnt.getElementsByTagName('img')[0];
- if (this.fadeUp) {
- if (this.lastContent !== curCnt) {
- YAO.fadeUp(currentImage);
- }
- }
- else {
- if (this.scroll) {
- itemHeight = currentImage.offsetHeight;
- scrollHeight = -(index * itemHeight);
- YAO.moveElement(this.scrollId, 0, 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-7 09:22 编辑 ] |
附件: 你需要登录才可以下载或查看附件。没有帐号?注册
|