JavaScript瀏覽器選項卡效果
更新時間:2010年08月25日 21:50:06 作者:
項目需要做了如下的東西,希望能給其他需要的同學(xué)們一點參考。
有圖如下:

代碼如下:
/*
head html : <span></span>
body html : <iframe></iframe>
*/
var Tab = function(id,title,url,isClose){
this.id = id;
this.title = title;
this.url = url;
this.head = jQuery('<span class="tab-head">' + this.title +'</span>');
this.body = jQuery('<iframe name="ifm' + this.id +'" src="' + this.url +'" frameborder=0 class="tab-body"></iframe>').hide();
isClose && (this.close = jQuery('<span class="tab-head-closeBtn">×</span>'),this.closeBtn());
};
Tab.prototype = {
closeBtn : function(){
var self = this;
self.close.bind("mouseover",function(){
jQuery(this).addClass("tab-closeBtn-hover");
});
self.close.bind("mouseout",function(){
jQuery(this).removeClass("tab-closeBtn-hover");
});
self.head.append(self.close);
},
getFocus : function(){
this.head.addClass("tab-head-active");
this.body.show();
},
loseFocus : function(){
this.head.removeClass("tab-head-active");
this.body.hide();
},
destory : function(){
this.head.remove();
this.body.remove();
},
};
/*
head html : <div><span></span><span></span>...</div>
body html : <div><iframe></iframe><iframe></iframe>...</div>
*/
var TabView = function(container){
this.container = container;
this.head = jQuery('<div class="tabView-head"></div>');
this.body = jQuery('<div class="tabView-body"></div>');
this.tabs = [];
this.tabId = 0;
this.focusTab = null;
this.init();
};
TabView.prototype = {
init : function(){
this.container.append(this.head).append(this.body);
},
add : function(title,url,isClose){
var self = this;
var tab = new Tab(self.tabId,title,url,isClose);
self._tabEvents(tab);
(self.tabs.length==0) && (tab.getFocus(),self.focusTab=tab);
self.tabs.push(tab);
self.head.append(tab.head);
self.body.append(tab.body);
self.tabId++;
},
remove_ref : function(tab){
var self = this;
for(var i=0;i<self.tabs.length;i++){
if(tab.id===self.tabs[i].id){
tab.destory();
self.tabs.splice(i,1);
return i;
}
}
return -1;
},
destory : function(){
this.head.remove();
this.body.remove();
},
_tabEvents : function(tab){
var self = this;
tab.head.bind("click",function(){
if(self.focusTab.id != tab.id){
tab.getFocus();
self.focusTab.loseFocus();
self.focusTab = tab;
}
});
tab.close && tab.close.bind("click",function(){
tab.destory();
var i = self.remove_ref(tab);
if(tab.id==self.focusTab.id){//如果關(guān)閉的是當(dāng)前的tab
if(self.tabs.length==0){//如果所有tab都已關(guān)閉
self.destory();
}else{
var nextIndex = self.tabs.length==i ? i-1 : i;
self.focusTab = self.tabs[nextIndex];
self.focusTab.getFocus();
}
}
});
},
};

代碼如下:
復(fù)制代碼 代碼如下:
/*
head html : <span></span>
body html : <iframe></iframe>
*/
var Tab = function(id,title,url,isClose){
this.id = id;
this.title = title;
this.url = url;
this.head = jQuery('<span class="tab-head">' + this.title +'</span>');
this.body = jQuery('<iframe name="ifm' + this.id +'" src="' + this.url +'" frameborder=0 class="tab-body"></iframe>').hide();
isClose && (this.close = jQuery('<span class="tab-head-closeBtn">×</span>'),this.closeBtn());
};
Tab.prototype = {
closeBtn : function(){
var self = this;
self.close.bind("mouseover",function(){
jQuery(this).addClass("tab-closeBtn-hover");
});
self.close.bind("mouseout",function(){
jQuery(this).removeClass("tab-closeBtn-hover");
});
self.head.append(self.close);
},
getFocus : function(){
this.head.addClass("tab-head-active");
this.body.show();
},
loseFocus : function(){
this.head.removeClass("tab-head-active");
this.body.hide();
},
destory : function(){
this.head.remove();
this.body.remove();
},
};
/*
head html : <div><span></span><span></span>...</div>
body html : <div><iframe></iframe><iframe></iframe>...</div>
*/
var TabView = function(container){
this.container = container;
this.head = jQuery('<div class="tabView-head"></div>');
this.body = jQuery('<div class="tabView-body"></div>');
this.tabs = [];
this.tabId = 0;
this.focusTab = null;
this.init();
};
TabView.prototype = {
init : function(){
this.container.append(this.head).append(this.body);
},
add : function(title,url,isClose){
var self = this;
var tab = new Tab(self.tabId,title,url,isClose);
self._tabEvents(tab);
(self.tabs.length==0) && (tab.getFocus(),self.focusTab=tab);
self.tabs.push(tab);
self.head.append(tab.head);
self.body.append(tab.body);
self.tabId++;
},
remove_ref : function(tab){
var self = this;
for(var i=0;i<self.tabs.length;i++){
if(tab.id===self.tabs[i].id){
tab.destory();
self.tabs.splice(i,1);
return i;
}
}
return -1;
},
destory : function(){
this.head.remove();
this.body.remove();
},
_tabEvents : function(tab){
var self = this;
tab.head.bind("click",function(){
if(self.focusTab.id != tab.id){
tab.getFocus();
self.focusTab.loseFocus();
self.focusTab = tab;
}
});
tab.close && tab.close.bind("click",function(){
tab.destory();
var i = self.remove_ref(tab);
if(tab.id==self.focusTab.id){//如果關(guān)閉的是當(dāng)前的tab
if(self.tabs.length==0){//如果所有tab都已關(guān)閉
self.destory();
}else{
var nextIndex = self.tabs.length==i ? i-1 : i;
self.focusTab = self.tabs[nextIndex];
self.focusTab.getFocus();
}
}
});
},
};
您可能感興趣的文章:
- javascript實現(xiàn)表現(xiàn)、結(jié)構(gòu)、行為分離的選項卡效果!
- javascript漸變顯示的雅虎中國選項卡效果代碼
- 用javascript實現(xiàn)的不錯的一款網(wǎng)頁選項卡
- javascript橫排豎排標(biāo)準(zhǔn)選項卡效果代碼
- js實現(xiàn)tab選項卡函數(shù)代碼
- 純php打造的tab選項卡效果代碼(不用js)
- 跨瀏覽器通用、可重用的選項卡tab切換js代碼
- 利用js實現(xiàn)選項卡的特別效果的實例
- javascript實現(xiàn)tabs選項卡切換效果(自寫原生js)
- jsp js鼠標(biāo)移動到指定區(qū)域顯示選項卡離開時隱藏示例
- js/jQuery簡單實現(xiàn)選項卡功能
- 基于JavaScript實現(xiàn)通用tab選項卡(通用性強)
相關(guān)文章
JavaScript比較當(dāng)前時間是否在指定時間段內(nèi)的方法
這篇文章主要介紹了JavaScript比較當(dāng)前時間是否在指定時間段內(nèi)的方法,涉及javascript時間與字符串的轉(zhuǎn)換及比較操作相關(guān)技巧,需要的朋友可以參考下2016-08-08hash特點、hashchange事件介紹及其常見應(yīng)用場景
淺析hash特點、hashchange事件介紹及其常見應(yīng)用場景(不同hash對應(yīng)不同事件處理、移動端大圖展示狀態(tài)后退頁面問題、原生輕應(yīng)用頭部后退問題、移動端自帶返回按鈕二次確認(rèn)問題),hashchange和popstate事件觸發(fā)條件2023-11-11