我试图找出为什么我的“后退”按钮无法正常工作。看起来我唯一做错的就是为previousSlide编写函数。希望有人可以提供帮助。请参阅下面的代码:
$(document).ready(function() {
$(function(){
var counter = 0;
var videos = [
{videoURL: 'media/SLIDE1.mp4'},
{videoURL: 'media/SLIDE2.mp4'},
{videoURL: 'media/SLIDE3.mp4'},
{videoURL: 'media/SLIDE4.mp4'},
{videoURL: 'media/SLIDE5.mp4'},
{videoURL: 'media/SLIDE6.mp4'},
{videoURL: 'media/SLIDE7.mp4'},
{videoURL: 'media/SLIDE8.mp4'},
{videoURL: 'media/SLIDE9.mp4'}
];
$('#next').click(updateSlide);
function updateSlide() {
console.log(counter);
console.log(videos[counter]);
$('video').attr('src', videos[counter].videoURL);
if (counter < videos.length - 1) {
counter++;
} else {
counter = 0;
}
//if you leave this uncommented it always increments counter, regardless of what you did above
//counter++;
}
$('#previous').click(previousSlide);
function previousSlide() {
console.log(counter);
console.log(videos[counter]);
$('video').attr('src', videos[counter].videoURL);
if (counter > 0) {
counter--;
} else {
counter = 0;
}
}
//Enables Left and Right Key Navigation
$(document).keydown(function(e){
if ( e.which === 37 ) { //left
$('#previous').click();
} else if ( e.which === 39 ) { //right
$('#next').click();
}
});
});
感谢您的帮助!
亚当
答案 0 :(得分:1)
之前减少:
function previousSlide() {
if (counter > 0) {
counter--;
} else {
counter = 0;
}
$('video').attr('src', videos[counter].videoURL);
}
为updateSlide
函数执行类似的操作(之前增加),您应该全部设置。似乎它只是一个错误的错误。
function updateSlide() {
console.log(counter);
console.log(videos[counter]);
if (counter < videos.length - 1) {
counter++;
} else {
counter = 0;
}
$('video').attr('src', videos[counter].videoURL);
}