Reputation: 3165
More exact is want to make a carousel that changes betwen more slideshows. I grouped by a class several images (a group of 3 images with class project1, a group with another 3 images with class project2 etc). In my carousel one group make an automatic slideshow but when i click next/back button i want that in the same container to change the group that shows. As an example i have a group with lion images that go one after another and when i click next i want them to change in a group of photos with cats for example. Is just a rough description to understand it better.
My Jquery for now:
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 );
});
I also have a fiddle that is here.
Upvotes: 1
Views: 154
Reputation: 3536
I have created a fiddle that switches between 2 shows. The CSS is unchanged, but there is a change to the HTML and I added new JS functions and optimised your slideswitch function as it wasn't correctly finding slides
HTML changes (add onclick events)
<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 );
});
Fiddle: http://jsfiddle.net/sjdaws/4QcYE/
Upvotes: 1