悬停时暂停幻灯片放映

Pause slide show on hover

我有以下 运行 幻灯片脚本,效果很好。

jQuery(document).ready(function ($) {


setInterval(function () {
    moveRight();
}, 5000);


var slideCount = $('#slider ul li').length;
var slideWidth = $('#slider ul li').width();
var slideHeight = $('#slider ul li').height();
var sliderUlWidth = slideCount * slideWidth;

$('#slider').css({ width: slideWidth, height: slideHeight });

$('#slider ul').css({ width: sliderUlWidth, marginLeft: - slideWidth });

$('#slider ul li:last-child').prependTo('#slider ul');

function moveLeft() {
    $('#slider ul').animate({
        left: + slideWidth
    }, 500, function () {
        $('#slider ul li:last-child').prependTo('#slider ul');
        $('#slider ul').css('left', '');
    });
};

function moveRight() {
    $('#slider ul').animate({
        left: - slideWidth
    }, 500, function () {
        $('#slider ul li:first-child').appendTo('#slider ul');
        $('#slider ul').css('left', '');
    });
};

$('a.control_prev').click(function () {
    moveLeft();
});

$('a.control_next').click(function () {
     moveRight();
});
});

但我希望它在鼠标悬停时暂停,我已经尝试了我在 SO 上找到的其他一些建议(包括下面的建议),但无法使它们中的任何一个与我的代码一起工作. 有任何想法吗?谢谢

$("#slider ul").mouseover(function() {
$("#slider ul").trigger('pause', true);
});

您可以使用.stop()clearInterval()

// define reference to `setInterval()` call
var interval = setInterval(function () {
    moveRight();
}, 5000);

// stop animations, clear `interval`
$("#slider ul").mouseover(function() {
  $(this).stop();
  clearInterval(interval);
});

您可以使用clearInterval()删除间隔。

希望对您有所帮助:

var timerObject=null; 

function StartSlide(){
timerObject = setInterval(function () {
    moveRight();
}, 5000);
}

function stopSlide(){
clearInterval(timerObject)
}

$("#slider ul").mouseover(function() {
stopSlide()
});

$("#slider ul").mouseleave(function() {
StartSlide()
});
.stop() | jQuery API

JSfiddle 示例: