Skip to content

Instantly share code, notes, and snippets.

@aaronbalthaser
Last active August 29, 2015 14:01
Show Gist options
  • Save aaronbalthaser/053347453f419041c2d1 to your computer and use it in GitHub Desktop.
Save aaronbalthaser/053347453f419041c2d1 to your computer and use it in GitHub Desktop.
Image Rotator
1. Upload the css and js files to the server.
2. Add the link to the css file in the head of your html page.
- Example:
<link rel="stylesheet" href="css/main.css">
3. Add the link to the js file in the head of your html page or within the body tag.
- Example:
<link rel="stylesheet" href="js/main.js">
4. Add the following block of code to your html page and update the image path to your image.
<div class="rotator">
<figure class="show">
<img alt="" src="http://placehold.it/1600x800" width="100%" />&nbsp;
<figcaption>Image caption goes here.</figcaption>
</figure>
<span class="prev">«</span>
<span class="next">»</span>
</div>
5. To add more images simply paste in the following code and update the image path.
<figure>
<img alt="" src="http://placehold.it/1600x800" width="100%" width="100%" />&nbsp;
<figcaption>Image caption goes here.</figcaption>
</figure>
6. The resulting code would look like this.
<div class="rotator">
<figure class="show">
<img alt="" src="http://placehold.it/1600x800" width="100%" />&nbsp;
<figcaption>Image caption goes here.</figcaption>
</figure>
<figure>
<img alt="" src="http://placehold.it/1600x800" width="100%" />&nbsp;
<figcaption>Image caption goes here.</figcaption>
</figure>
<figure>
<img alt="" src="http://placehold.it/1600x800" width="100%" />&nbsp;
<figcaption>Image caption goes here.</figcaption>
</figure>
<span class="prev">«</span>
<span class="next">»</span>
</div>
7. To add auto rotate functionality add the auto class to the container div
- Example
<div class="rotator auto”>
</div>
<!doctype html>
<html class="no-js">
<head>
<meta charset="utf-8">
<title>Boilerplate</title>
<link rel="stylesheet" href="main.css">
</head>
<body>
<div class="rotator">
<figure class="show">
<img alt="" src="http://placehold.it/1600x800" width="100%" />&nbsp;
<figcaption>Image caption goes here.</figcaption>
</figure>
<figure>
<img alt="" src="http://placehold.it/1600x800" width="100%" width="100%" />&nbsp;
<figcaption>Image caption goes here.</figcaption>
</figure>
<span class="prev">«</span>
<span class="next">»</span>
</div>
<script src="main.js"></script>
</body>
</html>
.rotator {
position: relative;
display: block;
overflow: hidden;
}
figure {
position: absolute;
opacity: 0;
transition: 1s opacity;
}
figcaption {
position: absolute;
font-family: sans-serif;
font-size: .8em;
bottom: .75em;
right: .35em;
padding: .25em;
color: #fff;
background: rgba(0, 0, 0, .25);
border-radius: 2px;
}
figcaption a {
color: #fff;
}
figure.show {
opacity: 1;
position: static;
transition: 1s opacity;
}
.next,
.prev {
color: #fff;
position: absolute;
background: rgba(0, 0, 0, .6);
top: 50%;
z-index: 1;
font-size: 2em;
margin-top: -.75em;
opacity: .3;
user-select: none;
}
.next:hover,
.prev:hover {
cursor: pointer;
opacity: 1;
}
.next {
right: 0;
padding: 10px 5px 15px 10px;
border-top-left-radius: 3px;
border-bottom-left-radius: 3px;
}
.prev {
left: 0;
padding: 10px 10px 15px 5px;
border-top-right-radius: 3px;
border-bottom-right-radius: 3px;
}
(function() {
var counter = 0;
var auto = document.querySelector('.auto');
// gets the figure elements
var img = document.querySelectorAll('.rotator figure');
// caches the number of figure elements
var numItems = img.length;
// event utility used to deal with IE8 junk
var eventUtility = {
addEvent: function(el, type, fn)
{
if (typeof addEventListener !== 'undefined') {
el.addEventListener(type, fn, false);
} else if (typeof attachEvent !== 'undefined') {
el.attachEvent("on" + type, fn);
} else {
el["on" + type] = fn;
}
},
getTarget: function(e)
{
if (typeof e.target !== 'undefined') {
return e.target;
} else {
return e.srcElement;
}
},
preventDefault: function(e)
{
if (typeof e.preventDefault !== 'undefined') {
e.preventDefault();
} else {
e.returnValue = false;
}
}
}
// enables auto advance if the auto class is found
if (auto) {
setInterval(next, 5000);
}
// event delegation
function clickHandler(e)
{
var target = eventUtility.getTarget(e);
console.log(target);
if (target.className == 'next') {
next();
}
if (target.className == 'prev') {
prev();
}
}
// keeps track of the count variable
function showCurrent()
{
var itemToShow = Math.abs(counter % numItems);
[].forEach.call( img, function(el) {
el.classList.remove('show');
});
img[itemToShow].classList.add('show');
}
// increments the count varialble to show the next image
function next()
{
counter++;
showCurrent();
}
// decrements the count variable to show the previous image
function prev()
{
counter--;
showCurrent();
}
eventUtility.addEvent(document, 'click', clickHandler);
})();
@aaronbalthaser
Copy link
Author

My intent was for this code to work in all modern browsers as well as IE8. I wrote a cross browser event utility, then proceeded to write the code for the rotator. The event utility can be seen between lines 13 and 43. When I began writing the code for the rotator I decided to get it working with a single rotator on the page, then figure out how to adjust it for multiple rotators. Once I had multiple rotators working my intent was to then write the code to support IE8. I never made it that far. I got stuck when attempting to add support for multiple rotators. I searched Google attempting to find a solution, additionally I created a post on Stackoverflow to no avail. I simply ran out of time.

I have built Homepage rotators using JS and a few using jQuery, but I have never had to support multiple rotators on the same page. This threw me off a bit. God willing I intend on making it my business to learn how to do this. I did an exhausting search filtering through all the tutorials I can find, and was not able to find one that had the exact specifications. I plan on putting together a more comprehensive document stating my problem and posting it on other JavaScript forums.

The above code works with a single rotator and I did not have time to add IE8 support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment