Skip to content

Instantly share code, notes, and snippets.

@savandriy
Last active December 13, 2019 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save savandriy/237f8ffd7cccdf00e0d4a72c4b6cc383 to your computer and use it in GitHub Desktop.
Save savandriy/237f8ffd7cccdf00e0d4a72c4b6cc383 to your computer and use it in GitHub Desktop.
A hack to prevent scroll chaining in Vanilla JS (re-wrote the code from here https://stackoverflow.com/a/39307075/5952509); btw, you should just use overscroll-behavior: none;
var element = document.getElementsByClassName("your-class-here")[0];
var scrollHandler = function (e) {
e.preventDefault();
document.documentElement.scrollTop = window.currentScrollTop;
document.documentElement.scrollLeft = window.currentScrollLeft;
};
element.addEventListener('mouseenter', function () {
var scrollTop = document.documentElement.scrollTop;
window.currentScrollTop = scrollTop;
window.currentScrollLeft = scrollTop;
window.addEventListener("scroll", scrollHandler)
});
element.addEventListener('mouseleave', function () {
window.removeEventListener('scroll', scrollHandler);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment