javascript - 悬停时暂停以实现简单的 jquery 轮播

标签 javascript jquery hover carousel

我在这里设置了一个 super 简单的功能轮播示例:

http://codepen.io/anon/full/myvAz

问题是,我无法让轮播在悬停时停止旋转。

即使它具有设定的宽度和高度,我什至无法在将鼠标悬停在包含的 div 上时收到触发警报。 但是,如果我将悬停部分粘贴到控制台中,我可以收到触发警报。

与其他代码结合使用时,它似乎不会触发。

如有任何帮助,我们将不胜感激

谢谢

代码如下:

<script>
// Main image carousel
$(document).ready(function(){
$("#headerMrS > div:gt(0)").hide();

var carouDiv = function(){
    setInterval(function() { 
      $('#headerMrS > div:first')
        .fadeOut(500)
        .next()
        .fadeIn(500)
        .end()
        .appendTo('#headerMrS');
    },  2000);
};

$(carouDiv());//Initialise the carousel function

$("#headerMrS").hover(function(){//Stop the carousel on hover
 $(this).stop;
  },function(){
  $(this).resume;
});


//Direction Arrow links
$(".button-sales").click(function(){
    $(".header").fadeOut(800);
    $(".sales").animate({opacity:"show"},800);
});
$(".button-modern").click(function(){
    $(".header").fadeOut(800);
    $(".modern").animate({opacity:"show"},800);
});
$(".button-antique").click(function(){
    $(".header").fadeOut(800);
    $(".antique").animate({opacity:"show"},800);
});
});

<style>

#headerMrS {position:relative; height:150px; width:350px}
.header {position:absolute; top:0; left:0}
</style>

<div id="headerMrS">
<div class="header sales active">
    <div class="header-content">
        <img src="http://placehold.it/350x150/4787ed/ffffff" alt="" />
        <div class="button-next button-antique">next</div>
        <div class="button-prev button-modern">prev</div>
    </div>
</div>
<div class="header antique">
    <div class="header-content">
        <img src="http://placehold.it/350x150/fc8a41/ffffff" alt="" />

        <div class="button-next button-modern">next</div>
        <div class="button-prev button-sales">prev</div>
    </div>
</div>
<div class="header modern">
    <div class="header-content">
        <img src="http://placehold.it/350x150/e7376b/ffffff" alt="" />

        <div class="button-next button-sales">next</div>
        <div class="button-prev button-antique">prev</div>
    </div>
</div>

最佳答案

使用clearInterval()怎么样?

不要简单地调用 setInterval(),而是将 setInterval() 函数分配给一个变量,下面我使用 carouselInt。这是调用 clearInterval() 所必需的。 要停止间隔,您可以调用 clearInterval(carouselInt)

根据我刚刚读到的内容,stop() 将停止当前动画,但每 2 秒就会有一个新动画。它对导致动画触发的 setInterval 没有明显影响。

您可以尝试以下操作。

var carouselInt = '';

var carouDiv = function(){
    carouselInt = setInterval(function() { 
      $('#headerMrS > div:first')
        .fadeOut(500)
        .next()
        .fadeIn(500)
        .end()
        .appendTo('#headerMrS');
    },  2000);
};

$(carouDiv());//Initialise the carousel function

$("#headerMrS").hover(function(){//Stop the carousel on hover
 clearInterval(carouselInt);
  },function(){
  carouDiv();
});

关于javascript - 悬停时暂停以实现简单的 jquery 轮播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14217106/

相关文章:

javascript - 使用 Control.Measure : no method 'setMap' 时出错

javascript - "[native code] "是什么意思?

css - 如何在悬停时停止悬 float 画

与 a.selected 效果相同的 css 命令,但对于 <p>

javascript - iPad 上的悬停 js 功能不会切换

javascript - Phonegap Sqlte :the statement callback raised an exception or statement errorcallback did not return false

javascript - React.js 返回简单的链接列表

javascript - 检查是否使用 mocha 触发了一个事件

javascript - 根据导航菜单中的 url 更改下拉选项

java - 是否可以使用 jsp 变量值来初始化 JQUERY 变量?