2016-12-07 9 views
0

イオン2に取り組んでいます。
アレイリストにはスライドが多数ありますが、これは自動スライドです。 そのリストからランダムなスライドを選択する必要があります。どうすればいいですか?イオン2の自動ランダムスライダーの選択方法は?

***************************************************** 
Your system information: 

Cordova CLI: 6.4.0 
Ionic CLI Version: 2.1.8 
Ionic App Lib Version: 2.1.4 
ios-deploy version: Not installed 
ios-sim version: 5.0.8 
OS: OS X Yosemite 
Node Version: v6.2.2 
Xcode version: Xcode 7.2 Build version 7C68 
****************************************************** 

mypage.html--

<ion-slides #mySlider [options]="mySlideOptions"> 
    <ion-slide *ngFor="let quote of slides"> 
    <div class="myslider"> 
     <div class="quote-container" [innerHTML]="quote.title"></div> 
     <div [innerHTML]="quote.author" ></div> 
    </div> 
    </ion-slide> 
</ion-slides> 

page.ts--

slides: Slide[]; 
this.slides = [ 
     {id: 1, author: "-Sanjay",  title: "my text goes here."}, 
     {id: 2, author: "-Sanjay",  title: "my text goes here"}, 
     {id: 3, author: "-Sanjay",  title: "my text goes here"}, 
     {id: 4, author: "-Sanjay",  title: "my text goes here"}] 

したままの私が持っているコードslider-

// quotes slider 
    mySlideOptions = { 
    initialSlide: 0, 
    loop: true, 
    effect: 'fade', 
    fade: {crossFade:true}, 
    autoplay:2700, 
    autoplayDisableOnInteraction: true, 
    direction:"horizontal", 
    speed:4000, 
    nextButton: ".pause-me", 
    prevButton: ".swiper-button-prev" 
    }; 

でもないのFonction無作為にスライドして、何か助けてくれる?

+0

から取得された、あなたが表示したいですランダムな順序でイメージ? – Sakuto

+0

はい、そのようなものですが、すべての画像は配列リストになければなりません。 –

答えて

1

以下の方法でスライドが自動スライドし、スライドが変わるたびに新しい(ランダムな)スライドが表示されます。 randomメソッドは、スライドリストの長さを使用してランダムインデックスを計算します(0〜最大リスト長 - 1)。

TS

import { ViewChild } from '@angular/core'; 
import { Slides } from 'ionic-angular'; 

class MyPage { 
    @ViewChild('mySlider') slider: Slides; 


    .... 

    getRandomIndex(): number { 
    // uses your list.length to get a random value within the range 
    return Math.floor(Math.random() * this.slides.length) 
    } 

    newSlide() { 
    this.slider.slideTo(this.getRandomIndex(),500) 
    } 
} 

HTML

<ion-slides #mySlider (ionDidChange)="newSlide()" ........>

この答えのすべての情報は、あなたの質問が明確ではないthe ionic docs

+0

ありがとう@ivaro、それは私のために働いた。 –

関連する問題