在滚动时,Chrome不会阻止鼠标滚轮事件

问题描述

有人知道如何解决此问题吗?

已设置超时以防止 2秒后发生鼠标滚轮事件,但是您可以尝试连续上下滚动,直到停止滚动才可以阻止它。

错误仅在Chrome,Firefox和Edge正常工作时出现。

const preventDefault = (e) =>{
    e.preventDefault()
}

const setEvent = () => {
    setTimeout(() => {
        window.addEventListener("mousewheel",preventDefault,{ passive: false });
        window.addEventListener("DOMMouseScroll",{ passive: false });
        console.log('Stopped!')
        document.querySelector('div').style.display = 'block'
    },2000)
}

setEvent()

document.querySelector('button').addEventListener('click',() =>{
    document.querySelector('div').style.display = 'none'
    window.removeEventListener("mousewheel",{ passive: false });
    window.removeEventListener("DOMMouseScroll",{ passive: false });
    setEvent()
})
section{
    background: linear-gradient(180deg,rgba(2,36,1) 0%,rgba(9,9,121,1) 35%,rgba(0,212,255,1) 100%);
}

.as-console-row:not(:last-of-type){
    display: none
}

.content{
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    max-width: 500px;
    text-align: center;
}

h1{
    font-family: 'Arial';
    font-size: 24px;
    margin: 0 0 20px;
    color: #fff
}
<section style="height: 1000vh"></section>
<div class="content" style="display: none">
    <h1>Scroll prevented after 2 seconds but you can still scroll until you stop scrolling</h1>
    <button>Try again</button>
</div>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...