Javascript 滚动后删除动画
我试图在滚动到顶部位置后删除动画。如何删除此动画? 我是否需要Javascript 滚动后删除动画,javascript,Javascript,我试图在滚动到顶部位置后删除动画。如何删除此动画? 我是否需要setTimeout功能?或者我可以使用其他功能吗 window.onscroll = function () { scrollFunction() }; function scrollFunction() { if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) { document.getEl
setTimeout
功能?或者我可以使用其他功能吗
window.onscroll = function () {
scrollFunction()
};
function scrollFunction() {
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
document.getElementById("topButton").style.display = "block";
} else {
document.getElementById("topButton").style.display = "none";
}
}
function topFunction(ID, btn) {
var elmnt = document.getElementById(ID);
var animation = btn;
animation.classList.add('rotate');
elmnt.scrollIntoView({behavior: 'smooth'});
if (document.body.scrollTop == 0 || document.documentElement.scrollTop == 0) {
animation.classList.remove('rotate')
}
}
可以在某些元素上使用全局变量或类来检测动画何时完成 例如:
var isAnimated = false;
window.addEventListener('scroll', function(e) {
if (!isAnimated) {
scrollFunction();
} else {
window.removeEventListener('scroll');
}
}
function scrollFunction() {
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
document.getElementById("topButton").style.display = "block";
} else {
isAnimated = true;
document.getElementById("topButton").style.display = "none";
}
}
如果您使用
removeEventListener()
?添加EventListener(“滚动”,scroll函数),当它到达顶部removeEventListener(“滚动”,scroll函数)时