我的标签是自动滑动的,我在悬停时需要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)CODE
$(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吗?
答案 0 :(得分:0)
此代码将在任何标签悬停时暂停自动幻灯片,并在鼠标移开时重新启动。
//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();
});`