溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

JavaScript插件如何實(shí)現(xiàn)Tab選項(xiàng)卡效果

發(fā)布時(shí)間:2021-06-29 09:19:39 來(lái)源:億速云 閱讀:141 作者:小新 欄目:web開發(fā)

這篇文章將為大家詳細(xì)講解有關(guān)JavaScript插件如何實(shí)現(xiàn)Tab選項(xiàng)卡效果,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

首先,來(lái)看看最終效果:

JavaScript插件如何實(shí)現(xiàn)Tab選項(xiàng)卡效果

這是一款普通的Tab選項(xiàng)卡插件,下面來(lái)講講它實(shí)現(xiàn)了哪些功能:

1、支持不同鼠標(biāo)事件觸發(fā)選項(xiàng)卡切換效果;
2、支持不同切換效果的配置,例如淡入淡出/直接切換;
3、支持默認(rèn)展示第幾個(gè)選項(xiàng)卡的配置;
4、支持選項(xiàng)卡的自動(dòng)切換效果。

例子很簡(jiǎn)單,需要用到的知識(shí)包括:
1、html、css的基礎(chǔ)知識(shí);
2、對(duì)this,prototype,new等關(guān)鍵詞的理解。

簡(jiǎn)而言之,就是通過(guò)參數(shù)配置的形式來(lái)完成不同效果的展示。

下面先看一看如何使用:

1、$(".js-tab").etab();
2、$(".js-tab").etab({
                triggerType: "click",
                effect: "fade",
                invoke: 2,
                auto: 3000
            });
3、Tab.init($(".js-tab"));

本插件支持幾種不同的初始化方式,代碼很簡(jiǎn)單,類似于BootStrap插件的使用方法。下面奉上完整的代碼:

index.html

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Tab選項(xiàng)卡</title>
 <link href="tab.css" rel="stylesheet">
 <style>
 * {
  margin:0;
  padding:0;
 }
 body {
  background-color: #323232;
  font-size:12px;
  font-family:微軟雅黑;
  padding:100px;
 }
 ul, li {
  list-style-type: none;
 }
 </style>
 <script src="../lib/jquery-1.11.3.js"></script>
 <script type="text/javascript" src="tab.js"></script>
</head>
<body>
 <div class="js-tab tab">
 <ul class="tab-nav">
  <li class="active"><a href="#">新聞</a> </li>
  <li><a href="#">電影</a> </li>
  <li><a href="#">娛樂</a> </li>
  <li><a href="#">科技</a> </li>
 </ul>

 <div class="content-wrap">
  <div class="content-item current">
  <h4>新聞</h4>
  </div>
  <div class="content-item">
  <h4>電影</h4>
  </div>
  <div class="content-item">
  <h4>娛樂</h4>
  </div>
  <div class="content-item">
  <h4>科技</h4>
  </div>
 </div>
 </div>
 <script>
 $(function() {
//  Tab.init($(".js-tab"));
  $(".js-tab").etab({
  triggerType: "click",
  effect: "fade",
  invoke: 2,
  auto: 3000
  });
  $(".js-tab").etab();
 });
 </script>
</body>
</html>
.tab {
 width: 300px;
}

.tab .tab-nav {
 height: 30px;
}

.tab .tab-nav li {
 float: left;
 margin-right:5px;
 background-color:#767676;
 border-radius:3px 3px 0 0;
}

.tab .tab-nav li a{
 display:block;
 height:30px;
 padding:0 20px;
 color: white;
 line-height:30px;
 text-decoration: none;
}

.tab .tab-nav .active {
 background-color: #fff;

}

.tab .tab-nav .active a{
 color: #777;
}

.tab .content-wrap{
 background-color: white;
 padding:5px;
 height:200px
}

.tab .content-wrap .content-item {
 position:absolute;
 height: 200px;
 display: none;
}

.tab .content-wrap .current {
 height: 200px;
 display: block;
}

最后將插件代碼列出來(lái),在代碼里面已經(jīng)寫了很詳細(xì)的注釋:

/**
 * Created by Wu.lin on 2017/11/12.
 */
(function($){

 var Tab = function(tab, _params) {
 var _this = this;

 //保存單個(gè)Tab組件
 this.tab = tab;

 this.params = _params;

 //默認(rèn)配置參數(shù)
 this.config = {
  //用來(lái)定義鼠標(biāo)的出發(fā)類型 "click"/mouseover
  "triggerType": "mouseover",

  //用來(lái)定義內(nèi)容切換效果,直接切換/淡入淡出
  "effect": "default",

  //默認(rèn)展示第幾個(gè)Tab
  "invoke": "1",

  //用來(lái)定義Tab是否自動(dòng)切換,當(dāng)指定了事件間隔,就表示自動(dòng)切換,并指定了切換間隔
  "auto": false
 };

 //如果配置參數(shù)存在,就擴(kuò)展默認(rèn)的配置參數(shù)
 if(this.params){
  $.extend(this.config, this.params);
 }

 //保存Tab標(biāo)簽列表,以及對(duì)應(yīng)的內(nèi)容列表
 this.tabItem = this.tab.find("ul.tab-nav li");
 this.contentItem = this.tab.find("div.content-wrap .content-item");

 //保存配置參數(shù)
 var config = this.config;

 if(config.triggerType === "click") {
  this.tabItem.bind(config.triggerType, function() {
  _this.invoke($(this));
  });

 } else {
  this.tabItem.mouseover(function(){
  _this.invoke($(this));
  });
 }

 //自動(dòng)切換功能
 if(config.auto) {
  this.timmer = null;

  //計(jì)數(shù)器
  this.loop = 0;

  this.autoPlay();

  this.tab.hover(function() {
  window.clearInterval(_this.timmer);
  }, function() {
  _this.autoPlay();
  });
 }

 //設(shè)置默認(rèn)顯示第幾個(gè)Tab
 if(config.invoke > 1) {
  this.invoke(this.tabItem.eq(config.invoke - 1));
 }


 };

 Tab.prototype = {

 //事件驅(qū)動(dòng)函數(shù)
 invoke: function(currentTab) {

  /**
  * 1、執(zhí)行Tab選中狀態(tài),當(dāng)前選中Tab加上Active,
  * 2、切換對(duì)應(yīng)Tab內(nèi)容,根據(jù)配置參數(shù)effect參數(shù)default|fade
  */

  var index = currentTab.index();
  var conItem = this.contentItem;

  //Tab切換
  currentTab.addClass("active").siblings().removeClass("active");

  //內(nèi)容區(qū)域切換
  var effect = this.config.effect;

  if(effect === "fade") {
  conItem.eq(index).fadeIn().siblings().fadeOut();
  } else {
  conItem.eq(index).addClass("current").siblings().removeClass("current");
  }

  //注意,如果配置了自動(dòng)切換,記得把當(dāng)前的loop值設(shè)置為當(dāng)前的Tab的index
  if(this.config.auto) {
  this.loop = index;
  }
 },

 //自動(dòng)間隔切換
 autoPlay: function() {

  var _this_ = this,
  tabItems = this.tabItem, //臨時(shí)保存Tab列表
  tabLength = tabItems.size(),
  config = this.config;

  this.timmer = window.setInterval(function() {
  _this_.loop++;
  if(_this_.loop >= tabLength) {
   _this_.loop = 0;
  }

  tabItems.eq(_this_.loop).trigger(config.triggerType);
  }, config.auto);

 }
 };

 Tab.init = function(tabs) {
 var _this_ = this;
 tabs.each(function() {
  new _this_($(this));
 });
 // var tab = new Tab($(".js-tab").eq(0));
 };

 //注冊(cè)成JQuery方法
 $.fn.extend({
 etab: function(_param) {
  this.each(function () {
  new Tab($(this), _param);
  });
  return this;
 }
 });

 window.Tab = Tab;

})(jQuery);

關(guān)于“JavaScript插件如何實(shí)現(xiàn)Tab選項(xiàng)卡效果”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI