旋转木马滑块图像不滑动

时间:2014-09-30 01:17:57

标签: javascript jquery css

我一直在研究我的carousal滑块我现在有图像显示。

但是不能让它滑动第一张图像应该淡出并重置幻灯片。

我有来自Google的当前Ajax / jquery。它应该能够滑动。不确定什么是错的。

直播示例: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;
}

1 个答案:

答案 0 :(得分:1)

我认为问题源于这行代码:

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

你实际上是在那里返回一个函数引用而不是一个对象,我认为正确的行应该是:

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

不过,我只是简单地使用jQuery.animate(),也许是一种完全不同的方法。检查更新后的代码,我也做了一些改进:

$(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);
});

我也改变了这个CSS规则:

.caoursel {
   position: relative;
}

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

现场演示:http://codepen.io/riwakawebsitedesigns/pen/CEgdm

我建议使用一些第三方旋转木马,例如Bootstrap的旋转木马:http://getbootstrap.com/javascript/#carousel