如何在两个或更多幻灯片之间切换?

时间:2013-02-22 09:56:50

标签: jquery html css slideshow carousel

更确切地说,想要制作一个更多幻灯片改变的旋转木马。我按类分组了几个图像(一组3个带有类project1的图像,一个组带有另外3个带有类project2的图像等)。在我的旋转木马中,一组进行自动幻灯片放映,但是当我点击下一个/后退按钮时,我希望在同一个容器中更改显示的组。 作为一个例子,我有一组狮子图像一个接一个地出现,当我点击下一个,我希望他们改变与猫的一组照片。只是一个粗略的描述,以便更好地理解它。

我的Jquery现在:

function slideswitch() {
    var $active = $("#project img.project1.active");
    $active.hide();
    if ( $active.length == 0 ) $active = $('#project IMG.project1:last');

    var $next =  $active.next(":has(.project1)").length ? $active.next()
        : $('#project IMG.project1:first');

    $active.addClass('last-active').show();

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
};
$(document).ready(function() {
    setInterval(slideswitch, 2000 );
});

我还有一个小提琴here

1 个答案:

答案 0 :(得分:1)

我创造了一个在两个节目之间切换的小提琴。 CSS没有改变,但是对HTML进行了更改,我添加了新的JS函数并优化了您的slidewitch函数,因为它没有正确查找幻灯片

HTML更改(添加onclick事件)

<div id="back" onclick="changeShow('back');">B</div><div id="next" onclick="changeShow('next');">N</div>

JS

// current project and total amount of projects we have
var project = 1, projects = 1;

function changeShow(direction)
{
    // change project based on direction
    if (direction == 'back') {
        // check if previous project exists, otherwise use last as we would've cycled
        project = (project - 1 > 0) ? (project - 1) : projects;
    } else {
        // check we aren't exceeding the number of projects we have, otherwise loop
        project = (project + 1 <= projects) ? (project + 1) : 1;
    }

    // remove any active images from the old project
    $('#project img').removeClass('active last-active');

    // force slide change
    slideShow();
}

function getProjects()
{
    // find the largest project assuming they will be sequential - project1, project2, projectX..
    $('img[class^="project"]').each(function(){
        var current = parseInt($(this).attr('class').replace('project', ''), 10);
        if (current > projects) {
            // update projects count
            projects = current;
        }
    });
}


function slideShow()
{    
    var $active = $('.project' + project + '.active');
    if ($active.length == 0) $active = $('.project' + project + ':last');
    var $next = $active.next('.project' + project).length ? $active.next() : $('.project' + project + ':first');
    $active.addClass('last-active').show();
    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

$(document).ready(function()
{
    // find out how many projects we have
    getProjects();
    // start slide show
    setInterval(slideShow, 2000 );
});

小提琴:http://jsfiddle.net/sjdaws/4QcYE/