个人资料滚动应用:如何返回上一个个人资料?(JS)



我正在尝试构建一个配置文件滚动器类型的应用程序,但我目前被困在我应该如何返回以前的配置文件上。请参阅下面的代码:

let profiles = profileIterator(profiles); // array of objects stored in a separate .js file
//  Next Event
next.addEventListener("click", nextProfile);

// Next Profile Display
function nextProfile() {
const currentProfile = profiles.next().value;
if(currentProfile !== undefined) {
name.innerHTML = `<h4>${currentProfile.name}</h4>`; // content on HTML page that will be changed
text.innerHTML = `<p>${currentProfile.info}</p>`
img.innerHTML = `<img src="${currentProfile.img}">`
next.innerHTML = 'Next';
previous.style.display = "inline-block";
previous.innerHTML = 'PREVIOUS';
} else {
window.location.reload();
}
}
function previousProfile() {
const currentProfile = profiles.previous().value;
if(currentProfile !== undefined) {
name.innerHTML = `<h4>${currentProfile.name}</h4>`;
text.innerHTML = `<p>${currentProfile.info}</p>`
img.innerHTML = `<img src="${currentProfile.img}">`
} else {
window.location.reload();
}
}
//  Profile Iterator
function profileIterator(profiles) {
let nextIndex = 0;
return {
next: function() {
return nextIndex < profiles.length
? { value: profiles[nextIndex++], done: false }
: { done: true }
},
previous: function() {
return nextIndex < profiles.length
? { value: profiles[nextIndex--], done: false }
: { done: true}
}
};
}

起初对我来说似乎很简单,但显然,我错了,因为每当我单击"上一个"按钮时,它不是浏览以前的配置文件,而是将它们按没有特定顺序混淆,也完全错过了其中一个。

有什么建议吗?有没有办法从批处理中设置 HTML5 视频元素的属性?

你确定在返回.previous((时你应该检查nextindex是否<profiles.length?乍一看,这不可能是错误的,也许这是你问题的根源。>

相关内容

最新更新