How to change swipe direction in swiper.js?

前端 未结 1 1936
执念已碎
执念已碎 2021-02-19 15:48

I\'m struggling with changing swipe direction when I rotate my swiper on 90deg. So in the beginning by default it has horizontal direction. When clicking on slide I\'m rotating

1条回答
  •  清酒与你
    2021-02-19 16:32

    Destroy previous Swiper and reinitialize Swiper giving the direction value. Here is doc

    DEMO

    let isVertical = true,
      direction = 'vertical';
    let swiper = initSwiper(direction);
    
    function initSwiper(direction) {
      return new Swiper('.swiper-container', {
        spaceBetween: 50,
        pagination: {
          el: '.swiper-pagination',
          clickable: true,
        },
        direction: direction
      });
    
    }
    
    function changeDirection() {
      isVertical = !isVertical;
      direction = isVertical ? 'vertical' : 'horizontal';
      let slideIndex = swiper.activeIndex;
      swiper.destroy(true, true);
      swiper = initSwiper(direction);
      swiper.slideTo(slideIndex,0);
    }
    html, body {
      position: relative;
      height: 100%;
    }
    body {
      background: #eee;
      font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
      font-size: 14px;
      color:#000;
      margin: 0;
      padding: 0;
    }
    .swiper-container {
      width: 400px;
      height: 400px;
    }
    .swiper-slide {
      text-align: center;
      font-size: 18px;
      background: #fff;
      /* Center slide text vertically */
      display: -webkit-box;
      display: -ms-flexbox;
      display: -webkit-flex;
      display: flex;
      -webkit-box-pack: center;
      -ms-flex-pack: center;
      -webkit-justify-content: center;
      justify-content: center;
      -webkit-box-align: center;
      -ms-flex-align: center;
      -webkit-align-items: center;
      align-items: center;
    }
    
    
    
    
    
    
    
    Slide 1
    Slide 2
    Slide 3

    0 讨论(0)
提交回复
热议问题