javascript - 停止用户悬停中自动滑动选项卡

标签 javascript jquery tabs hover slide

我的选项卡是自动滑动的,悬停时我需要 Puss 并自动运行,现在用户悬停可以停止自动滑动选项卡吗?我的 html、css 和 js 代码是: HTML 代码

<ul id='tabs'>
<li class='on'>tab 1</li>
<li>tab 2</li>
<li>tab 3</li>
<li>tab 4</li>
<li>tab 5</li>

CSS 代码

#tabs { list-style: none; margin: 0; padding: 0; }
#tabs li { float: left; background: #ddd; padding: 6px; }
#tabs li.on { background: #f90; color: #fff; }

JS(jQuery)代码

$(function() {

//cache a reference to the tabs
var tabs = $('#tabs li');

//on click to tab, turn it on, and turn previously-on tab off
tabs.click(function() { $(this).addClass('on').siblings('.on').removeClass('on'); });

//auto-rotate every 5 seconds
setInterval(function() {

        //get currently-on tab
    var onTab = tabs.filter('.on');

        //click either next tab, if exists, else first one
    var nextTab = onTab.index() < tabs.length-1 ? onTab.next() :      tabs.first();
    nextTab.click();
}, 5000);
  });

Hove 可以在我的代码中使用clearInterval 吗?

最佳答案

此代码将在任何选项卡悬停时暂停自动滑动,并在鼠标移开时重新启动`

//cache a reference to the tabs
var tabContainer = $('#tabs');
var tabs = $('#tabs li');

//on click to tab, turn it on, and turn previously-on tab off
tabs.click(function() {
    $(this).addClass('on').siblings('.on').removeClass('on');
});

//auto-rotate every 5 seconds
var slideInterval;

function initiateSlideInterval() {
    slideInterval = setInterval(function() {

        //get currently-on tab
        var onTab = tabs.filter('.on');

        //click either next tab, if exists, else first one
        var nextTab = onTab.index() < tabs.length - 1 ? onTab.next() : tabs.first();
        nextTab.click();
    }, 5000);

}
initiateSlideInterval();

tabContainer.mouseover(function() {
    clearInterval(slideInterval)
});
tabContainer.mouseout(function() {
    initiateSlideInterval();
});`

关于javascript - 停止用户悬停中自动滑动选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41416207/

相关文章:

javascript - 如何从 .js 文件而不是 webpack.config 配置和运行 webpack

Android 操作栏选项卡图标 png 尺寸

javascript - CSS 页面过渡留下痕迹

javascript - `[之间的差异。 ]` vs ` .` 在正则表达式中

javascript - 如何使用 jQuery 将一个类替换为另一个类?

jquery - 不使用显示隐藏上传表单 : none

javascript - Valums 文件 uploader 在 Internet Explorer 9 下不工作

jquery - css 左帮助 jquery

javascript - 如何在选项卡标题部分创建带有图像的可切换选项卡?

javascript - 在 Node.js 中调整图像大小的简单方法?