2013-03-13 81 views
3

当我将鼠标悬停在jQuery幻灯片的图像上时,如何添加暂停效果?如何在悬停时暂停幻灯片

$(document).ready(function() { 
    slideShow(); 
}); 

function slideShow() { 
    var showing = $('#slideshow .show'); 
    var next = showing.next().length ? showing.next() : showing.parent().children(':first'); 
    var timer; 
    showing.fadeOut(500, function() { 
     next.fadeIn(200).addClass('show'); 
    }).removeClass('show'); 
    setTimeout(slideShow, 3000); 
} 

回答

0

使用.delay(),这将有助于。

描述:设置一个计时器来延迟队列中后续项目的执行。

3
var hovering = false;    //default is not hovering 
$("#slideshow").hover(function() { //*replace body with your element 
    hovering = true;    //when hovered, hovering is true 
}, function() { 
    hovering = false;    //when un-hovered, hovering is false 
    slideShow();     //start the process again 
}); 

function slideShow() { 
    if(!hovering) {     //if not hovering, proceed 
     /* Your code here*/ 
     nextSlide(); 
     setTimeout(slideShow, 1000); 
    } 
} 

function nextSlide(){ 
    var showing = $('#slideshow .show'); 
    var next = showing.next().length ? showing.next() : showing.parent().children(':first'); 
    var timer; 
    showing.fadeOut(500, function() { 
     next.fadeIn(200).addClass('show'); 
    }).removeClass('show'); 
} 

演示:http://jsfiddle.net/DerekL/mqEbZ/

+0

OK,我出现的图像,但它不会改变图片。 – 2013-03-13 04:24:19

+0

好吧,我又重新开始工作了,但似乎悬停并不奏效。 – 2013-03-13 04:36:11

+0

@GarrettJohnson - 你是否用目标元素替换了'$(“body”)? – 2013-03-13 04:45:57

0

我认为你需要为两个功能......放幻灯片()和其他人说pauseSlideshow()...现在所说的幻灯片()鼠标移开时的事件和调用的mouseenter pauseSlideShow()

你的代码应该是这样的

$(document).ready(function(){ 

$('.slider').mouseout(slideShow()); 
$('.slider').mouseenter(pauseSlideShow()); 
}); 
function slideShow() { 
var showing = $('#slideshow .show'); 
var next = showing.next().length ? showing.next() : showing.parent().children(':first'); 
var timer; 
showing.fadeOut(500, function() { next.fadeIn(200).addClass('show'); }).removeClass('show');  
timeOut = setTimeout(slideShow, 3000);   
} 

function PauseSlideShow() { 
window.clearTimeout(timeOut); 
} 

TRY IT

0

工作过德里克的回答,徘徊另一种是使用mouseentermouseleave

见工作幻灯片的jsfiddle:演示:http://jsfiddle.net/highwayoflife/6kDG7/

var hovering = false; 
var slideshow = $("#slideshow"); 

slideshow.mouseenter(function() { 
    hovering = true; 
}).mouseleave(function() { 
    hovering = false; 
    slideShow(); 
}); 

function slideShow() { 
    if (!hovering) { 
     # Some browsers don't interpret "display:block" as being visible 
     # If no images are deemed visible, choose the first... 
     var currentImg = (slideshow.children("img:visible").length) ? slideshow.children("img:visible") : slideshow.children("img:first"); 
     var nextImg = (currentImg.next().length) ? currentImg.next() : slideshow.children("img:first"); 

     currentImg.fadeOut(500, function() { 
      nextImg.fadeIn(500, function() { 
       setTimeout(slideShow, 2000); 
      }); 
     }); 
    } 
} 
$(document).ready(function() { 
    slideShow(); 
});