猿问
下载APP

ionic实现可滑动的tab选项卡切换效果?

ionic实现可滑动的tab选项卡切换效果


繁花不似锦
浏览 524回答 1
1回答

皈依舞

利用ionic的slide-box组件实现可滑动的tab,主要是监听tab点击以及slide页面滑动的事件,做相应的处理,用ng-repeat循环,优化、简略了代码,有需要的同学可以看看。 &nbsp;&nbsp;先来张效果图: &nbsp;&nbsp;  &nbsp;用到的css代码:&nbsp; &nbsp; &nbsp;.tab_default{ &nbsp; &nbsp;border-bottom:solid 1px #F2F2F2; &nbsp; padding:6px 0; &nbsp;} &nbsp;.tab_select{ &nbsp; border-bottom:solid 1px #3E89F5; &nbsp; box-shadow:0 -3px 8px #C1D3F0 inset; &nbsp;} &nbsp;.arrow-top { &nbsp; position: absolute; &nbsp; width: 0; &nbsp; height: 0; &nbsp; &nbsp;top:20px; &nbsp; &nbsp;border: 6px solid #3E89F5; &nbsp; border-right-color:transparent; &nbsp; border-left-color: transparent; &nbsp; border-top-color: transparent; &nbsp;} &nbsp;.arrow-top:after { &nbsp; content:''; &nbsp; position:absolute; &nbsp; width: 0; &nbsp; height: 0; &nbsp; &nbsp;border: 12px solid #fff; &nbsp; right: -12px; &nbsp; bottom: -13px; &nbsp; border-right-color:transparent; &nbsp; border-left-color: transparent; &nbsp; border-top-color: transparent; &nbsp;} &nbsp; &nbsp;页面上html代码:&nbsp; &nbsp; &nbsp;<ion-view view-title="滑动tab"> &nbsp; &nbsp;<ion-content has-bouncing="false"> &nbsp; <div style="display:flex;width:100%;"> &nbsp; &nbsp;<div style="flex:1;text-align: center;" class="tab_default" ng-repeat="d in tabNames" ng-click="activeSlide($index)" ng-class="slectIndex==$index ? 'tab_select' : '' "> &nbsp; &nbsp; {{d}} &nbsp; &nbsp; <div class="arrow-top" style="left:{{15+$index*33}}%" ng-show="slectIndex==$index"></div> &nbsp; &nbsp;</div> &nbsp; &nbsp;</div> &nbsp; &nbsp;<ion-slide-box on-slide-changed="slideChanged(index)" active-slide="slideIndex" does-continue="false" show-pager="false"> &nbsp; &nbsp;<ion-slide ng-repeat="p in pages"> &nbsp; &nbsp; <div ng-include="p"></div> &nbsp; &nbsp;</ion-slide> &nbsp; </ion-slide-box> &nbsp; </ion-content> &nbsp;</ion-view> &nbsp; &nbsp; &nbsp; &nbsp;对应的controller.js代码:&nbsp; &nbsp; &nbsp;$scope.tabNames=['java','html5','android']; &nbsp;$scope.slectIndex=0; &nbsp;$scope.activeSlide=function(index){//点击时候触发 &nbsp; $scope.slectIndex=index; &nbsp; $ionicSlideBoxDelegate.slide(index); &nbsp;}; &nbsp;$scope.slideChanged=function(index){//滑动时候触发 &nbsp; $scope.slectIndex=index; &nbsp;}; &nbsp;$scope.pages=["templates/tab01.html","templates/tab02.html","templates/tab03.html"]; &nbsp; &nbsp; &nbsp; &nbsp;tab01.html、tab02.html、tab03.html这几个都是差不多的,贴一个tab01的:&nbsp; &nbsp; &nbsp;<div style="width:100%;text-align: center;padding-top:30px;"> &nbsp; page 01 &nbsp; <p style="margin-top:30px;"> &nbsp; &nbsp;<img src="img/tab01.jpg" style="width:100%;"/> &nbsp; </p> &nbsp;</div> &nbsp; &nbsp; &nbsp; &nbsp;注意点: &nbsp;&nbsp;•点击时候将当前循环的索引$index赋值给变量slideIndex,在tab中使用ng-class判断点击时候的索引和slideIndex是否一样,一样则改变相应的tab的样式; &nbsp;&nbsp;•滑动的时候是用的slide-box的一个函数,on-slide-changed,当slide页面发发生变化的时候,会向改函数传递一个变量index标识当前slide的索引 ; &nbsp;&nbsp;•如果不想滚动只点击,可以去掉on-slide-changed的监听,也可以增加一个属性,disable-scroll="true",禁止slide page滚动; &nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP
我要回答