I am working with some pretty simple HTML, CSS, and JavaScript and running into a strange issue in Chrome that seemed to crop up out of nowhere. Basically, once the user scrolls past a certain point on the screen, a 'Return-To-Top' button is displayed in the bottom right corner. Clicking this button takes the user back to the top of the screen.
The issue I am having is that in Google Chrome only, when the button is changing from "display: none"; to "display: block", the mousewheel scrolling stops working for about five seconds.
If I scroll the page using the arrow keys or page up/down, it is not an issue. It seems to happen more often if I scroll slowly with the mouse wheel. When the scrolling is not working, if I continue to scroll the problem actually last longer. If I stop scrolling for a second and then begin scrolling again it works as expected.
HTML:
<button onclick="topFunction()" id="myScrollBtn"><i class="fa fa-arrow-up"></i></button>
CSS:
#myScrollBtn {
display: none;
position: fixed;
bottom: 20px;
right: 20px;
z-index: 99;
border: none;
outline: none;
background-color: #e2e2e3;
color: #e35205;
cursor: pointer;
padding: 12px;
border-radius: 10px;
opacity: .75;
}
#myScrollBtn:hover {
background-color: #909195;
}
Javascript that controls the display of the button:
//When the user scrolls down 200px from the top of the document, show the return-to-top button
window.onscroll = function () { scrollFunction() };
function scrollFunction() {
if (document.body.scrollTop > 200 || document.documentElement.scrollTop > 200) {
document.getElementById("myScrollBtn").style.display = "block";
} else {
document.getElementById("myScrollBtn").style.display = "none";
}
}
As stated, this is only an issue in Google Chrome. Scrolling is working fine in Edge, FireFox, IE and Safari. I temporarily commented out the javascript and verified that the scrolling issue was no longer present in Chrome, so it does appear to be related to the changing of the display.
If someone could point me in the right direction here that would be great.