AngularJS实现tab选项卡的方法详解
程序员文章站
2022-11-26 14:42:06
本文实例讲述了angularjs实现tab选项卡的方法。分享给大家供大家参考,具体如下:
一、代码实现
本文实例讲述了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程序设计有所帮助。