javascript - 悬停时暂停幻灯片放映

标签 javascript jquery slider slideshow mousehover

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

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

关于javascript - 悬停时暂停幻灯片放映,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38067566/

相关文章:

javascript - 缓存 JQuery UI-Tabs Ajax 加载的内容?

xamarin - Xamarin.forms 中的垂直 slider ?

javascript - 使用 SlickGrid 中的复选框获取单行?

javascript - 为什么用css3动画换背景图片会出现淡出效果?

javascript - 为什么文档选择器不适用于 mouseenter 事件?

jquery - 为什么剩下:auto; right:auto not centering a div?

javascript - Typescript:使用接口(interface)从字符串创建函数

javascript - 如何在 Chrome 中试用 SIMD 指令?

javascript - 在Qualtrics中,如何动态连接两个 slider ?

jquery - 我如何使用 jquery 和 css 实现这样的半 slider ?