views:

363

answers:

2

I have a slide show built with jQuery that pauses on hover. It has a group of thumbnails sitting on top of the image that advances the image when clicked, otherwise the slideshow just auto-rotates through all the images. There is also a +/- to expand and contract a caption related to each image. I want to have the slideshow's automatic advancing to stop if one of the thumbnails is clicked, or the +/-. Basically, just stop whenever a user clicks anywhere within the gallery (div class=".homeImg"). I'm having a major brain fart in getting this working properly and could use some advice. Here's the jQuery:

$(document).ready(function() {

$(".main_image .desc").show(); //Show image info
$(".main_image .block").animate({ opacity: 0.85 }, 1 ); //Set Opacity

//Click and Hover events for thumbnail list
$(".image_thumb ul li:first").addClass('active');

// * Adds a class 'last' to the last li to let the rotator know when to return to the first
$(".image_thumb ul li:last").addClass('last');

$(".image_thumb ul li").click(function(){
//Set Variables
var imgAlt = $(this).find('img').attr("alt"); //Get Alt Tag of Image
var imgTitle = $(this).find('a').attr("href"); //Get Main Image URL
var imgDesc = $(this).find('.block').html(); //Get HTML of block
var imgDescHeight = $(".main_image").find('.block').height();   //Calculate height of block

if ($(this).is(".active")) { //If it's already active, then…
return false; // Don't click through
} else {
//Animate
$(".main_image img").animate({ opacity: 0}, 800 );
$(".main_image .block").animate({ opacity: 0, marginBottom: -imgDescHeight }, 800, function() {
$(".main_image .block").html(imgDesc).animate({ opacity: 0.85, marginBottom: "0" }, 250 );
$(".main_image img").attr({ src: imgTitle , alt: imgAlt}).animate({ opacity: 1}, 250 );
});
}

$(".image_thumb ul li").removeClass('active'); //Remove class of 'active' on all lists
$(this).addClass('active'); //add class of 'active' on this list only
return false;

}) .hover(function(){
$(this).addClass('hover');
}, function() {
$(this).removeClass('hover');
});

//Toggle teaser
$("a.collapse").click(function(){
$(".main_image .block").slideToggle();
$("a.collapse").toggleClass("show");
return false; // added to remove # browser jump
});

// If we are hovering over the image area, pause the clickNext function
pauseClickNext = false;
$(".homeImg").hover(
function () {
pauseClickNext = true;
},
function () {
pauseClickNext = false;
}
);

// Define function to click the next li
var clickNext = function(){
if(!pauseClickNext) {
/// find the next li after .active
var $next_li = $("li.active").next("li");
if($("li.active").hasClass("last") ){
$(".image_thumb ul li:first").trigger("click");
} else {
$next_li.trigger("click");
}
}
};

// Time between image transition
setInterval(clickNext, 6000);

});
+1  A: 

On your last line: var slideTimer = setInterval(clickNext, 6000);

Then when you want to stop the animation: clearInterval(slideTimer);

Then when you want to restart it just set it again: slideTimer = setInterval(clickNext, 6000);

mVChr
How is this tied to a click event? I'm not able to get this to stop the automation when any of the clicks I mentioned above happen. Thanks!
I should also add, that this seems to kill my caption show/hide feature. *EDIT - my mistake, typo.
You can add the second line to the events you want to stop the auto play and the third line to any events you want to restart the auto play.
mVChr
OK, yeah, that worked. Not sure what my problem was. Thank for the help!var slideTimer = setInterval(clickNext, 6000);$('.test').click(function() {clearInterval(slideTimer);});
A: 

Thanks man, your code works fine. Regards

Ziad