How to make a javascript slider that slides automatically - javascript

I'm trying to create a slider for my html page,
I found this slider in css/javascript, everything is working properly, but how can I make the slide sliding automatically too instead of sliding only when the user press the arrows ?
var slideIndex = 1;
showDivs(slideIndex);
function plusDivs(n) {
showDivs(slideIndex += n);
}
function currentDiv(n) {
showDivs(slideIndex = n);
}
function showDivs(n) {
var i;
var x = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("demo");
if (n > x.length) {
slideIndex = 1
}
if (n < 1) {
slideIndex = x.length
}
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" w3-white", "");
}
x[slideIndex - 1].style.display = "block";
dots[slideIndex - 1].className += " w3-white";
setTimeout(2000); // Change image every 2 seconds
}
<!DOCTYPE html>
<html>
<title>W3.CSS</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://www.w3schools.com/lib/w3.css">
<style>
.mySlides {
display: none
}
.w3-left,
.w3-right,
.w3-badge {
cursor: pointer
}
.w3-badge {
height: 13px;
width: 13px;
padding: 0
}
</style>
<body>
<div class="w3-container">
<h2>Slideshow Indicators</h2>
<p>An example of using buttons to indicate how many slides there are in the slideshow, and which slide the user is currently viewing.</p>
</div>
<div class="w3-content w3-display-container" style="max-width:800px">
<img class="mySlides" src="img_nature_wide.jpg" style="width:100%">
<img class="mySlides" src="img_fjords_wide.jpg" style="width:100%">
<img class="mySlides" src="img_mountains_wide.jpg" style="width:100%">
<div class="w3-center w3-section w3-large w3-text-white w3-display-bottomleft" style="width:100%">
<div class="w3-left w3-padding-left w3-hover-text-khaki" onclick="plusDivs(-1)">❮</div>
<div class="w3-right w3-padding-right w3-hover-text-khaki" onclick="plusDivs(1)">❯</div>
<span class="w3-badge demo w3-border w3-transparent w3-hover-white" onclick="currentDiv(1)"></span>
<span class="w3-badge demo w3-border w3-transparent w3-hover-white" onclick="currentDiv(2)"></span>
<span class="w3-badge demo w3-border w3-transparent w3-hover-white" onclick="currentDiv(3)"></span>
</div>
</div>
<script>
</script>
</body>
</html>

Hi here i just implemented the logic to slide the images frequently..
try this
<!DOCTYPE html>
<html>
<title>W3.CSS</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://www.w3schools.com/lib/w3.css">
<style>
.mySlides {display:none}
.w3-left, .w3-right, .w3-badge {cursor:pointer}
.w3-badge {height:13px;width:13px;padding:0}
</style>
<body>
<div class="w3-container">
<h2>Slideshow Indicators</h2>
<p>An example of using buttons to indicate how many slides there are in the slideshow, and which slide the user is currently viewing.</p>
</div>
<div class="w3-content w3-display-container" style="max-width:800px">
<img class="mySlides" alt="sample test1" src="mfdg2.jpg" style="width:100%">
<img class="mySlides" alt="sample test2" src="img2.jpg" style="width:100%">
<img class="mySlides" alt="sample test3" src="img1.jpg" style="width:100%">
</div>
<script>
showDivs(1);
function plusDivs(n) {
n=n+1;
showDivs(n);
}
function showDivs(n) {
var i;
var x = document.getElementsByClassName("mySlides");
if (n > x.length) {n = 1}
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
x[n-1].style.display = "block";
setTimeout(function() { //calls click event after a certain time
plusDivs(n);
}, 2000);
}
</script>
</body>
</html>
Its working for me..

In your case setInterval() should be global.
setInterval(function(){
slideIndex = slideIndex +1;
showDivs(slideIndex)
}, 2000);
https://jsfiddle.net/g9a2ez4s/

use setInterval
setInterval(function(){
document.getElementsByClassName("w3-left").click();
}, 800);

Related

Running 2 slideshows in the same page

I want to run 3 slideshows on one page, I tried copying the code but it wasn't successful. I'm using a W3 slideshow. Also, I want to make the Next & previous buttons like in this: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_slideshow Please help me
Here is the code:
var slideIndex = 1;
showDivs(slideIndex);
function plusDivs(n) {
showDivs(slideIndex += n);
}
function showDivs(n) {
var i;
var x = document.getElementsByClassName("mySlides");
if (n > x.length) {slideIndex = 1}
if (n < 1) {slideIndex = x.length}
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
x[slideIndex-1].style.display = "block";
}
.mySlides {display:none;}
<html>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<body>
<div class="w3-content w3-display-container">
<img class="mySlides" src="https://www.w3schools.com/w3css/img_snowtops.jpg" style="width:100%">
<img class="mySlides" src="https://www.w3schools.com/w3css/img_lights.jpg" style="width:100%">
<img class="mySlides" src="https://www.w3schools.com/w3css/img_mountains.jpg" style="width:100%">
<button class="w3-button w3-black w3-display-left" onclick="plusDivs(-1)">❮</button>
<button class="w3-button w3-black w3-display-right" onclick="plusDivs(1)">❯</button>
</div>
</body>
</html>

animations doesn't work when I add bootstrap4 cdn

<div class="mySlides fade">
<img src="https://media.giphy.com/media/9JpsWBPgRtBDOYE6QB/source.gif" style="width:100%; height: 100%; border-radius: 0; ">
</div>
<div class="mySlides fade">
<img src="https://media2.giphy.com/media/fRGq1cnopavJ7mKDYn/giphy.webp" style="width:100%; height: 100%; border-radius: 0; ">
</div>
<div class="mySlides fade">
<img src="https://media1.giphy.com/media/lPRrCc9lQsOPa2qZiw/giphy.webp" style="width:100%; height: 100%; border-radius: 0; ">
</div>
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(+1)">❯</a>
</div>
<div style="text-align:center">
<span class="dot" onclick="currentSlide(1)"></span>
<span class="dot" onclick="currentSlide(2)"></span>
<span class="dot" onclick="currentSlide(3)"></span>
</div>
<script>
var slideIndex = 1;
var timer = null;
showSlides(slideIndex);
function plusSlides(n) {
clearTimeout(timer);
showSlides(slideIndex += n);
}
function currentSlide(n) {
clearTimeout(timer);
showSlides(slideIndex = n);
}
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");
if (n==undefined){n = ++slideIndex}
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";
timer = setTimeout(showSlides, 2500);
}
</script>
This is my slider image with button and auto slide. I got this code from w3schools and edited.Some of the codes are not good but the main problem is when I add Bootstrap4 CDN it is not working and some of the divs(class "container") are getting smaller. How can I solve this.
Overwrite this bootstrap CSS
.fade:not(.show) { opacity: 1; }
Full Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
<style>
.fade:not(.show){
opacity: 1;
}
</style>
</head>
<body>
<div class="container">
<div class="mySlides fade">
<img src="https://media.giphy.com/media/9JpsWBPgRtBDOYE6QB/source.gif" style="width:100%; height: 100%; border-radius: 0 ">
</div>
<div class="mySlides fade">
<img src="https://media2.giphy.com/media/fRGq1cnopavJ7mKDYn/giphy.webp" style="width:100%; height: 100%; border-radius: 0 ">
</div>
<div class="mySlides fade">
<img src="https://media1.giphy.com/media/lPRrCc9lQsOPa2qZiw/giphy.webp" style="width:100%; height: 100%; border-radius: 0 ">
</div>
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(+1)">❯</a>
</div>
<div style="text-align:center">
<span class="dot" onclick="currentSlide(1)"></span>
<span class="dot" onclick="currentSlide(2)"></span>
<span class="dot" onclick="currentSlide(3)"></span>
</div>
<script>
var slideIndex = 1;
var timer = null;
showSlides(slideIndex);
function plusSlides(n) {
clearTimeout(timer);
showSlides(slideIndex += n);
}
function currentSlide(n) {
clearTimeout(timer);
showSlides(slideIndex = n);
}
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");
if (n==undefined){n = ++slideIndex}
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";
timer = setTimeout(showSlides, 2500);
}
</script>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#4.6.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-Piv4xVNRyMGpqkS2by6br4gNJ7DXjqk09RmUpJ8jgGtD7zP9yug3goQfGII0yAns" crossorigin="anonymous"></script>
</body>
</html>

W3 automatic slideshow 2 on same page

I need 2 automatic slideshow on one page, but there is a problem.
I use W3 slideshow, but if i make automatic they just after one rotation or just stop show up.
Here is code.
I try to change, but i not sure what i do wrong. If you can help me.
var myIndex = 0;
carousel();
var slideId = ["mySlides1", "mySlides2"]
function carousel() {
var i;
var x = document.getElementsByClassName("mySlides", "mySlides2");
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
myIndex++;
if (myIndex > x.length) {myIndex = 1}
x[myIndex-1].style.display = "block";
setTimeout(carousel, 2000); // Change image every 2 seconds
}
.mySlides {display:none;}
.mySlides2 {display:none;}
<!DOCTYPE html>
<html>
<head>
<title>W3.CSS</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta content="text/html; charset=iso-8859-2" http-equiv="Content-Type">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
</head>
<body>
<h2 class="w3-center">Automatic Slideshow</h2>
<div class="w3-content w3-section" style="max-width:500px">
<img class="mySlides" src="img_la.jpg" style="width:100%">
<img class="mySlides" src="img_ny.jpg" style="width:100%">
<img class="mySlides" src="img_chicago.jpg" style="width:100%">
</div>
<h2 class="w3-center">Automatic Slideshow</h2>
<div class="w3-content w3-section" style="max-width:500px">
<img class="mySlides2" src="img_la.jpg" style="width:100%">
<img class="mySlides2" src="img_ny.jpg" style="width:100%">
<img class="mySlides2" src="img_chicago.jpg" style="width:100%">
</div>
</body>
</html>
I made some edits and now your code is working !
You don't want to use only one index, you want to use one index for each slideshow.
For each call to carousel() you will need to stop displaying the current img (n°[x|y]Index), and display the next img (n°[x|y]Index).
With this function, you can add different numbers of images in each slideshow (in my example, mySlides displays 3 images and mySlides2 has 4).
let xIndex = 0;
let yIndex = 0;
carousel();
function carousel() {
let x = document.getElementsByClassName("mySlides");
let y = document.getElementsByClassName("mySlides2");
x[xIndex].style.display = "none";
y[yIndex].style.display = "none";
xIndex = (xIndex+1)%x.length;
yIndex = (yIndex+1)%y.length;
if (xIndex > x.length)
xIndex = 1;
if (yIndex > y.length)
yIndex = 1;
x[xIndex].style.display = "block";
y[yIndex].style.display = "block";
setTimeout(carousel, 2000); // Change image every 2 seconds
}
<h2>Automatic Slideshow</h2>
<div style="width:150px; height:150px; overflow: hidden;">
<img class="mySlides" src="http://lorempixel.com/400/400/" style="width:100%; display: none;">
<img class="mySlides" src="http://lorempixel.com/400/200/" style="width:100%; display: none;">
<img class="mySlides" src="http://lorempixel.com/200/400/" style="width:100%; display: none;">
</div>
<h2>Automatic Slideshow</h2>
<div style="width:150px; height:150px; overflow: hidden;">
<img class="mySlides2" src="http://lorempixel.com/400/300/" style="width:100%; display: none;">
<img class="mySlides2" src="http://lorempixel.com/300/400/" style="width:100%; display: none;">
<img class="mySlides2" src="http://lorempixel.com/300/300/" style="width:100%; display: none;">
<img class="mySlides2" src="http://lorempixel.com/800/300/" style="width:100%; display: none;">
</div>

W3 slideshow implementation doesn't work correctly

https://www.w3schools.com/w3css/tryit.asp?filename=tryw3css_slideshow_fading
Using the link above, you can see an example of an animated fading slideshow that I am trying to implement into my website. However, when I copy the exact code and use it for me, it displays the images stacked on top of one another as one image, and just fades it in and out to the same image (made up of all of the images).
Any idea why this is happening or another source where I can get an animated slideshow from? The only other thing to take into account is that in the styles file, everything referring to 'webkit' is underlined in red. I am running in chrome though so I don't think that is the issue.
Here is the code:
<!DOCTYPE html>
<html>
<title>W3.CSS</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<body>
<div class="w3-content w3-section" style="max-width:500px">
<p>The w3-animate-fading class animates an element in and out (takes about 10 seconds).</p>
<img class="mySlides w3-animate-fading" src="../../assets/img/img_rr_01.jpg" style="width:100%" alt="image1">
<img class="mySlides w3-animate-fading" src="../../assets/img/img_rr_02.jpg" style="width:100%" alt="image2">
<img class="mySlides w3-animate-fading" src="../../assets/img/img_rr_03.jpg" style="width:100%" alt="image3">
<img class="mySlides w3-animate-fading" src="../../assets/img/img_rr_04.jpg" style="width:100%" alt="image4">
</div>
<script>
var myIndex = 0;
carousel();
function carousel() {
var i;
var x = document.getElementsByClassName("mySlides");
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
myIndex++;
if (myIndex > x.length) {myIndex = 1}
x[myIndex-1].style.display = "block";
setTimeout(carousel, 9000);
}
</script>
</body>
</html>
This is exactly the code on the website, and I downloaded the images they use, so why is it that I can't get it to work?
<!DOCTYPE html>
<html>
<title>W3.CSS</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<body>
<div class="w3-content w3-section" style="max-width:500px">
<p>The w3-animate-fading class animates an element in and out (takes about 10 seconds).</p>
<img class="mySlides w3-animate-fading" src="img_rr_01.jpg" style="width:100%" alt="image1">
<img class="mySlides w3-animate-fading" src="img_rr_02.jpg" style="width:100%" alt="image2">
<img class="mySlides w3-animate-fading" src="img_rr_03.jpg" style="width:100%" alt="image3">
<img class="mySlides w3-animate-fading" src="img_rr_04.jpg" style="width:100%" alt="image4">
</div>
<script>
var myIndex = 0;
carousel();
function carousel() {
var i;
var x = document.getElementsByClassName("mySlides");
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
myIndex++;
if (myIndex > x.length) {myIndex = 1}
x[myIndex-1].style.display = "block";
setTimeout(carousel, 9000);
}
</script>
</body>
</html>
Here its working correctly. It may me something else that would be causing some problem.

Manual and Automatic Slideshow

I have to make a slideshow that has to work both automatic and manual.
For the manual part I have two buttons: next and previous that allows me to see the photos without having to wait a certain period of time between images.
When I don't click on the buttons, the slideshow goes automatic and the images change after ten seconds (for example).
My problem is that, after I click on the previous/ next button, the images start to appear faster or they appear more than one on the screen.
Here is the code I am working with:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>xPeke</title>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="icon" href="images/favicon-16x16.png" sizes="16x16" type="image/png">
</head>
<body>
<!-- PROGRAM BAR -->
<div id="program" class="topbar">
<b style="color:black;">Program:</b> Luni-Vineri: 9:00-22:00 Sambata: 9:00–20:00 Duminica: Inchis
<div class="topbar2">
Login <span> / </span> Register
</div>
</div>
<br>
<div class="slideshow-container">
<div class="wall">
<img class="mySlides" src="images/logo1.png" alt="Logo1" width="1500" height="300">
<img class="mySlides" src="images/logo2.png" alt="Logo2" width="1500" height="300">
<img class="mySlides" src="images/logo3.png" alt="Logo3" width="1500" height="300">
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a><br>
</div></div>
<form>
<img class="generalone" src="images/logo.png" alt="Logo" width="180" height="45">
<input class="search" type="text" name="search" placeholder="Search..">
</form>
<div class="generaltwo">
<img class="socialmedia" src="images/facebook.png" alt="Facebook">
<img class="socialmedia" src="images/youtube.png" alt="Youtube">
<img class="socialmedia" src="images/reddit.png" alt="Reddit">
</div>
<script>
var slideIndex = 1;
var timer = null;
showSlides(slideIndex);
function plusSlides(n) {
showSlides(slideIndex += n);
}
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides");
if (n==undefined){n = ++slideIndex}
if (n > slides.length) {slideIndex = 1}
if (n < 1) {slideIndex = slides.length}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
slides[slideIndex-1].style.display = "block";
timer = setTimeout(showSlides, 10000);
}
// clear Timeout(timer);
</script>
</body>
</html>

Categories

Resources