0
Hello. I’m new here but I thought it would be the ideal place to solve my problem. Come on.
I’m beginner and I’m developing a page. On this page, I created a slideshow to present the steps of a particular project. My problem is that when I do another slideshow, I end up repeating the same code and script. And I think it ends up generating a lot of code. Is there any way I could create multiple Slideshows using the same script? I’ll upload the code:
$(document).ready(function(){
$('.top').click(function(){
$('ul').toggleClass('active');
})
})
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("mySlides");
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";
}
var slideIndex2 = 1;
showSlides2(slideIndex2);
function plusSlides2(n) {
showSlides2(slideIndex2 += n);
}
function currentSlide2(n) {
showSlides2(slideIndex2 = n);
}
function showSlides2(n) {
var i;
var slides = document.getElementsByClassName("mySlides2");
var dots = document.getElementsByClassName("dot2");
if (n > slides.length) {slideIndex2 = 1}
if (n < 1) {slideIndex2 = 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(" active2", "");
}
slides[slideIndex2-1].style.display = "block";
dots[slideIndex2-1].className += " active2";
}
.mySlides {
display: none;
}
.slideshow-container {
position: relative;
margin: auto;
width: 100%;
height: 550px;
background: #e6ffe6;
}
.mySlides img {
max-width: 100%;
position: absolute;
top: 50%;
left: 50%;
margin-right: -50%;
transform: translate(-50%, -50%)
}
.prev, .next {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
padding: 16px;
margin-top: -24px;
color: white;
font-weight: bold;
font-size: 18px;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
user-select: none;
}
.prev{
left: 0;
background: #009900;
}
.next {
right: 0;
border-radius: 3px 0 0 3px;
background: #009900;
}
.prev:hover, .next:hover {
background-color: #009900;
}
.dot {
cursor: pointer;
height: 15px;
width: 15px;
margin: 0 2px;
background-color: #33cc33;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
}
.active, .dot:hover {
background-color: #009900;
}
.mySlides2 {
display: none;
}
.slideshow-container2 {
position: relative;
margin: auto;
width: 100%;
height: 550px;
background: #e6ffe6;
}
.mySlides2 img {
max-width: 100%;
position: absolute;
top: 50%;
left: 50%;
margin-right: -50%;
transform: translate(-50%, -50%)
}
.prev2, .next2 {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
padding: 16px;
margin-top: -24px;
color: white;
font-weight: bold;
font-size: 18px;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
user-select: none;
}
.prev2{
left: 0;
background: #009900;
}
.next2 {
right: 0;
border-radius: 3px 0 0 3px;
background: #009900;
}
.prev2:hover, .next2:hover {
background-color: #009900;
}
.dot2 {
cursor: pointer;
height: 15px;
width: 15px;
margin: 0 2px;
background-color: #33cc33;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
}
.active2, .dot2:hover {
background-color: #009900;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/style_cobertura.css">
</head>
<body>
<div class="row">
</div>
<div class="mid-column col-7">
<div class="slideshow-container">
<div class="mySlides fade">
<img src="image/cobertura-verde/001.jpg">
</div>
<div class="mySlides fade">
<img src="image/cobertura-verde/002.jpg">
</div>
<div class="mySlides fade">
<img src="image/cobertura-verde/003.jpg">
</div>
<div class="mySlides fade">
<img src="image/cobertura-verde/004.jpg">
</div>
<div class="mySlides fade">
<img src="image/cobertura-verde/005.jpg">
</div>
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
<br>
<div>
<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(4)"></span>
<span class="dot" onclick="currentSlide(5)"></span>
</div>
<div class="slideshow-container2">
<div class="mySlides2 fade">
<img src="image/cobertura-verde/fase1/020.jpg">
</div>
<div class="mySlides2 fade">
<img src="image/cobertura-verde/fase1/021.jpg">
</div>
<div class="mySlides2 fade">
<img src="image/cobertura-verde/fase1/022.jpg">
</div>
<div class="mySlides2 fade">
<img src="image/cobertura-verde/fase1/023.jpg">
</div>
<div class="mySlides2 fade">
<img src="image/cobertura-verde/fase1/024.jpg">
</div>
<small class="small"> Fotos do dia 16/03/2010 </small>
<a class="prev2" onclick="plusSlides2(-1)">❮</a>
<a class="next2" onclick="plusSlides2(1)">❯</a>
</div>
<br>
<div>
<span class="dot2" onclick="currentSlide2(1)"></span>
<span class="dot2" onclick="currentSlide2(2)"></span>
<span class="dot2" onclick="currentSlide2(3)"></span>
<span class="dot2" onclick="currentSlide2(4)"></span>
<span class="dot2" onclick="currentSlide2(5)"></span>
</div>
create a javascript event for each slide, in case you should create the event with different classes.
– Eduardo
Fabiano, thanks for the reply. The event in which you say would be in Script ne?
– Leonam Santtana
Add 2 in variables and classes or HTML ID. .
– Carvalho Carvalho