【问题标题】:Is it possible to use slides in ionic sidebar是否可以在离子侧边栏中使用幻灯片
【发布时间】:2019-06-29 11:07:46
【问题描述】:

我正在用 ionic 构建一个移动应用程序,我想通过放置幻灯片来制作一个类似松弛的侧边菜单。

例如,当您单击主菜单项时,它会像 slack 一样滑出侧面菜单中的另一张幻灯片。

我尝试在 ion-menu 中使用 ion-slides,但幻灯片无法正常工作。

请看截图。

这里是代码sn-p。

<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>

这是我正在尝试构建的。

关于如何实现这个有什么想法吗?

谢谢。

【问题讨论】:

  • 你能分享你的侧边菜单的实现吗?我还没有在侧边菜单中使用幻灯片,但我不明白为什么它不应该工作。
  • 谢谢。我在描述中添加了代码 sn-p。 @StephanStrate

标签: ionic-framework ionic3 ion-slides


【解决方案1】:

ion-slides 组件在底层使用了 Swiper 库。 Swiper 的部分初始化代码取决于知道滑动容器的宽度,代码使用clientWidth 来获取它。由于菜单以display:none 开头,因此检索到的宽度始终为零,并且初始化代码会交给您。

您可以通过在 Swiper 初始化时临时设置 display:block 来解决此问题。我的侧边菜单位于组件内部,因此您可能需要根据您的情况调整此代码:

app.html:

<sidebar [content]="content"></sidebar>
<ion-nav #content [root]="rootPage" swipeBackEnabled="false"></ion-nav>

sidebar.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>

sidebar.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);
  }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-15
    • 2020-06-01
    • 2018-08-17
    • 1970-01-01
    • 2015-05-12
    • 1970-01-01
    相关资源
    最近更新 更多