我使用jq.carousel(http://www.eafit.edu.co/banners-eafit/carousel/index.html)来展示一些房间的图像。当您单击房间的幻灯片时,它将指向具有唯一URL的页面,例如http; // www ..... / 14095 /.
我的第一个代码可以使用,但是当我尝试将" room"使用for循环和数组将脚本合并为一个(参见第二个代码),它不起作用。有谁知道我缺少或需要更新的内容?谢谢你!
注意:"开始:"在脚本中基本上告诉加载页面后应该出现什么幻灯片
代码1:正常工作
<script src="/path/to/jq.carousel.js"></script>
<script>
(function() {
// Room 1
if (window.location.href.indexOf('14083') > -1) {
var $carousel = $('#carousel_2').carousel({
start: 1,
indicator: true
});
}
// Room 2
if (window.location.href.indexOf('14094') > -1) {
var $carousel = $('#carousel_2').carousel({
start: 2,
indicator: true
});
}
// Room 3
if (window.location.href.indexOf('14095') > -1) {
var $carousel = $('#carousel_2').carousel({
start: 3,
indicator: true
});
}
// Script for Prev and Next buttons to navigate through the carousel
var totalSlides = $(".carousel_box").length - 4;
$('#carousel_2_prev').on('click', function(ev) {
ev.preventDefault();
$carousel.carousel('prev');
});
$('#carousel_2_next').on('click', function(ev) {
ev.preventDefault();
$carousel.carousel('next');
});
}());
</script>
代码2:合并&#34;房间&#34;脚本
<script src="/path/to/jq.carousel.js"></script>
<script>
var nodearray = ['14083', '14094', '14095'];
(function() {
// Section that needs to be updated
for (var i = 0; i < nodearray.length; i++) {
if (window.location.href.indexOf(nodearray[i]) > -1) {
var $carousel = $('#carousel_2').carousel({
start: i++,
indicator: true
});
}
}
// Script for Prev and Next buttons to navigate through the carousel
var totalSlides = $(".carousel_box").length - 4;
$('#carousel_2_prev').on('click', function(ev) {
ev.preventDefault();
$carousel.carousel('prev');
});
$('#carousel_2_next').on('click', function(ev) {
ev.preventDefault();
$carousel.carousel('next');
});
}());
</script>
答案 0 :(得分:0)
您可能不希望第二行代码块中的第8行使用++。这将增加计数变量i
。
<script src="/path/to/jq.carousel.js"></script>
<script>
var nodearray = ['14083', '14094', '14095'];
(function() {
// Section that needs to be updated
for (var i = 0; i < nodearray.length; i++) {
if (window.location.href.indexOf(nodearray[i]) > -1) {
var $carousel = $('#carousel_2').carousel({
start: i+1,
indicator: true
});
}
}
// Script for Prev and Next buttons to navigate through the carousel
var totalSlides = $(".carousel_box").length - 4;
$('#carousel_2_prev').on('click', function(ev) {
ev.preventDefault();
$carousel.carousel('prev');
});
$('#carousel_2_next').on('click', function(ev) {
ev.preventDefault();
$carousel.carousel('next');
});
}());
</script>