有人可以帮我自动播放我的滑块吗?

时间:2013-12-17 00:26:45

标签: javascript jquery css

我正在尝试让我的滑块自动播放。经过几个小时的尝试,我无法得到它。每次图像自动播放时,您必须单击下一个按钮才能跟随自动播放。我对jQuery不太熟悉。

我的Javascript:

sliderInt = 1;
sliderNext = 2;

$(document).ready(function() {

    $(".slide > img#1").fadeIn(300);
    startSlider();
});

function startSlider(){

    count = $(".slide > img").size();

    loop = setInterval(function() {

        if (sliderNext > count) {
            sliderNext = 1;
            sliderInt = 1;
        }

        $(".slide > img").fadeOut(300);
        $(".slide > img#" + sliderNext).fadeIn(300);

        sliderInt = sliderNext;
        sliderNext = sliderNext + 1;

    }, 3000)

}

我的CSS:

.slide
{
top: 0;
opacity: 0;
width: 745px;
height: 420px;
display: block;
position: absolute;

transform: scale(0);

transition: all .7s 
}

.slide img 
{
width: 100%;
height: 100%;
border-radius: 6px;
border: 1px solid #95ca1a;
}

.nav label
{
width: 200px;
height: 100%;
display: none;
position: absolute;

opacity: 0;
z-index: 9;
cursor: pointer;

transition: opacity .2s;

color: #FFF;
font-size: 156pt;
text-align: center;
line-height: 380px;
font-family: "Varela Round", sans-serif;
background-color: rgba(255, 255, 255, .3);
text-shadow: 0px 0px 15px rgb(119, 119, 119);
}

.slide:hover + .nav label 
{
opacity: 0.5; 
}

.nav label:hover 
{
 opacity: 1; 
}

.nav .next {
 right: 0;
}

input:checked + .slide-container  .slide 
{
opacity: 1;

transform: scale(1);

transition: opacity 1s ease-in-out;
}

input:checked + .slide-container .nav label 
{
display: block;
border-radius: 6px; 
}

1 个答案:

答案 0 :(得分:0)

我改变了一些东西,不是我编写的完美代码,但它完成了工作。

从您的代码和CSS中,我只能假设您正在尝试build a slideshow like this

  • 我重命名了如何创建元素ID,如下所示:<img id="slide_1" ...

  • 我从css样式中删除了不透明度

  • jQuery代码如下所示:

    (function (){
        var count = $(".slide > img").length;
        var current = 1;
        var sliderNext = 2;
    
        $("img[id^='slide_']").fadeOut(0);
        $("#slide_" + current).fadeIn(300);
        var loop = setInterval(function() {
            $("#slide_" + current).fadeOut(300);
            $("#slide_" + sliderNext).fadeIn(300);
            (sliderNext >= count) ? sliderNext = 1 : sliderNext++;
            (current >= count) ? current = 1 : current++;
        }, 3000)
    })()