1
\$\begingroup\$

An implementation of an image-slider I figured out.

Could it be further improved? Or should I do it in a completely different way?

Not sure if the technique of assigning a function to addEventListener by using bind works as expected.

Are there possible flaws with the technique? Should I replace it with the old technique of calling a function (which needs parameter) within a anonymous function?

var boxes      = document.querySelectorAll('.box');
var prevButton = document.querySelector('#prev');
var nextButton = document.querySelector('#next');
var images     = document.querySelectorAll('.slideshow-image');

try {
  var counter = new Counter(images.length);
} catch (e) {
  console.log(e.message);
}

/**
* Manages a counter-variable. Provides methods for 
*  incrementing / decrementing the counter-variable.
* 
* @param { number } collectionSize - The length of an 
*  array or nodeList.
*
* @throws { object } - When assigned parameter 'is not number' 
*  or 'is NaN' or is 0.
*/
function Counter(collectionSize) {
  var i = 0;

  if ( typeof collectionSize !== 'number' ||
       collectionSize !== collectionSize || 
       collectionSize === 0 ) {
    throw new Error('Assigned parameter "collectionSize" invalid.');
  }

  this.increment = function() {

    i++;
    i = i % collectionSize;

    return i;
  }

  this.decrement = function() {

    i--;
    i = (i + collectionSize) % collectionSize;

    return i;
  }
}

/**
*  Changes the image shown within the div-container.
*  
*  @param { function } counterMethod - Method of an 
*   counter-object.			
*/ 
function changeImage(counterMethod) {

  document.querySelector('.current').classList.remove('current');

  images[counterMethod()].classList.add('current');
}

prevButton.addEventListener('click', changeImage.bind(null, counter.decrement));			   
nextButton.addEventListener('click', changeImage.bind(null, counter.increment));
body {
  background-color: rgba(240, 240, 240, 1.0);
}

.wrap {
  width: 462px;
  margin: 30px auto;
  border: 1px solid grey;
  padding: 20px 30px;
  border-radius: 6px;
  background-image: radial-gradient( circle closest-corner, #898989, #dedede );
}

.wrap, img, .slideshow-nav {
  box-shadow: 1px 1px 1px black;
}

.slideshow-image {		 
  display: none;
}

.slideshow {
  width: 412px;
  margin: auto;
}

.current {
  display: block;
}

img {
  display: inline-block;
  padding: 5px;
  border: 1px solid grey;
  border-radius: 6px;
  background-color: rgba(230, 230, 230, 0.9);
}

.buttons {
  display: flex;
  justify-content: space-around;
  margin: 10px 0 20px;
}

.slideshow-nav {
  text-decoration: none;
  color: white;
  background-color: rgba(0, 0, 0, 0.7);
  border: 3px solid rgba(230, 230, 230, 0.9);
  font-size: 1.5em;
  font-family: verdana, sans-serif;
  font-weight: 800;		  
  padding: 0 20px;
  border-radius: 8px;
}

.slideshow-nav:hover {
  background-color: rgba(0, 0, 0, 0.1);
}

.slideshow-nav:active {
  color: rgba(0, 0, 0, 0.9);
  background-color: rgba(240, 240, 240, 1.0);
  border: 3px solid rgba(0, 0, 0, 0.9);
}
<div class="wrap">
  <div class="slideshow">
    <div class="boxes">
      <div class="box">
        <img class="slideshow-image current" title="pic1"
             src="http://placehold.it/400x200/ff0000" alt="pic1" />
      </div>
      <div class="box">
        <img  class="slideshow-image" title="pic2"
             src="http://placehold.it/400x200/00ff00" alt="pic2" />
      </div>
      <div class="box">
        <img class="slideshow-image" title="pic3"
             src="http://placehold.it/400x200/0000ff" alt="pic3" />
      </div>
      <div class="box">
        <img class="slideshow-image" title="pic4"
             src="http://placehold.it/400x200/ff00ff" alt="pic1" />
      </div>
      <div class="box">
        <img class="slideshow-image" title="pic5"
             src="http://placehold.it/400x200/ffff00" alt="pic1" />
      </div>
      <div class="box">
        <img class="slideshow-image" title="pic6"
             src="http://placehold.it/400x200/00ffff" alt="pic1" />
      </div>

    </div>
    <div class="buttons">
      <a href="#" id="prev" class="slideshow-nav"><<</a>
      <a href="#" id="next" class="slideshow-nav">>></a>
    </div>
  </div>
</div>

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.