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

时间:2018-10-02 06:07:11

标签: javascript html css

我正在尝试构建一个配置文件滚动器类型的应用程序,并且目前我仍停留在应该如何返回上一个配置文件的位置。请参见下面的代码:

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}
        }
    };
}

起初,这对我来说似乎很简单,但是显然,我错了,因为每当我单击“上一步”按钮时,它都没有按特定顺序将它们混杂在一起,而不是遍历先前的配置文件,从而完全丢失了其中一个好。

有什么建议吗?是否可以通过Batch设置HTML5视频元素的属性?

1 个答案:

答案 0 :(得分:0)

您确定返回.previous()时应检查nextIndex