让jQuery幻灯片动画停止点击

时间:2010-05-25 17:18:45

标签: jquery slideshow

我有一个用jQuery构建的幻灯片放映暂停。它有一组位于图像顶部的缩略图,可在单击时使图像前进,否则幻灯片只会自动旋转所有图像。还有一个+/-来扩展和收缩与每个图像相关的标题。如果单击其中一个缩略图,或者+/-,我希望幻灯片的自动前进停止。基本上,只要用户点击图库中的任何位置(div class =“。homeImg”),就停止。让我正常工作并且可以使用一些建议我有一个主要的大脑放屁。这是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);

});

2 个答案:

答案 0 :(得分:2)

在最后一行:var slideTimer = setInterval(clickNext, 6000);

然后当你想要停止动画时:clearInterval(slideTimer);

然后,当您想要重新启动它时,只需再次设置:slideTimer = setInterval(clickNext, 6000);

答案 1 :(得分:0)

我最近在div中设置了一个幻灯片,并使用递归使幻灯片继续播放,直到我的“stop”变量设置为“true”。使用此方法,幻灯片放映动画将在您设置stop = true时随时停止;在onclick或其他期间:

var stop = false;
var i = 0;

function playGallery(){         
  $("#playerWrapper").fadeOut(1500, function() {

    if(i<8){
      i = i+1;
    } 
    else{
      i = 1;
    }

    $("#playerWrapper")
     .css('background-image','url("images/' + 'brooke' + i + '.jpg' + '")');

    if(!stop) {
      //Use recursion here to keep rotating slides until stop==true
      playGallery();
    }

  }).fadeIn(1500);
}