引导轮播仅适用于移动设备

时间:2019-06-29 18:47:55

标签: html angular bootstrap-4

我对bootstrap4轮播有疑问。我目前为所有设备实现了一个轮播,但我希望它仅在移动设备上出现。在桌面上,并排放置照片。 我知道我可以做两个容器,一个用于桌面,另一个用于旋转木马。但我想避免这种情况。所有建议的解决方案都适用于jQuery,但是我使用的是Angular7,我想避免使用jQuery。

                    <div id="demo" class="carousel slide" data-ride="carousel">

                      <!--  Indicators -->

                        <ul class="carousel-indicators">
                                <li data-target="#demo" data-slide-to="0" class="active"></li>
                                <li data-target="#demo" data-slide-to="1"></li>
                                <li data-target="#demo" data-slide-to="2"></li>
                        </ul>

                            <!--      The slideshow -->

                              <div class="carousel-inner">
                                <div class="carousel-item">
                                  <img src="img1.jpg">
                                </div>
                                <div class="carousel-item">
                                  <img src="img2.jpg">
                                </div>
                                <div class="carousel-item">
                                  <img src="img3.jpg">
                                </div>
                              </div>

                              <!-- Left and right controls -->
                              <a class="carousel-control-prev" href="#demo" data-slide="prev">
                                  <mat-icon class="prev-icon">chevron_left</mat-icon>
                                <!--<span class="carousel-control-prev-icon"></span>  --> 
                              </a>
                              <a class="carousel-control-next" href="#demo" data-slide="next">
                                      <mat-icon class="next-icon">chevron_right</mat-icon>
                              </a>

                            </div>

1 个答案:

答案 0 :(得分:0)

img1: string = 'img1.jpg'
img2: string = 'img2.jpg'
img3: string = 'img3.jpg'

isHandset$: Observable<boolean> = this.breakPointObserver.observe(['(max-width: 750px)'])
    .pipe(
      map(result => result.matches)
    );

  constructor(private breakPointObserver: BreakpointObserver) { }

隐藏在屏幕上的模板小于750px:

<div *ngIf="!(isHandset$ | async)">

解决方案:

// this whows on 750-
<div *ngIf="(isHandset$ | async)" id="demo" class="carousel slide" data-ride="carousel">
  <ul class="carousel-indicators">
    <li data-target="#demo" data-slide-to="0" class="active"></li>
    <li data-target="#demo" data-slide-to="1"></li>
    <li data-target="#demo" data-slide-to="2"></li>
  </ul>
  <div class="carousel-inner">
    <div class="carousel-item">
      <img [src]="img1">
    </div>
    <div class="carousel-item">
      <img [src]="img2">
    </div>
    <div class="carousel-item">
      <img [src]="img3">
    </div>
  </div>
  <a class="carousel-control-prev" href="#demo" data-slide="prev">
    <mat-icon class="prev-icon">chevron_left</mat-icon>
  </a>
  <a class="carousel-control-next" href="#demo" data-slide="next">
    <mat-icon class="next-icon">chevron_right</mat-icon>
  </a>
</div>
//this shows on 750+
<div *ngIf="!(isHandset$ | async)" class="row carousel-inner">
  <div class="col-4 carousel-item">
    <img [src]="img1">
  </div>
  <div class="col-4 carousel-item">
    <img [src]="img2">
  </div>
  <div class="col-4 carousel-item">
    <img [src]="img3">
  </div>
</div>