user3300065
user3300065

Reputation:

carousal slider images not sliding

I have been working on my carousal slider I have got images showing now.

But can not get it to slide the first image should fade and reset slide.

I have the current Ajax / jquery from Google. It should be able to slide. Not sure whats wrong.

Live Example: http://codepen.io/riwakawebsitedesigns/pen/CEgdm

<!DOCTYPE html>
<html>
<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1">
   <title></title>
   <link rel="stylesheet" href="css/responsive.css" media="screen" type="text/css" media="all" />
   <link rel="stylesheet" href="css/carousel.css" media="screen" type="text/css" media="all" />
   <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
   <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
   <script type="text/javascript">
    $(document).ready(function () {
            $(".carousel #1").show("fade", 500);
            $(".carousel #1").delay(5500).hide("slide", {direction:'left'}, 500);

            var sc = $(".carousel img").size();
            var count = 2;

            setInterval(function() {
                $(".carousel #"+count).show("slide",function(){direction: 'right', 500});
                $(".carousel #"+count).delay(5500).hide("slide", {direction:'left'}, 500);

                if (count == sc) {
                    count = 1;
                } else {
                    count = count + 1
                }
            }, 1700);
        })
    </script>
</head>
<body>

<div class="container">
    <div class="row">
        <div class="span12">
            <div class="carousel">
                <img id="1" class="active" src="images/image1.jpg" border="0" alt="" />
                <img id="2" src="images/image2.jpg" border="0" alt="" />
                <img id="3" src="images/image3.jpg" border="0" alt="" />
            </div>
        </div>
    </div>
</div><!-- . Container -->

</body>
</html>

Slider CSS

.carousel {
    width: 100%;
    height: 350px;
    overflow: hidden;
    background-image: url('../images/loading.gif');
    background-repeat: no-repeat;
    background-position: center;
}

.carousel img {
    width: 100%;
    height: 350px;
    display: none;
}

Upvotes: 0

Views: 134

Answers (1)

emerino
emerino

Reputation: 1120

I believe the problem is due to this line of code:

$(".carousel #"+count).show("slide",function(){direction: 'right', 500});

You are actually returning a function reference instead of an object there, I think the right line should be:

$(".carousel #"+count).show("slide",{direction: 'right'}, 500);

Still, I would simply use jQuery.animate() instead, and perhaps a whole different approach. Check the updated code, I've made some enhancements too:

$(document).ready(function() {
    var images = $(".carousel img");
    var timeout = 5000; // 5 seconds
    var max = images.length;

    var slide = function(index) {
        var img = $(images[index]);

        // first show the image      
        img.animate({left: "0"}, 500, function() {

            // specify timeout to change image
            setTimeout(function() {
                // hide current image
                img.animate({left: "100%"}, 500, function() {                  
                    // reset state to start over
                    img.css("left" , "-100%");
                });
                if (++index == max) {
                    index = 0; // start over
                }
                // recursive call
                slide(index);
            }, timeout);        
        });          
    };

    slide(0);
});

I changed this CSS rules too:

.caoursel {
   position: relative;
}

.carousel img {
   position: absolute;
   width: 100%;
   height: 350px;
   left: -100%;
}

Live demo: http://codepen.io/riwakawebsitedesigns/pen/CEgdm

I recommend using some third party carousel, like Bootstrap's carousel: http://getbootstrap.com/javascript/#carousel

Upvotes: 1

Related Questions