Whenever I click on a specific div
, I use jQuery
to dynamically apply overflow: hidden
to the html
and body
.
$(".mydiv").on("click", function() {
$("html, body").css("overflow", "hidden");
});
However, this action causes the window to scroll to the top of the page and lose its current scroll position. To regain the scroll position, I simply remove the overflow property like this:
$("html, body").css("overflow", "");
This issue occurs when the user clicks outside the previously opened div
. Any suggestions on how to prevent this?