Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10

Thread: Slideshow

  1. #1
    New to the CF scene
    Join Date
    May 2016
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Slideshow

    Hi I am trying to make a slideshow with 6 images in a row that slide to the next image every 2 seconds but I can't figure out how. I am only able to it with 1 image. Here is the code I am using atm:
    Code:
    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, 2000);
    }
    Thanks for reading and I hope someone can help me
    Last edited by vinyl-junkie; 05-03-2016 at 10:11 PM. Reason: added code tags

  2. #2
    Regular Coder
    Join Date
    Feb 2016
    Posts
    128
    Thanks
    0
    Thanked 32 Times in 32 Posts
    Your code works perfectly for me. Did you place the script below the image to slide? If not the elements are not yet ready.

  3. #3
    New to the CF scene
    Join Date
    May 2016
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Yeah I know the code is working fine for a 1 image slide show. But i am trying to make it 6 width so that 6 images slide to the next image. Thanks for the fast respond tho

  4. #4
    Regular Coder
    Join Date
    Feb 2016
    Posts
    128
    Thanks
    0
    Thanked 32 Times in 32 Posts
    Six images side by side? And each sliding at the same time?

  5. #5
    New to the CF scene
    Join Date
    May 2016
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    ^^yeah

  6. #6
    Regular Coder
    Join Date
    Feb 2016
    Posts
    128
    Thanks
    0
    Thanked 32 Times in 32 Posts
    Try this:
    Code:
            <span class="slideContainer">
                <img class="mySlides" src="bilder/1.jpg">
                <img class="mySlides" src="bilder/2.jpg">
                <img class="mySlides" src="bilder/1.jpg">
                <img class="mySlides" src="bilder/2.jpg">
            </span>
            <span class="slideContainer">
                <img class="mySlides" src="bilder/1.jpg">
                <img class="mySlides" src="bilder/2.jpg">
                <img class="mySlides" src="bilder/1.jpg">
                <img class="mySlides" src="bilder/2.jpg">
            </span>
            <script>
                var myIndex = 1;
                carousel();
    
                function carousel() {
                    var containers = document.getElementsByClassName("slideContainer");
                    for (var i = 0; i < containers.length; i++ ) {
                        var slides = containers[i].getElementsByClassName("mySlides");
                        for (var j = 0; j < slides.length; j++) {
                            slides[j].style.display = "none";
                        }
                        slides[myIndex - 1].style.display = "block";
                    }
                    myIndex++;
                    if (myIndex > slides[0].length) {
                        myIndex = 1
                    }
                    setTimeout(carousel, 2000);
                }
            </script>

  7. Users who have thanked Sempervivum for this post:

    ali123 (05-03-2016)

  8. #7
    New to the CF scene
    Join Date
    May 2016
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you very much man that worked good

  9. #8
    New to the CF scene
    Join Date
    May 2016
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Got 1 little problem now the show stops when it got a full round is there a way to keep it going?

  10. #9
    Regular Coder
    Join Date
    Feb 2016
    Posts
    128
    Thanks
    0
    Thanked 32 Times in 32 Posts
    Sorry, there is an error:
    Code:
                function carousel() {
                    var containers = document.getElementsByClassName("slideContainer");
                    for (var i = 0; i < containers.length; i++ ) {
                        var slides = containers[i].getElementsByClassName("mySlides");
                        for (var j = 0; j < slides.length; j++) {
                            slides[j].style.display = "none";
                        }
                        slides[myIndex - 1].style.display = "block";
                    }
                    myIndex++;
                    if (myIndex > slides[0].length) { // error here
                        myIndex = 1
                    }
                    setTimeout(carousel, 2000);
                }
    This is correct:
    Code:
    if (myIndex > slides.length)
    Note, that there is a limitation in this script: All slideshows must have the same number of images.

  11. #10
    New to the CF scene
    Join Date
    May 2016
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi thanks you are the best . Luckely that is exactly what I want so thats no problem for me.


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •