ionic实现可滑动的tab

转载请注明出处:
http://blog.csdn.net/lishihong108/article/details/52316313

利用ionic的slide-box组件实现可滑动的tab,主要是监听tab点击以及slide页面滑动的事件,做相应的处理,用ng-repeat循环,优化、简略了代码,有需要的同学可以看看。
  先来张效果图:
  这里写图片描述

用到的css代码:

.tab_default{ 
	border-bottom:solid 1px #F2F2F2;
	padding:6px 0;
}
.tab_select{
	border-bottom:solid 1px #3E89F5;
	box-shadow:0 -3px 8px #C1D3F0 inset;
}
.arrow-top {
    position: absolute;
    width: 0;
    height: 0; 
    top:20px; 
    border: 6px solid #3E89F5;
    border-right-color:transparent;
    border-left-color: transparent;
    border-top-color: transparent;
}
.arrow-top:after {
    content:'';
    position:absolute;
    width: 0;
    height: 0; 
    border: 12px solid #fff;
    right: -12px;
    bottom: -13px;
    border-right-color:transparent;
    border-left-color: transparent;
    border-top-color: transparent;
}

页面上html代码:

<ion-view view-title="滑动tab"> 
  <ion-content has-bouncing="false">
  	<div style="display:flex;width:100%;">
  		<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' : '' ">
  			{{d}}
  			<div class="arrow-top" style="left:{{15+$index*33}}%" ng-show="slectIndex==$index"></div>
  		</div> 
  	</div> 
  	<ion-slide-box on-slide-changed="slideChanged(index)" active-slide="slideIndex" does-continue="false" show-pager="false">
  	  <ion-slide ng-repeat="p in pages">
	  		<div ng-include="p"></div>
  	  </ion-slide>
  	</ion-slide-box>
  </ion-content>
</ion-view>

对应的controller.js代码:

$scope.tabNames=['java','html5','android'];
$scope.slectIndex=0;
$scope.activeSlide=function(index){//点击时候触发
	$scope.slectIndex=index;
	$ionicSlideBoxDelegate.slide(index);
};
$scope.slideChanged=function(index){//滑动时候触发
	$scope.slectIndex=index;
};
$scope.pages=["templates/tab01.html","templates/tab02.html","templates/tab03.html"];

tab01.html、tab02.html、tab03.html这几个都是差不多的,贴一个tab01的:

<div style="width:100%;text-align: center;padding-top:30px;">
	page 01
	<p style="margin-top:30px;">
		<img src="img/tab01.jpg"  style="width:100%;"/>
	</p>
</div>

注意点:

  • 点击时候将当前循环的索引$index赋值给变量slideIndex,在tab中使用ng-class判断点击时候的索引和slideIndex是否一样,一样则改变相应的tab的样式;
  • 滑动的时候是用的slide-box的一个函数,on-slide-changed,当slide页面发发生变化的时候,会向改函数传递一个变量index标识当前slide的索引 ;
  • 如果不想滚动只点击,可以去掉on-slide-changed的监听,也可以增加一个属性,disable-scroll="true",禁止slide page滚动。
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ionic实现图片预览可以使用 Ionic 的 ion-slides 组件和 ngx-gallery 库。 1. 首先,在终端中运行以下命令安装 ngx-gallery 库: ``` npm install @kolkov/ngx-gallery --save ``` 2. 在 app.module.ts 中导入 NgxGalleryModule: ```typescript import { NgxGalleryModule } from '@kolkov/ngx-gallery'; @NgModule({ imports: [ // ... NgxGalleryModule ], // ... }) export class AppModule { } ``` 3. 在 HTML 中使用 ion-slides 组件,并在其中使用 ngx-gallery: ```html <ion-slides pager="true"> <ion-slide *ngFor="let image of images"> <ngx-gallery [options]="galleryOptions" [images]="[image]"></ngx-gallery> </ion-slide> </ion-slides> ``` 4. 在 TypeScript 文件中定义图片和 ngx-gallery 的选项: ```typescript import { Component } from '@angular/core'; @Component({ selector: 'app-home', templateUrl: 'home.page.html', styleUrls: ['home.page.scss'], }) export class HomePage { images = [ { small: 'https://picsum.photos/id/1018/200/300', medium: 'https://picsum.photos/id/1018/500/750', big: 'https://picsum.photos/id/1018/1200/1800', description: 'Image 1' }, { small: 'https://picsum.photos/id/1015/200/300', medium: 'https://picsum.photos/id/1015/500/750', big: 'https://picsum.photos/id/1015/1200/1800', description: 'Image 2' }, { small: 'https://picsum.photos/id/1019/200/300', medium: 'https://picsum.photos/id/1019/500/750', big: 'https://picsum.photos/id/1019/1200/1800', description: 'Image 3' } ]; galleryOptions = [ { width: '100%', height: 'auto', thumbnailsColumns: 4, imageAnimation: 'slide', preview: false } ]; constructor() {} } ``` 这样,就可以在 Ionic实现图片预览了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值