【发布时间】:2014-12-11 13:06:21
【问题描述】:
我正在尝试实现一个简单的图像轮播,它可以无限地改变我网站的横幅图片。
这是我目前所拥有的:
function bannerSlider() {
setInterval(function() {
if ($(".pictureBanner img").hasClass("activePicture")) {
alert("fired");
$(".pictureBanner img").removeClass("activePicture");
$(".pictureBanner img").next().addClass("activePicture");
}
}, 4000);
}
$(document).ready(bannerSlider);
实施后,这会更改第一张图片,但随后会停止触发(警报在这里让我看到函数实际触发了)。
我对网络开发比较陌生,但这似乎在理论上应该可行。
任何提示都会很棒,谢谢。
编辑:下面添加了 html 标记。
<div class="pictureBanner"><img class="activePicture" src="img/img1.jpg"></div>
<div class="pictureBanner"><img src="img/img2.jpg"></div>
<div class="pictureBanner"><img src="img/img3.jpg"></div>
编辑:经过你们的大力帮助,我得到了它的工作。下面是最终代码:
HTML
>
<div id="bannerPictures">
<img class="activePicture" src="img/img1.jpg">
<img src="img/img2.jpg">
<img src="img/img3.jpg">
</div>
JS
函数bannerSlider() {
setInterval(function() {
var img = $("#bannerPictures img.activePicture");
var next = $(img.next());
next.addClass("activePicture");
img.removeClass("activePicture");
if (next.length === 0) {
$("#bannerPictures img").first().addClass("activePicture");
}
}, 4000);
}
$(document).ready(bannerSlider);
【问题讨论】:
-
显示您的 html 标记。
alert不是很好的调试机制,学习如何使用控制台日志 -
刚刚添加。感谢您的提醒提示,从现在开始我会牢记这一点。
标签: jquery image slider carousel