js scrollIntoView不流畅,无法正常工作

时间:2019-12-03 23:56:58

标签: javascript events view scroll smoothing

嗨,我需要在平滑滚动到该视图中的帮助,还是有另一种方法可以更好地做到这一点?

这是我的代码:

document.addEventListener("click", (evt) => {
        const pack = document.querySelector('#packagingTitle');
        let targetElement = evt.target; // clicked element
        do {
            if (targetElement == pack) {
                // This is a click inside. Do nothing, just return.
                evt.preventDefault();
                pack.classList.toggle('activeCat');
                pack.classList.toggle('backgroundLight');
                document.querySelector('#packagingSection').classList.toggle('hidden');
                pack.scrollIntoView({behavior: "smooth", block: "end", inline: "nearest"});
                return;
            }
            // Go up the DOM
            targetElement = targetElement.parentNode;
        } while (targetElement);
        // This is a click outside.
        document.querySelector('#packagingSection').classList.add("hidden");
    });

谢谢!

0 个答案:

没有答案