我正在用 ionic 构建一个移动应用程序,我想通过放置幻灯片来制作一个类似松弛的侧面菜单。
例如,当您单击主菜单项时,它将像 slack 一样在侧菜单中滑出另一张幻灯片。
我尝试在离子菜单中使用离子幻灯片,但幻灯片不起作用。
请查看屏幕截图。
这是代码片段。
<ion-menu [content]="mycontent" [swipeEnabled]="false">
<ion-content>
<ion-slides>
<ion-slide>
<h1>Slide 1</h1>
</ion-slide>
<ion-slide>
<h1>Slide 2</h1>
</ion-slide>
<ion-slide>
<h1>Slide 3</h1>
</ion-slide>
</ion-slides>
</ion-content>
</ion-menu>
<ion-nav #mycontent [root]="rootPage"></ion-nav>
这就是我正在尝试构建的内容。
关于如何实现这一点有什么想法吗?
Thanks.
离子载玻片组件在底层使用了 Swiper 库。 Swiper 的部分初始化代码取决于了解幻灯片容器的宽度,并且该代码使用clientWidth
为拿到它,为实现它。由于菜单以display:none
,检索到的宽度始终为零,并且初始化代码会影响您。
您可以通过临时设置来解决这个问题display:block
当 Swiper 初始化时。我的侧面菜单位于组件内,因此您可能需要根据您的情况调整此代码:
应用程序.html:
<sidebar [content]="content"></sidebar>
<ion-nav #content [root]="rootPage" swipeBackEnabled="false"></ion-nav>
侧边栏.html:
<ion-menu [content]="content" swipeEnabled="false">
<ion-content>
<ion-slides pager>
<ion-slide>
<h2>Slide 1</h2>
</ion-slide>
<ion-slide>
<h2>Slide 2</h2>
</ion-slide>
<ion-slide>
<h2>Slide 3</h2>
</ion-slide>
</ion-slides>
</ion-content>
</ion-menu>
侧边栏.component.ts:
...
@Component({
selector: 'sidebar',
templateUrl: 'sidebar.html',
})
export class SidebarComponent implements AfterViewInit {
@Input('content') content: NavController;
@ViewChild(Slides) slides: Slides;
@ViewChild(Menu, { read: ElementRef }) menuRef: ElementRef;
// Use Renderer2 instead of direct DOM manipulation through the
// ElementRef.nativeElement.
//
// @see: https://medium.com/@kmathy/angular-manipulate-properly-the-dom-with-renderer-16a756508cba
//
constructor(private renderer: Renderer2) {
}
// ViewChild template references might not be available until
// AfterViewInit lifecycle hook runs.
//
// @see: https://blog.angular-university.io/angular-viewchild/
//
ngAfterViewInit() {
this.renderer.setStyle(this.menuRef.nativeElement, 'display', 'block');
setTimeout(() => {
// Swiper init has its own ngAfterViewInit code that delays 300ms
// before running. Don't remove the 'display' style until after that.
this.renderer.removeStyle(this.menuRef.nativeElement, 'display');
}, 310);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)