当前位置:Gxlcms > JavaScript > AngularJS实现tab选项卡的方法详解

AngularJS实现tab选项卡的方法详解

时间:2021-07-01 10:21:17 帮助过:44人阅读

本文实例讲述了AngularJS实现tab选项卡的方法。分享给大家供大家参考,具体如下:

一、代码实现

<!DOCTYPE html>
<html ng-app='app'>
  <head>
    <meta charset="UTF-8">
    <title></title>
    <style>
      *{
        list-style: none;
        margin: 0;
        padding: 0;
      }
      .TabNav{
        height: 131px;
        width: 450px;
        position: relative;
        margin-left: auto;
        margin-right: auto;
        margin-top: 100px;
      }
      .TabNav ul li{
        float: left;
        background: -webkit-gradient(linear,left top,left bottom, from(#fefefe), to(#ededed));
        border: 1px solid #ccc;
        padding: 5px 0;
        width: 100px;
        text-align: center;
        margin-left: -1px;
        position: relative;
        cursor: pointer;
      }
      .TabCon{
        position: absolute;
        left: -1px;
        top: 30px;
        border: 1px solid #ccc;
        border-top: none;
        width: 403px;
        height: 100px;
      }
      .TabNav ul li.active{
        background: #FFFFFF;
        border-bottom: none;
      }
    </style>
    <script src="js/angular.min.js" type="text/javascript" charset="utf-8"></script>
  </head>
  <body>
    <div class="TabNav">
      <ul ng-init='activeTab=1'>
        <li ng-class='{active:activeTab==1}' ng-click='activeTab=1'>许嵩</li>
        <li ng-class='{active:activeTab==2}' ng-click='activeTab=2'>周杰伦</li>
        <li ng-class='{active:activeTab==3}' ng-click='activeTab=3'>林俊杰</li>
        <li ng-class='{active:activeTab==4}' ng-click='activeTab=4'>陈奕迅</li>
      </ul>
      <div class="TabCon">
        <div ng-show='activeTab==1'>断桥残雪、千百度、幻听、想象之中</div>
        <div ng-show='activeTab==2'>红尘客栈、牛仔很忙、给我一首歌的时间、听妈妈的话</div>
        <div ng-show='activeTab==3'>被风吹过的夏天、江南、一千年以后</div>
        <div ng-show='activeTab==4'>十年、K歌之王、浮夸</div>
      </div>
    </div>
  </body>
  <script type="text/javascript">
    var app=angular.module('app',[]);
    app.controller('tabcontroller',function($scope){
      var vm=$scope.vm;
    });
  </script>
</html>

二、效果预览

三、实现原理

选项卡的内容是显示还是隐藏是由activeTab的值决定的,而这个值是通过选项卡上面的ng-click指令设置的,当对应选项卡的内容显示的时候,给点击的按钮添加样式,这样做虽然也能实现选项卡的内容,但是这样做的的弊端是,选项卡的内容是固定的,不好去改变,所以接下来我们将上面的代码改成下面这种形式

四、改版

<!DOCTYPE html>
<html ng-app='app'>
  <head>
    <meta charset="UTF-8">
    <title></title>
    <style>
      *{
        list-style: none;
        margin: 0;
        padding: 0;
      }
      .TabNav{
        height: 131px;
        position: relative;
        margin-left: 100px;
        margin-top: 100px;
      }
      .TabNav ul li{
        float: left;
        background: -webkit-gradient(linear,left top,left bottom, from(#fefefe), to(#ededed));
        border: 1px solid #ccc;
        padding: 5px 0;
        width: 100px;
        text-align: center;
        margin-left: -1px;
        position: relative;
        cursor: pointer;
      }
      .TabCon{
        position: absolute;
        left: -1px;
        top: 30px;
        border: 1px solid #ccc;
        border-top: none;
        width: 403px;
        height: 100px;
      }
      .TabNav ul li.active{
        background: #FFFFFF;
        border-bottom: none;
      }
    </style>
    <script src="js/angular.min.js" type="text/javascript" charset="utf-8"></script>
  </head>
  <body>
    <div class="TabNav" ng-controller='tabcontroller'>
      <ul ng-init='selected=0'>
        <li ng-class='{active:selected==$index}' ng-click='show($index)' ng-repeat='item in vm'>{{item.list}}</li>
      </ul>
      <div class="TabCon">
        <div ng-show='selected==$index' ng-repeat='item in vm'>{{item.con}}</div>
      </div>
    </div>
  </body>
  <script type="text/javascript">
    var app=angular.module('app',[]);
    app.controller('tabcontroller',function($scope){
      $scope.vm=[
        {"list":"许嵩","con":"断桥残雪、千百度、幻听、想象之中"},
        {"list":"周杰伦","con":"红尘客栈、牛仔很忙、给我一首歌的时间、听妈妈的话"},
        {"list":"林俊杰","con":"被风吹过的夏天、江南、一千年以后"},
        {"list":"陈奕迅","con":"十年、K歌之王、浮夸"}
      ];
      var selected=$scope.selected;
      $scope.show=function(index){
        $scope.selected=index;
      };
    });
  </script>
</html>

说明:vm这个数组里面是我们自己定义的一些假数据(这个数据实际上是可以从后台获取的),然后我们通过ng-repeat指令循环遍历出vm里面的数据,插入到页面中,$index是每个内容对象的索引值

更多关于AngularJS相关内容感兴趣的读者可查看本站专题:《AngularJS指令操作技巧总结》、《AngularJS入门与进阶教程》及《AngularJS MVC架构总结》

希望本文所述对大家AngularJS程序设计有所帮助。

人气教程排行