Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Trying make images slideshow with Javascript

I was trying to make slideshow with Javascript by use fade in and fade out the first image then trying to use loop. But it doesn't work. Can anyone show me the simplest way to make a slideshow? Thanks

<!DOCTYPE HTML>

<link rel="stylesheet" type="text/css" href="css/style.css"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>slider</title>


<div class="wrapper">
    <div class="image">

        <img id="1" src="images/designer_1.jpeg">
        <img id="2" src="images/designer_2.jpeg">
        <img id="3" src="images/designer_3.jpg">
    </div>

        <a class="float_left" href="#">Previous</a>
        <a class="float_right" href="#">Next</a>
</div>

<script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="script.js" type="text/javascript"></script>    
var sildeInt = 1;
var counter=0;

function startSile(){
    $("img#" + sildeInt).fadeIn(300);
    $("img").fadeOut(300);
    sildeInt +=1;   
}

while(counter<3){
    startSile();
    counter+=1;
}

2 Answers

I've come up with an actual image slider that you can use - jQuery is the only dependency. This code will dynamically handle any number of images you want in the carousel. Please let me know if you have any questions or if this helps at all.

The code that is commented out is for automatically sliding.

scripts.js
$(function() {
    /****************************************
            Variables
    ****************************************/

    var $carousel = $(".carousel"); 

    /*
        Adding or removing images is as simple as updating
        this list.
    */
    var images = [
        "img/mustang.jpg",
        "img/hangar.jpg",
        "img/tron.jpg"
    ];



    /****************************************
            Event Handling
    ****************************************/

    $("button").click(function() {
        slide();
    });



    /****************************************
            Functions
    ****************************************/   

    function appendImages() {
        var $images = $(".carousel img");
        $images.remove();
        for (var i = 0; i < images.length; ++i) {
            var $image = $("<img>");
            $image.attr("src", images[i]);
            $image.css({
                "max-width": "100%",
                "position": "absolute"
            });
            if (i > 0) {
                $image.css("left", "100%");
            }
            $carousel.append($image);
        }
    }

    function complete() {
        var img = images.shift();
        images.push(img);
        appendImages();
        //setTimeout(slide, 2000);      
    }

    function slide() {
        var $images = $(".carousel img");
        $images.eq(0).animate({
            "left": "-100%"
        }, 1000);

        $images.eq(1).animate({
            "left": "0%"
        }, 1000, complete)
    }



    /****************************************
            Document On-load Actions
    ****************************************/

    appendImages(); 
    //setTimeout(slide, 2000);
});
main.css
body {
    position: relative;
    overflow-x: hidden;
}

.carousel {
    width: 800px;
    height: 600px;
    margin: 0 auto;
    position: relative;
    overflow-x: hidden;
}

button {
    margin: 0 auto;
    margin-top: 50px;
    padding: 20px 40px;
    position: absolute;
    left: 50%;
    transform: translate(-50%, 0);  
}
index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="main.css">
</head>
<body>
    <div class="carousel"></div>
    <button type="button">Slide</button>

    <script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
    <script src="scripts.js"></script>
</body>
</html>

Hi An,

This may not be the simplest image slider, but you're welcome to look at the code for my JavaScript carousel.

Hope this helps,

Cheers