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

(编辑:jimmy 日期: 2024/9/29 浏览:2)

本文实例讲述了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="/UploadFiles/2021-04-02/angular.min.js">

二、效果预览

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

三、实现原理

选项卡的内容是显示还是隐藏是由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="/UploadFiles/2021-04-02/angular.min.js">

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

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

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

一句话新闻

Windows上运行安卓你用过了吗
在去年的5月23日,借助Intel Bridge Technology以及Intel Celadon两项技术的驱动,Intel为PC用户带来了Android On Windows(AOW)平台,并携手国内软件公司腾讯共同推出了腾讯应用宝电脑版,将Windows与安卓两大生态进行了融合,PC的使用体验随即被带入到了一个全新的阶段。