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);
}
4

4 回答 4

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');
}

Demo: http://jsfiddle.net/DerekL/mqEbZ/

于 2013-03-13T04:07:10.980 回答
0

根据 Derek 的回答,悬停的替代方法是使用mouseenterand mouseleave

请参阅工作幻灯片 Jsfiddle:http: Demo: //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();
});
于 2013-03-13T05:21:33.363 回答
0

使用.delay()它会有所帮助。

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

于 2013-03-13T04:04:00.477 回答
0

我认为你需要两个函数...... slideShow()和另一个说pauseSlideshow()......现在在mouseout事件和mouseenter调用pauseSlideShow()上调用slideshow()

你的代码应该是这样的

$(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);
}

试试看

于 2013-03-13T04:09:27.567 回答