setTimeout In HTML/CSS/JavaScript slideshow - javascript

I have a simple slider made with HTML/CSS/Javascript
My problem is that I don't know how to make a simple setTimeout. My idea is that the slides
must change when you click on any of the control dots ( that is already done by me and it works as expected). But a timer needs to work there too, so every 4 seconds this timer must change the active slide and if a control dot is clicked, the timer must resets and counts for another 4 seconds.
Here is my code
HTML
<div class="slideshow-container">
<div class="slides">
<div class="intro-slide fade">
<h2>title</h2>
<p>
text
</p>
</div>
<!--slide-->
<div class="intro-slide fade">
<h2>title</h2>
<p>
text
</p>
</div>
<!--slide-->
<div class="intro-slide fade">
<h2>title</h2>
<p>
text
</p>
</div>
<!--slide-->
</div>
<!--slides-->
<div class="control-dots">
<span class="dot" onclick="currentSlide(1)"></span>
<span class="dot" onclick="currentSlide(2)"></span>
<span class="dot" onclick="currentSlide(3)"></span>
</div>
<!--control-dots
</div>
<!--slideshow-container -->
SCSS
.slideshow-container {
width: 570px;
h2 {
font-weight: 800;
font-size: 34px;
line-height: 1.27;
margin-bottom: 20px;
font-family: 'Raleway';
}
.control-dots {
width: 100%;
text-align: center;
margin-bottom: 40px;
}
.dot {
cursor: pointer;
height: 15px;
width: 15px;
margin: 0 2px;
background-color: #bbb;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
&:hover,
&.active {
background-color: #fff;
}
}
}
Javascript
var slideIndex = 1;
showSlides(slideIndex);
function plusSlides(n) {
showSlides(slideIndex += n);
}
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("intro-slide");
var dots = document.getElementsByClassName("dot");
if (n > slides.length) {
slideIndex = 1
}
if (n < 1) {
slideIndex = slides.length
}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[slideIndex - 1].style.display = "block";
dots[slideIndex - 1].className += " active";
setTimeout(showSlides, 4000); // Change image every 4 seconds
}
Thanks in advance

So cancel the timeout if it is running. The timeout returns the id of the timer. So you store that into a variable and cancel it.
function plusSlides(n) {
n = n || 1
showSlides(slideIndex += n);
}
var activeTimer
function showSlides(n) {
if (activeTimer) window.clearTimeout(activeTimer)
// ...
activeTimer = window.setTimeout(plusSlides, 4000);
}

Related

Javascript interfering with CSS Transition

I've recently started work on a basic landing-page website. Part of the page is a basic image slider with both the auto-nav and the manual nav being powered by JS. I got everything working but for one thing: I just can't figure out how to get the smooth transition between images - as seen in this example - to work. I figured out the problem after reading some related questions on Stackoverflow: To make the Slideshow work I'm using slides[i].style.display = "none"; and slides[slideIndex-1].style.display = "block"; to update the css code handling the 'display' element - this over rights the 'transition: 2s' css code one would need for the simple slide animation as seen in the video.
As I'm terribly new to Web development I could not wrap my head around possible solutions posted here on Stackoverflow and would really appreciate anyone that could help me out with an answer to my specific problem or an alternative way to solve it.
Cheers,
Jerome
var slideIndex = 1;
showSlides(slideIndex);
// Next/previous controls
function plusSlides(n) {
showSlides(slideIndex += n);
}
// Thumbnail image controls
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("item");
var dots = document.getElementsByClassName("dot");
if (n > slides.length) {
slideIndex = 1
}
if (n < 1) {
slideIndex = slides.length
}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[slideIndex - 1].style.display = "block";
dots[slideIndex - 1].className += " active";
}
//automatic
var slideIndexAuto = 0;
showSlidesAuto();
function showSlidesAuto() {
var i;
var slides = document.getElementsByClassName("item");
var dots = document.getElementsByClassName("dot");
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
dots[i].className = dots[i].className.replace(" active", "");
}
slideIndexAuto++;
if (slideIndexAuto > slides.length) {
slideIndexAuto = 1
}
slides[slideIndexAuto - 1].style.display = "block";
dots[slideIndexAuto - 1].className += " active";
setTimeout(showSlidesAuto, 5000);
}
.slider {
width: 65%;
max-width: 940px;
height: 500px;
border-radius: 0.25rem;
position: relative;
overflow: hidden;
}
.slider .left-slide,
.slider .right-slide {
position: absolute;
height: 40px;
width: 40px;
background-color: #444444;
border-radius: 50%;
color: #ffffff;
font-size: 20px;
top: 50%;
cursor: pointer;
margin-top: -20px;
text-align: center;
line-height: 40px;
}
.slider .left-slide:hover,
.slider .right-slide:hover {
box-shadow: 0px 0px 10px black;
background-color: #29a8e2;
}
.slider .left-slide {
left: 30px;
}
.slider .right-slide {
right: 30px;
}
.slider .slider-items .item img {
width: 100%;
height: 500px;
display: block;
}
.slider .slider-items .item {
position: relative;
transition: 4s;
}
.slider .slider-items .item .caption {
position: absolute;
width: 100%;
height: 100%;
bottom: 0px;
left: 0px;
background-color: rgba(0, 0, 0, .5);
}
<div class="slider">
<div class="slider-items">
<div class="item fade">
<img src="/images/cs-slider-high.jpg" />
<div class="caption">
<p class="caption-text">COMING</p>
<p class="caption-text">OKTOBER 10th</p>
</div>
</div>
<div class="item fade">
<img src="/images/building-slider.jpg" />
<div class="caption">
<p class="caption-text-2">Blackstoneroad 109</br>
</p>
</div>
</div>
<div class="item fade">
<img src="/images/kontact-slider.jpg" />
<div class="caption">
<p class="caption-text-3">Coffee<br>Drinks<br>Food<br>& More</p>
</div>
</div>
<div class="item fade">
<img src="/images/seminar-slider.jpg" />
<div class="caption">
<p class="caption-text-3">Seminar Rooms<br>Inspiration<br>Shopping<br>& More</p>
</div>
</div>
</div>
<!-- Next and previous buttons -->
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
<!-- The dots/circles -->
<div class="align-text-center">
<span class="dot" onclick="currentSlide(1)"></span>
<span class="dot" onclick="currentSlide(2)"></span>
<span class="dot" onclick="currentSlide(3)"></span>
<span class="dot" onclick="currentSlide(3)"></span>
</div>
How is this: http://jsfiddle.net/lharby/qox05y96/
For simplification I have stripped out the dot animation (although that code is closer to the effect you want).
Here is the simplified JS:
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("item");
var dots = document.getElementsByClassName("dot");
if (n > slides.length) {
slideIndex = 1
}
if (n < 1) {
slideIndex = slides.length
}
for (i = 0; i < slides.length; i++) {
slides[i].classList.remove('active'); // this is updated
}
slides[slideIndex - 1].className += " active";
}
//automatic
var slideIndexAuto = 0;
showSlidesAuto();
function showSlidesAuto() {
var i;
var slides = document.getElementsByClassName("item");
var dots = document.getElementsByClassName("dot");
for (i = 0; i < slides.length; i++) {
slides[i].classList.remove('active'); // this is updated
}
slideIndexAuto++;
if (slideIndexAuto > slides.length) {
slideIndexAuto = 1
}
slides[slideIndexAuto - 1].classList.add('active'); // this is updated
setTimeout(showSlidesAuto, 4000);
}
This means we also have to change the css. display none/block is harder to animate with css transitions. So I have used opacity: 0/1 and visibility: hidden/visible. However one other trade off is that that the item elements cannot be positioned relatively this would stack them on top of one another (or side by side) usually for an animated slideshow you would use position absolute for each slide, but I realise that causes you another issue.
CSS:
.slider .slider-items .item {
visibility: hidden;
opacity: 0;
transition: 4s;
}
.slider .slider-items .item.active {
visibility: visible;
opacity: 1;
transition: 4s;
}
At least now all of the css is handled within the css and the js purely adds or removes a class name which I feel makes it a bit easier to update.
There are priorities as far as what CSS deems should happen.
Take a look at this and let me know if it changes anything for you. If not I'll try and run the code and fix your errors. I want to let you know in advance, I am a pure JS developer. When I need CSS or HTML I create it in JS. I want you to try first, thus it will make you a better developer.
https://www.thebookdesigner.com/2017/02/styling-priorities-css-for-ebooks-3/

creating html div from json using javascript

i wanted to create a sliding image gallery in a div. To get the images and the description i make a get request on an api. The returned json is as expected and until that, the code works.
I have found this sliding gallery on W3Ccourses https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_slideshow_auto and i wanted to use it with my data.
I get these 2 errors:
Unable to get property 'style' of undefined or null reference Here is the code
'require' is not defined
i have tried with require.js but it didn't work. I tried to create my div even with document.createElement and then .appendChild but it didn't work. I don't think the issue is in creating the html element. Thanks for replying
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {box-sizing: border-box;}
body {font-family: Verdana, sans-serif;}
.mySlides {display: none;}
img {vertical-align: middle;}
/* Slideshow container */
.slideshow-container {
max-width: 1000px;
position: relative;
margin: auto;
}
/* Caption text */
.text {
color: #f2f2f2;
font-size: 15px;
padding: 8px 12px;
position: absolute;
bottom: 8px;
width: 100%;
text-align: center;
}
/* Number text (1/3 etc) */
.numbertext {
color: #f2f2f2;
font-size: 12px;
padding: 8px 12px;
position: absolute;
top: 0;
}
/* The dots/bullets/indicators */
.dot {
height: 15px;
width: 15px;
margin: 0 2px;
background-color: #bbb;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
}
.active {
background-color: #717171;
}
/* Fading animation */
.fade {
-webkit-animation-name: fade;
-webkit-animation-duration: 1.5s;
animation-name: fade;
animation-duration: 1.5s;
}
#-webkit-keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
#keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
/* On smaller screens, decrease text size */
#media only screen and (max-width: 300px) {
.text {font-size: 11px}
}
</style>
</head>
<!-- ####################################################### -->
<body>
<div class="slideshow-container">
<!-- ### want to get this kind of div , but with my data ###
<div class="mySlides fade">
<div class="numbertext">1 / 3</div>
<img src="img_nature_wide.jpg" style="width:100%">
<div class="text">Caption Text</div>
</div> -->
<script type="text/javascript">
url = 'http://www.myUrl.com';
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var httpReq = new XMLHttpRequest();
httpReq.open("GET" , url , false);
httpReq.send(null);
var jsonObj = JSON.parse(httpReq.responseText);
var description = jsonObj[i].description
for ( i = 0 ; i < 9 ; i++) {
document.writeln('<div class=\"mySlides fade\">');
document.writeln('<div class = \"numbertext\">' + i + '/9</div>');
document.writeln('<img src=\"' + jsonObj[i].url_image + '\" style="width :100%">');
document.writeln('<div class=\"text\">' + description + '</div>');
}
</script>
</div>
<div style="text-align:center">
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
</div>
<script>
var slideIndex = 0;
showSlides();
function showSlides() {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
slideIndex++;
if (slideIndex > slides.length) {slideIndex = 1}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[slideIndex-1].style.display = "block";
dots[slideIndex-1].className += " active";
setTimeout(showSlides, 2000); // Change image every 2 seconds
}
</script>
</body>
</html>
I checked you code and I notice three things,
You don't need this line var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; you don't need to require it here as the XMLHttpRequest is part of the windows object.
Your usage of the xmlhttprequest method is wrong see correct implementation below.
var url = 'http://www.myUrl.com';
var httpReq = new XMLHttpRequest();
httpReq.open("GET" , url);
httpReq.send(null);
var jsonObj, description;
httpReq.onload = function() {
if (httpReq.status == 200) { // analyze HTTP status of the response
var jsonObj = JSON.parse(httpReq.responseText);
var description = jsonObj[i].description // e.g. 404: Not Found
for ( i = 0 ; i < 9 ; i++) {
document.writeln('<div class=\"mySlides fade\">');
document.writeln('<div class = \"numbertext\">' + i + '/9</div>');
document.writeln('<img src=\"' + jsonObj[i].url_image + '\" style="width :100%">');
document.writeln('<div class=\"text\">' + description + '</div>');
}
var slideIndex = 0;
showSlides();
} else { // show the result
alert(`Error: httpReq.status`); // responseText is the server
}
};
You will get a CORS error if you are not running this from the same origin as http://www.myUrl.com
Hope this helps.
You can use ajax request to retrieve your json data, then loop each object while appending a mySlides div
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script>
<script>
$(document).ready(function(){
$.getJSON("https://jsonplaceholder.typicode.com/todos/1", function(result){
$.each(result, function(i, field){
$("div.slideshow-container").append(`<div class="mySlides fade"><div class="numbertext">1 / 3</div><img src="${field.url}" style="width:100%"><div class="text">${field.caption}</div></div>`);
});
});
showSlides();
});
</script>

How to create divs for slideshow image change left/ right on click?

I am trying to create a div that is 50% width left and another 50% width right of the viewport. I want each div to change the slideshow image previous / next on click
Example: https://jonoverrall.com/colin/owzib05x14nq1xd08etgqoe7nmmvum
Does anyone have any suggestions on how to achieve this?
<div class="slideshow-container">
<div class="mySlides fade">
<img src="001%20(1).jpg" onclick="plusSlides(1)">
<div class="text">As a Fountain 2017</div>
<div class="opacity">001 </div>
</div>
<div class="mySlides fade">
<img src="001%20(2).jpg" onclick="plusSlides(1)">
<div class="text">Deliverance 2017</div>
<div class="opacity">002 </div>
</div>
CSS
img {
height: 100%;
width: 100%;
max-height: 100vh;
max-width: 100vh;
object-fit: contain;
}
.mySlides img {
display: inline-block;
vertical-align: middle;
}
.slideshow-container img {
display: block;
margin: 0px auto;
}
JS
var slideIndex = 1;
var indexes = document.querySelectorAll(".numbertext span");
var slides = document.getElementsByClassName("mySlides");
indexes[1].innerHTML = slides.length;
showSlides(slideIndex);
function plusSlides(n) {
showSlides(slideIndex += n);
}
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides(n) {
var i;
if (n > slides.length) {
slideIndex = 1
}
if (n < 1) {
slideIndex = slides.length
}
indexes[0].innerHTML = slideIndex;
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
slides[slideIndex - 1].style.display = "block";
}
https://codepen.io/anon/pen/xobQRM

Home Page Banner Slideshow Not Functioning

I'm trying to have an image slideshow with change captions on a home page I'm working on. I was using w3schools as the tutorial and I have it working on there, but when I put the code into my text editor nothing happens; nothing is rendering. I'm using Atom and running the code in its html preview screen and have the file open in my browser. I'm using external CSS and JavaScript files and using css-grid for the layout.
Here's my HTML:
<div class="slideshow-container">
<div class="mySlides fade">
<img src="headerpark.jpg" style="width:100%">
<div class="text">Caption One</div>
</div>
<div class="mySlides fade">
<img src="headerpark2.jpg" style="width:100%">
<div class="text">Caption Two</div>
</div>
<div class="mySlides fade">
<img src="headerpark3.jpg" style="width:100%">
<div class="text">Caption Three</div>
</div>
</div>
My CSS:
.mySlides {display: none}
.slideshow-container {
grid-area: banner;
height: auto;
max-height: 100vh;
position: relative;
justify-self: stretch;
}
.slideshow-container img {
vertical-align: middle;
}
.text {
color: #f2f2f2;
font-size: 15px;
padding: 8px 12px;
position: absolute;
bottom: 8px;
width: 100%;
text-align: center;
}
And lastly my JavaScript:
var slideIndex = 0;
showSlides(slideIndex);
function showSlides() {
var i;
var slides = document.getElementsByClassName("mySlides");
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
slideIndex++;
if (slideIndex > slides.length) {slideIndex = 1}
slides[slideIndex-1].style.display = "block";
setTimeout(showSlides, 5000);
}
Your code looks fine. Try copying it into an online IDE such as codepen.io and see if it works. It may just be your browser (emulator?).
It was trying to set block on the first slide before the slides collection existed, making sure slides is not undefined before calling that line fixes it.
var slideIndex = 0;
showSlides(slideIndex);
function showSlides() {
var i;
var slides = document.getElementsByClassName("mySlides");
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
console.log(slideIndex);
slideIndex++;
if (slideIndex > slides.length) {slideIndex = 1}
if( typeof slides[slideIndex-1] !== 'undefined') {
slides[slideIndex-1].style.display = "block";
}
setTimeout(showSlides, 5000);
}
Now if you want to get a little fancier, this is a solution from a friend that creates a class so you can do things like instantiate it with options like time, or create a pause. Both solutions work with your code just by replacing the JS
class Slideshow {
constructor({delay = 5000, selector} = {}) {
this.delay = delay
this.index = 0
this.selector = selector
this.timeoutRef = undefined
}
play() {
console.log(this.index)
const slides = document.querySelectorAll('.mySlides')
slides.forEach(slide => slide.style.display = 'none')
slides.item(this.index).style.display = 'block'
this.index = this.index >= slides.length - 1 ? 0 : this.index + 1
this.timeoutRef = setTimeout(this.play.bind(this), this.delay)
}
}
const slideshow = new Slideshow({ selector: '.mySlides'})
slideshow.play()

Reduce code length

I want to have multiple independent slideshows in the same webpage I have this script and is working well but I want to know is there a shorter way to make this because I need to add five more slideshows to the page I used this example from w3 slideshow
var slideIndex = 1;
showSlides(slideIndex);
function plusSlides(n) {
"use strict";
showSlides(slideIndex += n);
}
function currentSlide(n) {
"use strict";
showSlides(slideIndex = n);
}
function showSlides(n) {
"use strict";
var i;
var slides = document.getElementsByClassName("comment-box");
var dots = document.getElementsByClassName("dot");
if (n > slides.length) {
slideIndex = 1;
}
if (n < 1) {
slideIndex = slides.length;
}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[slideIndex - 1].style.display = "block";
slides[slideIndex - 1].style.margin = "0 auto";
dots[slideIndex - 1].className += " active";
}
var slideIndex1 = 1;
showSlides1(slideIndex1);
function plusSlides1(n) {
"use strict";
showSlides1(slideIndex1 += n);
}
function currentSlide1(n) {
"use strict";
showSlides1(slideIndex1 = n);
}
function showSlides1(n) {
"use strict";
var j;
var slides1 = document.getElementsByClassName("mini-box1");
var dots1 = document.getElementsByClassName("dot1");
if (n > slides1.length) {
slideIndex1 = 1;
}
if (n < 1) {
slideIndex1 = slides1.length;
}
for (j = 0; j < slides1.length; j++) {
slides1[j].style.display = "none";
}
for (j = 0; j < dots1.length; j++) {
dots1[j].className = dots1[j].className.replace(" active", "");
}
slides1[slideIndex1 - 1].style.display = "block";
slides1[slideIndex1 - 1].style.margin = "0 auto";
dots1[slideIndex1 - 1].className += " active";
}
var slideIndex2 = 1;
showSlides2(slideIndex2);
function plusSlides2(n) {
"use strict";
showSlides2(slideIndex2 += n);
}
function currentSlide2(n) {
"use strict";
showSlides2(slideIndex2 = n);
}
function showSlides2(n) {
"use strict";
var k;
var slides2 = document.getElementsByClassName("mini-box2");
var dots2 = document.getElementsByClassName("dot2");
if (n > slides2.length) {
slideIndex2 = 1;
}
if (n < 1) {
slideIndex2 = slides2.length;
}
for (k = 0; k < slides2.length; k++) {
slides2[k].style.display = "none";
}
for (k = 0; k < dots2.length; k++) {
dots2[k].className = dots2[k].className.replace(" active", "");
}
slides2[slideIndex2 - 1].style.display = "block";
slides2[slideIndex2 - 1].style.margin = "0 auto";
dots2[slideIndex2 - 1].className += " active";
}
var slideIndex3 = 1;
showSlides3(slideIndex3);
function plusSlides3(n) {
"use strict";
showSlides3(slideIndex3 += n);
}
function currentSlide3(n) {
"use strict";
showSlides3(slideIndex3 = n);
}
function showSlides3(n) {
"use strict";
var l;
var slides3 = document.getElementsByClassName("mini-box3");
var dots3 = document.getElementsByClassName("dot3");
if (n > slides3.length) {
slideIndex3 = 1;
}
if (n < 1) {
slideIndex3 = slides3.length;
}
for (l = 0; l < slides3.length; l++) {
slides3[l].style.display = "none";
}
for (l = 0; l < dots3.length; l++) {
dots3[l].className = dots3[l].className.replace(" active", "");
}
slides3[slideIndex3 - 1].style.display = "block";
slides3[slideIndex3 - 1].style.margin = "0 auto";
dots3[slideIndex3 - 1].className += " active";
}
Well, you could convert the first (dynamic block) into a JavaScript object/class structure.
Then you can create five or more instances of a Slideshow and each one will have its own index to keep track of the current slide. Each function will be a method on the object instead, so they should be independent of each other.
function Slideshow(options) {
this.slideIndex = 1;
this.slideClass = options.slideClass;
this.dotClass = options.dotClass;
}
Slideshow.prototype.plusSlides = function(n) {
this.showSlides(this.slideIndex += n);
}
Slideshow.prototype.currentSlide = function(n) {
this.showSlides(this.slideIndex = n);
}
Slideshow.prototype.showSlides = function(n) {
var i;
var slides = document.getElementsByClassName(this.slideClass);
var dots = document.getElementsByClassName(this.dotClass);
if (n > slides.length) {
this.slideIndex = 1;
}
if (n < 1) {
this.slideIndex = slides.length;
}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[this.slideIndex - 1].style.display = "block";
slides[this.slideIndex - 1].style.margin = "0 auto";
dots[this.slideIndex - 1].className += " active";
}
Usage
var mySlideshow = new Slideshow({
slideClass : "comment-box",
dotClass : "dot"
});
mySlideshow.showSlides(mySlideshow.slideIndex);
Demo
Here is a demo showing two slideshows running concurrently.
function Slideshow(options) {
this.slideIndex = 1;
this.selector = options.selector;
this.el = document.getElementById(options.id);
this.createListeners();
this.showSlides(this.slideIndex);
}
Slideshow.prototype.createListeners = function(n) {
var self = this;
var dots = self.el.getElementsByClassName('slideshow-dot');
for (var i = 0; i < dots.length; i++) {
self.createDotListener(dots[i], i + 1);
}
self.el.getElementsByClassName('slideshow-prev')[0]
.addEventListener('click', function() {
self.plusSlides(-1);
});
self.el.getElementsByClassName('slideshow-next')[0]
.addEventListener('click', function() {
self.plusSlides(1);
});
};
Slideshow.prototype.createDotListener = function(dot, index) {
var self = this;
dot.addEventListener('click', function() {
return self.currentSlide(index);
});
};
Slideshow.prototype.plusSlides = function(n) {
this.showSlides(this.slideIndex += n);
};
Slideshow.prototype.currentSlide = function(n) {
this.showSlides(this.slideIndex = n);
};
Slideshow.prototype.showSlides = function(n) {
var i;
var slides = this.el.getElementsByClassName('slideshow-slide');
var dots = this.el.getElementsByClassName('slideshow-dot');
if (n > slides.length) {
this.slideIndex = 1;
}
if (n < 1) {
this.slideIndex = slides.length;
}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = 'none';
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(' active', '');
}
slides[this.slideIndex - 1].style.display = 'block';
slides[this.slideIndex - 1].style.margin = '0 auto';
dots[this.slideIndex - 1].className += ' active';
};
var mySlideshow1 = new Slideshow({ id : 'my-slideshow-1' });
var mySlideshow2 = new Slideshow({ id : 'my-slideshow-2' });
* {box-sizing:border-box}
/* Slideshow container */
.slideshow-container {
max-width: 1000px;
position: relative;
margin: auto;
}
.slideshow-slide {
display: none;
}
/* Next & previous buttons */
.slideshow-prev, .slideshow-next {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
margin-top: -22px;
padding: 16px;
color: white;
font-weight: bold;
font-size: 18px;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
}
/* Position the "next button" to the right */
.slideshow-next {
right: 0;
border-radius: 3px 0 0 3px;
}
/* On hover, add a black background color with a little bit see-through */
.slideshow-prev:hover, .slideshow-next:hover {
background-color: rgba(0,0,0,0.8);
}
/* Caption text */
.text {
color: #f2f2f2;
font-size: 15px;
padding: 8px 12px;
position: absolute;
bottom: 8px;
width: 100%;
text-align: center;
}
/* Number text (1/3 etc) */
.numbertext {
color: #f2f2f2;
font-size: 12px;
padding: 8px 12px;
position: absolute;
top: 0;
}
/* The dots/bullets/indicators */
.slideshow-dot {
cursor:pointer;
height: 13px;
width: 13px;
margin: 0 2px;
background-color: #bbb;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
}
.active, .dot:hover {
background-color: #717171;
}
/* Fading animation */
.fade {
-webkit-animation-name: fade;
-webkit-animation-duration: 1.5s;
animation-name: fade;
animation-duration: 1.5s;
}
#-webkit-keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
#keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
<div id="my-slideshow-1">
<div class="slideshow-container">
<div class="slideshow-slide fade">
<div class="numbertext">1 / 3</div>
<img src="https://www.w3schools.com/howto/img_nature_wide.jpg" style="width:100%">
<div class="text">Caption Text</div>
</div>
<div class="slideshow-slide fade">
<div class="numbertext">2 / 3</div>
<img src="https://www.w3schools.com/howto/img_fjords_wide.jpg" style="width:100%">
<div class="text">Caption Two</div>
</div>
<div class="slideshow-slide fade">
<div class="numbertext">3 / 3</div>
<img src="https://www.w3schools.com/howto/img_lights_wide.jpg" style="width:100%">
<div class="text">Caption Three</div>
</div>
<a class="slideshow-prev">❮</a>
<a class="slideshow-next">❯</a>
</div>
<br>
<div style="text-align:center">
<span class="slideshow-dot"></span>
<span class="slideshow-dot"></span>
<span class="slideshow-dot"></span>
</div>
</div>
<div id="my-slideshow-2">
<div class="slideshow-container">
<div class="slideshow-slide fade">
<div class="numbertext">1 / 3</div>
<img src="https://www.w3schools.com/howto/img_nature_wide.jpg" style="width:100%">
<div class="text">Caption Text</div>
</div>
<div class="slideshow-slide fade">
<div class="numbertext">2 / 3</div>
<img src="https://www.w3schools.com/howto/img_fjords_wide.jpg" style="width:100%">
<div class="text">Caption Two</div>
</div>
<div class="slideshow-slide fade">
<div class="numbertext">3 / 3</div>
<img src="https://www.w3schools.com/howto/img_lights_wide.jpg" style="width:100%">
<div class="text">Caption Three</div>
</div>
<a class="slideshow-prev">❮</a>
<a class="slideshow-next">❯</a>
</div>
<br>
<div style="text-align:center">
<span class="slideshow-dot"></span>
<span class="slideshow-dot"></span>
<span class="slideshow-dot"></span>
</div>
</div>

Categories

Resources