jQuery选择滑块中的列表项
嘿 我正在构建一个简单的slider,现在它工作得很好,只是现在我仍然需要给类命名,比如slider1,slider2 enz 如果(sliderNumber==7),这部分看起来也很傻{ 我想知道如何改进我的代码,使其更具响应性jQuery选择滑块中的列表项,jquery,Jquery,嘿 我正在构建一个简单的slider,现在它工作得很好,只是现在我仍然需要给类命名,比如slider1,slider2 enz 如果(sliderNumber==7),这部分看起来也很傻{ 我想知道如何改进我的代码,使其更具响应性 $(document).ready(function () { fadeSlider(); }); function fadeSlider() { sliderNumber = 1; $('.slide'
$(document).ready(function () {
fadeSlider();
});
function fadeSlider() {
sliderNumber = 1;
$('.slide' + sliderNumber).addClass('active');
fader();
function fader() {
if (sliderNumber == 7) {
setTimeout(function () {
$('.slide' + sliderNumber).removeClass('active');
sliderNumber = 1;
$('.slide' + sliderNumber).addClass('active');
fader();
}, 1000);
} else {
setTimeout(function () {
$('.slide' + sliderNumber).removeClass('active');
sliderNumber++;
$('.slide' + sliderNumber).addClass('active');
fader();
}, 1000);
}
}
}
Html
JSFIDLE:您可以使用以下逻辑来切换活动类:
$(fadeSlider);
function fadeSlider() {
setInterval(function () { // use an interval if you want code to be called in loop
var $next = $('.active').next().length ? $('.active').next() : $('.totalRaised:first-child'); //'next' element would be: if there is an element following current active one, the next one, otherwise, the first child
$('.active').add($next).toggleClass('active');// toggle active class for the active one and the next one
}, 1000);
}
谢谢,你能试着解释一下你在这里做什么吗?@IvoJonkers你不明白哪一部分?@A.Wolff他正在学习..请解释你的代码:-)(编辑:错误标记)var$next=$('.active').next().length?$('.active').next():$('.totalRaised:first child'));我知道您正在获取下一个变量并将其添加到var中,但我不知道它是如何工作的。非常感谢您的解释!这使它更清晰了
.totalSlider li{
display: none;
list-style: none;
}
.totalSlider li.active{
display: inline-block;
}
$(fadeSlider);
function fadeSlider() {
setInterval(function () { // use an interval if you want code to be called in loop
var $next = $('.active').next().length ? $('.active').next() : $('.totalRaised:first-child'); //'next' element would be: if there is an element following current active one, the next one, otherwise, the first child
$('.active').add($next).toggleClass('active');// toggle active class for the active one and the next one
}, 1000);
}