我正在尝试使滑块自动播放。经过数小时的尝试,我无法理解。每次自动播放图像时,您都必须单击“下一步”按钮以使其跟随自动播放。我对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;
}

最佳答案

我更改了一些内容,虽然不是我编写的完美代码,但可以完成工作。

从您的代码和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)
})()

关于javascript - 有人可以帮我自动播放滑块吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20623702/

10-09 22:14