【发布时间】:2019-02-03 22:49:28
【问题描述】:
我正在使用 Angular 6 和 ng-bootstrap 并尝试轮播。
carousel 工作正常,但无法在一个滑块中显示 4 张图片。它在每个滑块中显示 1 张图像。
这是通过 API 响应传来的数据:
games= [{"title_id":1,"title_name":"MIssion Impossible","title_img":"https://media.services.cinergy.ch/media/box1600/f1323e57a2c4ea79dde779a89d561f85bfbe6bf5.jpg","genres":[{"id":1,"name":"Action"},{"id":2,"name":"Adventure"}]},{"title_id":2,"title_name":"Matrix","title_img":"https://www.sideshowtoy.com/assets/products/903302-neo/lg/the-matrix-neo-sixth-scale-figure-hot-toys-903302-01.jpg","genres":[{"id":1,"name":"Action"},{"id":2,"name":"Adventure"},{"id":6,"name":"Fantasy"}]},{"title_id":3,"title_name":"Avengers","title_img":"http://media.comicbook.com/2018/03/avengers-infinity-war-poster-all-iron-man-version-1096449.jpeg","genres":[{"id":1,"name":"Action"},{"id":2,"name":"Adventure"},{"id":6,"name":"Fantasy"}]},{"title_id":4,"title_name":"Stargate SG-1","title_img":"https://image.tmdb.org/t/p/w300_and_h450_bestv2/rst5xc4f7v1KiDiQjzDiZqLtBpl.jpg","genres":[{"id":1,"name":"Action"},{"id":5,"name":"Drama"},{"id":2,"name":"Adventure"},{"id":9,"name":"Sci Fi"}]},{"title_id":5,"title_name":"Scooby Doo","title_img":"https://images-na.ssl-images-amazon.com/images/G/01/aplusautomation/vendorimages/1cdd3ea2-f14f-416b-9aaa-644a9a01ad8c.jpg._CB321085566_.jpg","genres":[{"id":1,"name":"Action"},{"id":10,"name":"Thriller"},{"id":6,"name":"Fantasy"}]}];
这是我的 component.html 代码:
<ngb-carousel *ngIf="games" [showNavigationArrows]="showNavigationArrows" [showNavigationIndicators]="showNavigationIndicators">
<ng-template ngbSlide *ngFor="let image of games" style="">
<div class="" style="">
<div class="col-xs-3 col-md-3 col-lg-3 col-sm-6">
<img class="" src="{{image.title_img}}" width="" >
</div>
</div>
</ng-template>
</ngb-carousel>
每张幻灯片的每张图片都来了。
我尝试了如下静态图片,
<ngb-carousel *ngIf="games" [showNavigationArrows]="showNavigationArrows" [showNavigationIndicators]="showNavigationIndicators">
<ng-template ngbSlide style="">
<div class="">
<img src="https://media.services.cinergy.ch/media/box1600/f1323e57a2c4ea79dde779a89d561f85bfbe6bf5.jpg" width="200px" class="img-responsive" style="float:left">
<img src="https://www.sideshowtoy.com/assets/products/903302-neo/lg/the-matrix-neo-sixth-scale-figure-hot-toys-903302-01.jpg" width="200px" class="img-responsive" style="float:left">
<img src="http://media.comicbook.com/2018/03/avengers-infinity-war-poster-all-iron-man-version-1096449.jpeg" width="200px" class="img-responsive" style="float:left">
</div>
</ng-template>
<ng-template ngbSlide style="">
<div class="">
<img src="https://media.services.cinergy.ch/media/box1600/f1323e57a2c4ea79dde779a89d561f85bfbe6bf5.jpg" width="200px" class="img-responsive" style="float:left">
<img src="https://www.sideshowtoy.com/assets/products/903302-neo/lg/the-matrix-neo-sixth-scale-figure-hot-toys-903302-01.jpg" width="200px" class="img-responsive" style="float:left">
<img src="http://media.comicbook.com/2018/03/avengers-infinity-war-poster-all-iron-man-version-1096449.jpeg" width="200px" class="img-responsive" style="float:left">
</div>
</ng-template>
</ngb-carousel>
但是,在移动设备中,它显示的是一个低于一个。我只需要显示一张图片,点击箭头导航时应该显示下一张图片
【问题讨论】:
-
第一个代码(动态版)似乎是正确的。在第二个(静态)中,每个
ng-template部分放置 3 张图像。因此,一次显示 3 张图像是正常的。也许我误解了你的问题 -
我需要在桌面视图的动态版本中一次显示 3 张图像。当我在移动设备中查看同一部分时,我应该在滑块中一次只显示一张图像
-
所以您希望在桌面视图中每张幻灯片 3 张图片,而在移动端每次 1 张图片?
-
是的@XavierBrt ... 不仅是 3 个 ... 与屏幕一样多的图像 ... 比如桌面上一次 7 个图像,移动设备上一次一个图像
-
如果我是你,我会实现 2 个轮播,一个用于桌面,一个用于移动设备,并根据屏幕大小显示一个或另一个,使用 *ngIf 如下所示:stackoverflow.com/questions/47034573/…
标签: javascript css carousel angular6 ng-bootstrap