javascript - 鼠标未悬停时重新启动图片库

标签 javascript jquery html css

当鼠标没有悬停在图库上时,如何重新启动图库?我知道我很接近;但是,我想不通。我改编了这个 Simple-jQuery-Carousel-Slider来自 github 上的@paulmason。

js代码:

jQuery(function ($) {

// settings
var $slider = $('.slider'); // class or id of carousel slider
var $slide = 'img'; // could also use 'img' if you're not using a ul
var $transition_time = 1000; // 1 second
var $time_between_slides = 4300; // 4 seconds

function slides() {
    return $slider.find($slide);
}

slides().fadeOut();

// set active classes
slides().first().addClass('active');
slides().first().fadeIn($transition_time);

// auto scroll 
    $interval = setInterval(
      function () {
          var $i = $slider.find($slide + '.active').index();

          slides().eq($i).removeClass('active');
          slides().eq($i).fadeOut($transition_time);

          if (slides().length == $i + 1) $i = -1; // loop to start

          slides().eq($i + 1).fadeIn($transition_time);
          slides().eq($i + 1).addClass('active');
      }
      , $transition_time + $time_between_slides
    );
function pauseLoop() {
        window.clearInterval($interval);
    }

$(".slider").hover(
    function () {
        //alert("pause");
        pauseLoop(); // pause the loop  | works 
    },
    function () {
        //alert("unpause");
        $interval; //scroll()  here is the problem 
    }
);
});

html 很简单。只是一个带有图像的 div。

编辑 jsfiddle:http://jsfiddle.net/xDf4Z/10

最佳答案

将区间包装在函数中,声明区间变量,将函数返回到外部并将悬停关闭设置为新建函数。

jQuery(function ($) {

    // settings
    var $slider = $('.slider'); // class or id of carousel slider
    var $slide = 'img'; // could also use 'img' if you're not using a ul
    var $transition_time = 1000; // 1 second
    var $time_between_slides = 4300; // 4 seconds
    var $interval;

    function slides() {
        return $slider.find($slide);
    }

    slides().fadeOut();

    // set active classes
    slides().first().addClass('active');
    slides().first().fadeIn($transition_time);

    // auto scroll 
    function startloop(){
    $interval = setInterval(

    function () {
        var $i = $slider.find($slide + '.active').index();

        slides().eq($i).removeClass('active');
        slides().eq($i).fadeOut($transition_time);

        if (slides().length == $i + 1) $i = -1; // loop to start

        slides().eq($i + 1).fadeIn($transition_time);
        slides().eq($i + 1).addClass('active');
    }, $transition_time + $time_between_slides);
}
    function pauseLoop() {
        window.clearInterval($interval);
    }

    $(".slider").hover(

    function () {
        //alert("pause");
        pauseLoop(); // pause the loop
    },

    function () {
        //alert("unpause");
          startloop(); //scroll()
    });
    return startloop();
});

http://jsfiddle.net/xDf4Z/19/

关于javascript - 鼠标未悬停时重新启动图片库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24876436/

相关文章:

javascript - 如何只在鼠标位置div上滚动?

html - 根据 Sprite 使我的表单响应

javascript - 使用 jquery 或 javascript 更改 html 中类属性的值

jQuery:使用 .replace() 替换元素内的文本

Javascript - 切换多个类 onclick

javascript - 将 javascript 确认框中的文本显示为粗体字母

javascript - Wordpress 主题中古怪的 JS 代码

javascript - 选择要加载的 javascript/jquery 库

html - 给 div 添加 border-bottom

javascript - Shiny :在登录屏幕上使用带有操作按钮的回车键