1
\$\begingroup\$

I have a really basic image carousel that I just wrote.

Basically I'm trying to keep it as small and light weight as possible, but I get the feeling that I've gotten way off the beaten path of how these things normally work.

function hide(element, index, array) {
 if (index > 0) {
 slides[index].setAttribute('style', 'opacity:0;');
 }
}
var carousel = document.getElementById("carousel"),
 slides = carousel.getElementsByTagName('li'),
 counter = 0,
 liList = Array.prototype.slice.call(slides);
setInterval(function() {
 slides[counter].setAttribute('style', 'opacity:1;');
 counter++;
 if (counter == slides.length) {
 counter = 0;
 setTimeout(function() {
 liList.forEach(hide);
 }, 3000); // setTimeout
 }
}, 3000); // setInterval
#carousel {
 padding: 0;
 margin: 0;
 position: relative;
 width: 315px;
 height: 177px;
}
#carousel li {
 opacity: 0;
 list-style: outside none none;
 width: 315px;
 position: absolute;
 background: #fff;
 transition: opacity 1s;
}
<ul id="carousel">
 <li style="opacity:1;">
 <img src="http://pluggedinwebdesign.com/images/labyrinth.jpg" alt="" />
 </li>
 <li>
 <img src="http://pluggedinwebdesign.com/images/RopesLittlePlanet2.jpg" alt="" />
 </li>
 <li>
 <img src="http://pluggedinwebdesign.com/images/FireRing2.jpg" alt="" />
 </li>
</ul>

Could this be trimmed down further, is this a bad way to go about something this simple, or am I just over thinking it?

asked Nov 5, 2014 at 2:38
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

How about this: every 3 seconds, you hide the previous image and show the next image. This is simple.

Some changes I made:

  1. I prefer .style.opacity = as opposed to setAttribute("style"). This may be personal preference, but it looks cleaner to me because it uses javascript's object access instead of setting it via a string.
  2. You can use a modulus (%) instead of checking if counter === slides.length.

var carousel = document.getElementById("carousel"),
 slides = carousel.getElementsByTagName('li'),
 counter = 0,
 liList = Array.prototype.slice.call(slides);
setInterval(function() {
 slides[counter].style.opacity = 0; // Hide the previous image
 counter = (counter + 1) % slides.length; // Increment counter
 slides[counter].style.opacity = 1; // Show the next image
}, 3000); // setInterval
#carousel {
 padding: 0;
 margin: 0;
 position: relative;
 width: 315px;
 height: 177px;
}
#carousel li {
 opacity: 0;
 list-style: outside none none;
 width: 315px;
 position: absolute;
 background: #fff;
 transition: opacity 1s;
}
<ul id="carousel">
 <li style="opacity:1;">
 <img src="http://pluggedinwebdesign.com/images/labyrinth.jpg" alt="" />
 </li>
 <li>
 <img src="http://pluggedinwebdesign.com/images/RopesLittlePlanet2.jpg" alt="" />
 </li>
 <li>
 <img src="http://pluggedinwebdesign.com/images/FireRing2.jpg" alt="" />
 </li>
</ul>

answered Nov 5, 2014 at 2:52
\$\endgroup\$
2
  • \$\begingroup\$ Can you explain this line: counter = (counter + 1) % slides.length; It looks great I'm just not too sure how that bit works \$\endgroup\$ Commented Nov 5, 2014 at 3:38
  • \$\begingroup\$ @apaul34208 Basically, it's a remainder operator. It's used frequently when looping back around an array. Example: 8 % 3 === 2 because 8 leaves a remainder of 2 when divided by 3. So when counter + 1 === slides.length, it resets to zero because slides.length / slides.length always leaves a remainder of 0. \$\endgroup\$ Commented Nov 5, 2014 at 3:39

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.