我在这个Shopify主题中有一个旋转木马,我正在尝试编辑它以使其每5秒自动滚动到下一张幻灯片(而不是点击进入下一张幻灯片)。
以下是我用于旋转木马的内容:
//Carousel interaction
(function() {
if (document.getElementById('carousel-nav')) {
var slides = document.getElementById('carousel-images'),
slidesItems = slides.getElementsByTagName('li'),
nav = document.getElementById('carousel-nav'),
navItems = nav.getElementsByTagName('li'),
current = 0;
function showSlide(i) {
if (i != current && slidesItems[i]) {
slide = slidesItems[i];
slide.className += ' show';
setTimeout (function() {
slide.className = slide.className.replace('show', 'appear');
}, 1);
setTimeout(function() {
slidesItems[current].className = slidesItems[current].className.replace('current', '');
slide.className = slide.className.replace('appear', 'current');
current = i;
}, 300);
navItems[i+1].className += ' current';
navItems[current+1].className = navItems[current+1].className.replace('current', '');
if (i == 0) {
if (navItems[0].className.indexOf('disabled') == -1) {
navItems[0].className += ' disabled';
}
} else {
navItems[0].className = navItems[0].className.replace(' disabled', '');
}
var l = navItems.length - 1;
if (i == slidesItems.length - 1) {
if (navItems[l].className.indexOf('disabled') == -1) {
navItems[l].className += ' disabled';
}
} else {
navItems[l].className = navItems[l].className.replace(' disabled', '');
}
}
}
nav.onclick = function(e) {
e = e || window.event; e = e.target || e.srcElement;
e = getParentByTagName(e, 'A');
if (e) {
var action = e.getAttribute('data-action');
if (action == 'prev') {
showSlide(current - 1);
} else if (action == 'next') {
showSlide(current + 1);
} else {
showSlide(parseInt(action));
}
}
return false;
}
}
})();
有什么想法吗?我有点难过。
谢谢!
答案 0 :(得分:3)
让我跟进最后一个答案,因为这个想法是正确的。你想要做的是创建一个自我延续循环。一种方法是在if语句的末尾再次调用slide函数。要做到这一点,我们将使用setInterval(),它有两个参数,第一个是函数(当我们加1时),第二个是以毫秒为单位(在这种情况下我们要将它设置为10)秒(10000毫秒)。
您的代码看起来应该是这样的:
//Carousel interaction
(function() {
if (document.getElementById('carousel-nav')) {
var slides = document.getElementById('carousel-images'),
slidesItems = slides.getElementsByTagName('li'),
nav = document.getElementById('carousel-nav'),
navItems = nav.getElementsByTagName('li'),
current = 0;
function showSlide(i) {
if (i != current && slidesItems[i]) {
slide = slidesItems[i];
slide.className += ' show';
setTimeout (function() {
slide.className = slide.className.replace('show', 'appear');
}, 1);
setTimeout(function() {
slidesItems[current].className = slidesItems[current].className.replace('current', '');
slide.className = slide.className.replace('appear', 'current');
current = i;
}, 300);
navItems[i+1].className += ' current';
navItems[current+1].className = navItems[current+1].className.replace('current', '');
if (i == 0) {
if (navItems[0].className.indexOf('disabled') == -1) {
navItems[0].className += ' disabled';
}
} else {
navItems[0].className = navItems[0].className.replace(' disabled', '');
}
var l = navItems.length - 1;
if (i == slidesItems.length - 1) {
if (navItems[l].className.indexOf('disabled') == -1) {
navItems[l].className += ' disabled';
}
} else {
navItems[l].className = navItems[l].className.replace(' disabled', '');
}
}
}
nav.onclick = function(e) {
e = e || window.event; e = e.target || e.srcElement;
e = getParentByTagName(e, 'A');
if (e) {
var action = e.getAttribute('data-action');
if (action == 'prev') {
showSlide(current - 1);
} else if (action == 'next') {
showSlide(current + 1);
} else {
showSlide(parseInt(action));
}
}
return false;
}
setInterval(current+1, 10000);
}
})();
答案 1 :(得分:1)
这只是使用类似showSlide()
的内容,每隔五秒触发setTimeout()
方法。
要让它前进到下一张幻灯片,您需要触发showSlide(current + 1)
。
答案 2 :(得分:1)
语法有点偏。这样就可以了:
setInterval(function() { showSlide(current + 1) }, 5000);
它应该放在代码的更下方,因此它不在nav.onclick块中,但仍在原始函数中。见下面的例子。
nav.onclick = function(e) {
e = e || window.event; e = e.target || e.srcElement;
e = getParentByTagName(e, 'A');
if (e) {
var action = e.getAttribute('data-action');
if (action == 'prev') {
showSlide(current - 1);
} else if (action == 'next') {
showSlide(current + 1);
} else {
showSlide(parseInt(action));
}
}
return false;
}
}
setInterval(function() { showSlide(current + 1) }, 5000);
})();
答案 3 :(得分:0)
如果您想在单击按钮后停止滚动,请确保将该setInterval添加到变量中,然后在使用clearInterval单击时将其停止。请参阅下面的代码。
var rotation = setInterval(function() { showSlide(current + 1) }, 7000);
nav.onclick = function(e) {
e = e || window.event; e = e.target || e.srcElement;
e = getParentByTagName(e, 'A');
if (e) {
var action = e.getAttribute('data-action');
if (action == 'prev') {
showSlide(current - 1);
} else if (action == 'next') {
showSlide(current + 1);
} else {
showSlide(parseInt(action));
}
}
clearInterval(rotation);
return false;
}