JSFiddle - React, Tailwind, and code Playground
by Simon Pratt
HTML
<body>
<div id="wrapper">
<div id="slider">
<!--navigation left/right buttons-->
<div id="navigation">
<div id="nav-prev" class="nav-button">«</div>
<div id="nav-next" class="nav-button">»</div>
</div>
<!--slideshow-->
<figure id="inner-slider">
<img class="item" src="http://placehold.it/700x300&text=1">
<img class="item" src="http://placehold.it/700x300&text=2">
<img class="item" src="http://placehold.it/700x300&text=3">
<img class="item" src="http://placehold.it/700x300&text=4">
<img class="item" src="http://placehold.it/700x300&text=5">
</figure>
<!--container for the js-created pagination buttons-->
<div id="pagination">
<!-- <div id="circle-fill"></div>
<div id="circle-border"></div> -->
</div>
</div>
</div>
<script type="text/javascript" src="js/simon-slider.js"></script>
</body>
CSS
/* http://meyerweb.com/eric/tools/css/reset/
v2.0 | 20110126
License: none (public domain)
*/
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline; }
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block; }
body {
line-height: 1; }
ol, ul {
list-style: none; }
blockquote, q {
quotes: none; }
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none; }
table {
border-collapse: collapse;
border-spacing: 0; }
#wrapper {
margin: 50px auto;
width: 700px; }
#slider {
position: relative;
width: 100%;
max-width: 700px;
overflow: hidden; }
#slider #inner-slider {
position: relative;
width: 500%;
margin: 0;
padding: 0;
font-size: 0;
text-align: left; }
.item {
width: 20%;
height: auto;
float: left; }
#pagination {
position: absolute;
z-index: 1;
right: 0;
bottom: 0;
/*#circle-fill {
width: 20px;
height: 20px;
-webkit-border-radius: 50%;
-moz-border-radius: 50%;
border-radius: 50%;
background: deepskyblue;
}
#circle-border {
}*/ }
#pagination div {
display: inline-block;
margin: 0 5px 0 0;
padding: 0;
width: 14px;
height: 14px;
-webkit-border-radius: 50%;
-moz-border-radius: 50%;
border-radius: 50%;
border: 3px solid deepskyblue;
opacity: 0.5; }
#pagination...
JavaScript
(function simonSlider() {
//find the size of the viewport of the slider
var imageWidth = $("#slider").width();
//count number of images in slideshow
var totalItems = document.getElementsByClassName('item').length;
//find outer limit for position.left - for buffer in nav and autoplay
var outerLimit = imageWidth * (totalItems - 1);
outerLimit = -outerLimit;
console.log("Slider viewport: " + imageWidth + "px");
console.log("Number of images in slideshow: " + totalItems);
console.log("Outer limit for positon.left: " + outerLimit);
//CREATE PAGINATION
function addElement() {
var currentDiv = document.getElementById("pagination");
var counter = 0;
//make corresponding buttons for each image
for (var i = 0; i < totalItems; i++) {
var newButton = document.createElement("div");
//add separate id for each page button created
newButton.id = 'page' + ++counter;
//var newContent = document.createTextNode("page" + (i + 1));
//newButton.appendChild(newContent);
//add newly created element into DOM
currentDiv.appendChild(newButton);
}
}
addElement();
//SET THE PAGINATION BUTTON CORRESPONDING TO THE CURRENTLY DISPLAYED IMAGE AS ACTIVE/FILLED
function setFilled() {
var position = $("#inner-slider").position();
//remove filled class from currently selected page button
$("#pagination div").removeClass("filled");
//add filled class to page button corresponding to currently displayed image
n = position.left / imageWidth;
n *= -1;
$('#page' + (n + 1)).addClass('filled');
console.log(position);
}
setFilled();
//AUTOPLAY ANIMATION (and return to start at end of loop)
function autoPlay() {
var position = $("#inner-slider").position();
if (position.left <= outerLimit) {
...