There is such code:
if (window.addEventListener) window.addEventListener('DOMMouseScroll', wheel, {passive: false} ); window.onmousewheel = document.onmousewheel = wheel; function wheel(event) { var delta = 0; if (event.wheelDelta) delta = event.wheelDelta / 120; else if (event.detail) delta = -event.detail / 3; handle(delta); if (event.preventDefault) event.preventDefault(); event.returnValue = false; } var goUp = true; var end = null; var interval = null; function handle(delta) { var animationInterval = 20; //lower is faster var scrollSpeed = 20; //lower is faster if (end == null) { end = $(window).scrollTop(); } end -= 20 * delta; goUp = delta > 0; if (interval == null) { interval = setInterval(function () { var scrollTop = $(window).scrollTop(); var step = Math.round((end - scrollTop) / scrollSpeed); if (scrollTop <= 0 || scrollTop >= $(window).prop("scrollHeight") - $(window).height() || goUp && step > -1 || !goUp && step < 1 ) { clearInterval(interval); interval = null; end = null; } $(window).scrollTop(scrollTop + step ); }, animationInterval); } }
body { padding: 0 20px; font-size: 20px; line-height: 1.5; }
<script src="https://code.jquery.com/jquery-3.1.0.js"></script> <main> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quidem repellat, fugiat iusto nam debitis magnam temporibus! Eius harum commodi ut suscipit veritatis accusantium ex at, vitae. Possimus deserunt totam perferendis veniam maiores nihil! Doloremque aliquam voluptate aperiam, eos nulla dolor? </p> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Similique molestiae cupiditate obcaecati consectetur dolor facilis quod consequuntur est cumque pariatur sed sint neque hic magni aliquid, cum placeat blanditiis! Ab cupiditate harum placeat, esse quaerat neque! Nam est excepturi, ab accusamus provident error, beatae mollitia rem deserunt architecto earum quos. Magnam animi harum perspiciatis autem id vitae doloremque, ducimus est fugit illum aspernatur tempora temporibus at esse voluptate possimus deserunt nesciunt corrupti mollitia odit, veniam architecto. Ea, consequatur, enim. Facilis ipsam numquam cum earum molestias, ab sequi reiciendis distinctio necessitatibus illum! Laborum error autem repudiandae rerum cum, dicta voluptatibus! Itaque. </p> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Beatae dolore eum molestias possimus dolor voluptatibus aliquid nobis quia dolorum nulla! </p> </main>
Smooth scrolling, everywhere works and worked in chrome until recently.
Now start to get out the error:
[Intervention] Unable to preventDefault inside passive event listener See
And smooth scrolling in chrome does not work.
Question: What is the error and how to solve the problem of smooth scrolling in google chrome for this code?
window.onmousewheel = document.onmousewheel = wheel;
- as you can see, passive is not specified here, therefore it istrue
, thereforepreventDefault
is forbiddenpreventDefault
- Grundy