I am trying to create a simple modal popup. I have CSS, and Javascript is basically done. I am at the point where I click the button on the page button and a popup appears, but it appears at the top of the page. It seems that it would be nice if you scroll to the very top of the page.
However, I want the modal popup to appear in the center based on the current scroll position. I also want it to stay centered when I scroll. At the moment, he does not swim in the center of the screen.
What javascript property should be used to adjust the position of the pop-up div when scrolling.
I tried, but to no avail:
function showModal(id) { window.onscroll = function () { document.getElementById(divID).style.top = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop; }; document.getElementById(id).style.display = "block"; document.getElementById(id).style.top = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop; }; }
I use direct vanilla javascript, without jQuery.
TL DR: I created a modal popup, but it does not stay centered when I scroll away from the very top of the page.
source share