功能的实现思路:
S1 雅虎中国
·单击触发
·<A>块锁定:a{display:block;height:16px;}/*将A锁定为块级,再定义高度和宽度*/
·再次单击弹出链接:在<A>标记的onclick事件中需要执行读取时,return false;否则return true;
·AJAX读取数据
·数据暂存
·窗口平滑收缩
<!--由于我看不懂雅虎的代码,所以使用了一些基本的代码去实现这些功能,可能效率不高,不过效果是出来了.-->
S2 网易
·鼠标滑过延时触发: onmouseover时var waitInterval=window.setTimeout("func();",300);onmouseout时clearTimeout(waitInterval);
·<A>块锁定、AJAX读取数据、数据暂存、窗口平滑收缩
<!--还是网易的代码简单,直接拿来用了-->
综合效果:
http://www.lorlo.com/tab.html
JS文件分析:
程序代码
function getObject(objectId) { //获得DOM对象通用函数
if([code]document.getElementById && document.getElementById(objectId)) {
// W3C DOM
return document.getElementById(objectId);
}
else if (document.all && document.all(objectId)) {
// MSIE 4 DOM
return document.all(objectId);
}
else if (document.layers && document.layers[objectId]) {
// NN 4 DOM.. note: this won't find nested layers
return document.layers[objectId];
}
else {
return false;
}
}
var responsecont;
var xmlHttp;
var requestType;
var newsstring;
var ajccache=new Object(); //定义缓存对象
var url;
var MouseDelayTime=200;//鼠标感应延迟
var waitInterval;
//判别浏览器类型的通用函数
var Browser = new Object();
Browser.isMozilla = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined') && (typeof HTMLDocument!='undefined');
Browser.isIE = window.ActiveXObject ? true : false;
Browser.isFirefox = (navigator.userAgent.toLowerCase().indexOf("firefox")!=-1);
Browser.isOpera = (navigator.userAgent.toLowerCase().indexOf("opera")!=-1);
//ajax相关处理
function CreateXMLHttpRequest(){
// Initialize Mozilla XMLHttpRequest object
if (window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
// Initialize for IE/Windows ActiveX version
else if (window.ActiveXObject) {
try{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP.3.0");
}
catch (e){
try{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){newsstring = "<div class='loading'>Loading rquest content fail, Please try it again latter...</div>";}
}
}
}
function getnews(tagid,x){
url = "tab/"+tagid+'_'+x+'.html';
requestType = tagid;
if(ajccache[url]==null){
CreateXMLHttpRequest();
xmlHttp.onreadystatechange = processRequestChange;
xmlHttp.open("GET", url, true);
xmlHttp.setRequestHeader("If-Modified-Since","0");
xmlHttp.send(null);
}
else
{ shownews(requestType,ajccache[url]); }
}
function processRequestChange(){
// only if xmlHttp shows "complete"
if (xmlHttp.readyState == 4){
// only http 200 to process
if (window.location.href.indexOf("http")==-1 || xmlHttp.status == 200){
newsstring = xmlHttp.responseText;
//inject centent to tab-pane
shownews(requestType,newsstring);
ajccache[url]=newsstring;
}
}
}
function shownews(requestType,newsstring){
//<![CDATA[
responsecont = getObject(requestType+'_cnt');
responsecont.innerHTML = newsstring;
//]]>
}
文章录入:cainiaowang 责任编辑:cainiaowang