Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have been trying to make two sliders for my products screen one for the image and other for information.

I can get one working however after taking the w3schools code for making sliders and adjusting it for 2 it seems to either crash out or just plainly not work for one of the sliders.

What I have tried:

I have tried adapting the code by changing all variables that I thought could be a factor to something different i.e. one might have leftSlides the other rightSlides.

HTML CODE

HTML
<div class="left-product slider-container">
        <div class="leftSlides fade">
            <div class="one">1111111</div>
            <img class="slider-img" src="">
        </div>
        <div class="leftSlides fade">
            <div class="two">dddd</div>
            <img class="slider-img"src="">
        </div>
        <div class="leftSlides fade">
            <div class="three">ASDASD</div>
            <img class="slider-img" src="">
        </div>
    </div>

    <div class="center-product"></div>

    <div class="right-product slider-container">
        <div class="rightSlides fade">
            <div class="one">1111111</div>
            <img class="slider-img" src="">
        </div>
        <div class="rightSlides fade">
            <div class="two">dddd</div>
            <img class="slider-img"src="">
        </div>
        <div class="rightSlides fade">
            <div class="three">ASDASD</div>
            <img class="slider-img" src="">
        </div>
    </div>

    <div class="left-product toggle-slide">
        <span class="slide-left slide-one" onclick="currentSlide(1)"></span>
        <span class="slide-left slide-two" onclick="currentSlide(2)"></span>
        <span class="slide-left slide-three" onclick="currentSlide(3)"></span>
    </div>

    <div class="right-product toggle-slide">
        <span class="slide-right slide-one" onclick="currentSlide(4)"></span>
        <span class="slide-right slide-two" onclick="currentSlide(5)"></span>
        <span class="slide-right slide-three" onclick="currentSlide(6)"></span>
    </div>


JavaScript
JavaScript
var leftslideIndex = 1;
var rightslideIndex = 4;
leftSlides(leftslideIndex);
leftSlides(rightslideIndex);

function currentSlide(n) {
    leftSlides(leftslideIndex = n);
    rightSlides(rightslideIndex = n);
}

function leftSlides(n) {
    var i;
    var slides = document.getElementsByClassName("leftSlides");
    var dots = document.getElementsByClassName("slide-left");
    if (n > slides.length) {leftslideIndex = 1}
    if (n < 1) {leftslideIndex = 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[leftslideIndex-1].style.display = "block";
    dots[leftslideIndex-1].className += " active";
}

function rightSlides(n) {
    var j;
    var rightslides = document.getElementsByClassName("rightSlides");
    var rightdots = document.getElementsByClassName("slide-right");
    if (n > rightslides.length) {rightslideIndex = 1}
    if (n < 1) {rightslideIndex = rightslides.length}
    for (j = 0; j < rightslides.length; j++) {
        rightslides[j].style.display = "none";
    }
    for (j = 0; j < rightdots.length; j++) {
        rightdots[j].className = rightdots[j].className.replace(" active", "");
    }
    rightslides[rightslideIndex-1].style.display = "block";
    rightdots[rightslideIndex-1].className += " active";
}


Any help on this would be greatly appreciated.

Thanks for looking,
Ben :)
Posted
Comments
Richard Deeming 12-Mar-20 7:42am    
leftSlides(leftslideIndex);
leftSlides(rightslideIndex);

Shouldn't that second call be to rightSlides?
tzusun67 12-Mar-20 19:18pm    
Hi,

Thanks I never noticed that. Guess I must have been looking too hard into it haha.
It is duplicating the slides though now. Any idea why this is.

Basically what is happening now is both slides are being controlled by the slide-left buttons not left and right.

Any idea why this is as the numbers are completely different?
Richard Deeming 13-Mar-20 7:02am    
Both sets of buttons are calling currentSlide, which is calling both leftSlides and rightSlides.

You need to create separate functions to control the left and right slides, and call the appropriate function from the buttons.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900