javascriptjqueryhtml

Showing number of slides on the web page


I wrote a custom slider as shown in this codepen:
http://codepen.io/anon/pen/NqQpjG
I added extra feature which counts total number of slides that are moved. For example, if total slides are 8, then starting value would be like this:
Slides: 0/8. When one slide moves, it becomes Slides: 1/8 and so on. Here is my code:

<div>
            <span id="numberSlides">0/0 Offers</span>
        </div>  

JS

var slide = new slide('.swiper-slide', {
        var totalSlides = 10;
        var newSlide = document.getElementsByClassName('swiper-slide');
        var newValue = newSlide + 1;
        document.getElementById('numberSlides').innerHTML = newValue + "/" + totalSlides + " Slides";
    });   

But it isn't working. How can I display the number of slides that are moved?
EDIT:
My slides are in these divs:

<div class="swiper-container">
        <div class="swiper-wrapper">
            <div class="swiper-slide">Slide 1</div>
            <div class="swiper-slide">Slide 2</div>
            <div class="swiper-slide">Slide 3</div>
            <div class="swiper-slide">Slide 4</div>
            <div class="swiper-slide">Slide 5</div>
            <div class="swiper-slide">Slide 6</div>
            <div class="swiper-slide">Slide 7</div>
            <div class="swiper-slide">Slide 8</div>
            <div class="swiper-slide">Slide 9</div>
            <div class="swiper-slide">Slide 10</div>
        </div>
        <!-- Add Pagination -->
        <div class="swiper-pagination"></div>
    </div>

Solution

  • Use this properties

      runCallbacksOnInit: true, 
      onInit: function(sw){}, //runs callback in initialization
      onSlideChangeEnd: function(sw){} //runs callback in slide change end
    

    Demo

    var swiper = new Swiper('.swiper-container', {
      pagination: '.swiper-pagination',
      paginationClickable: true,
      nextButton: '.swiper-button-next',
      prevButton: '.swiper-button-prev',
      spaceBetween: 30,
      centeredSlides: true,
      autoplay: 3000,
      autoplayDisableOnInteraction: false,
      runCallbacksOnInit: true,
      onInit: function(sw){
        var offer = document.querySelector('#numberSlides');
        offer.innerHTML = (sw.activeIndex +  1) + '/' + sw.slides.length + 'Offers';
      },
      onSlideChangeEnd: function(sw){
        var offer = document.querySelector('#numberSlides');
        offer.innerHTML = (sw.activeIndex +  1) + '/' + sw.slides.length + 'Offers';
      }
    });
    #numberSlides{
      position: absolute; 
      bottom: 0;
      left: 0;
      z-index: 9999;
    }
    
    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: 100%;
      height: 100%;
    }
    
    .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;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/3.1.2/js/swiper.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/3.1.2/css/swiper.css">
    <div class="swiper-container">
      <div class="swiper-wrapper">
        <div class="swiper-slide">Slide 1</div>
        <div class="swiper-slide">Slide 2</div>
        <div class="swiper-slide">Slide 3</div>
        <div class="swiper-slide">Slide 4</div>
        <div class="swiper-slide">Slide 5</div>
        <div class="swiper-slide">Slide 6</div>
        <div class="swiper-slide">Slide 7</div>
        <div class="swiper-slide">Slide 8</div>
        <div class="swiper-slide">Slide 9</div>
        <div class="swiper-slide">Slide 10</div>
      </div>
      <!-- Add Pagination -->
      <div class="swiper-pagination swiper-pagination-clickable"></div>
      <div id="numberSlides">
        0/0 Offers
      </div>
    </div>