Ionic slides - dynamically add slides before and after

前端 未结 2 656
悲哀的现实
悲哀的现实 2020-12-19 22:36

Hi I\'m using ngFor to create an set of 3 slides while starting in the middle so I\'m guaranteed to be able to slide to left or right on start.

When I slide right I

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-19 23:27

    You can do that by using the ionSlideNextEnd and ionSlidePrevEnd events from the Slides. Please take a look at this working plunker

    The view

    
      
        Dynamic slides Demo
      
    
    
        
            
                

    Current slide: {{n}}

    The component

    @Component({...})
    export class HomePage {
        @ViewChild('slider') private slider: Slides;
    
        numbers = [0,1,2];
        firstLoad = true;
    
        constructor() {}
    
        loadPrev() {
            console.log('Prev');
            let newIndex = this.slider.getActiveIndex();
    
            newIndex++;
            this.numbers.unshift(this.numbers[0] - 1);
            this.numbers.pop();
    
            // Workaround to make it work: breaks the animation
            this.slider.slideTo(newIndex, 0, false);
    
            console.log(`New status: ${this.numbers}`);
        }
    
        loadNext() {
            if(this.firstLoad) {
              // Since the initial slide is 1, prevent the first 
              // movement to modify the slides
              this.firstLoad = false;
              return;
            }
    
            console.log('Next');
            let newIndex = this.slider.getActiveIndex();
    
            newIndex--;
            this.numbers.push(this.numbers[this.numbers.length - 1] + 1);
            this.numbers.shift();
    
            // Workaround to make it work: breaks the animation
            this.slider.slideTo(newIndex, 0, false);
    
            console.log(`New status: ${this.numbers}`);
        }
    }
    

提交回复
热议问题