I created a modal using only HTML, CSS, and JavaScript. When I click a button on the main page, the modal pops up.
However, if I scroll down in the modal and then close it, when I reopen it, it still shows the scrolled-down view. How can I make it always display the content at the top whenever I open the modal?
Here is the code I used:
<div class="button" id="btn"></div>
<div class="modal" id="mymdl">
<div class="modal-content" id="mdl-con">
<div class="modalwidth">
<img class="image-title" id="img-ttl" src="./img/banner.png">
<span class="close" aria-hidden="true">×</span>
</div>
<div class="paragraph">
Here is the main content.
</div>
</div>
</div>
document.getElementById('btn').addEventListener('click', function(e) {
modal.style.display = "block";
});
var closeX = document.getElementsByClassName("close")[0];
closeX.onclick = function() {
modal.style.display = "none";
}
I attempted to use the window.scroll
method suggested in a solution for this issue, but it did not resolve my problem.
As a beginner in HTML, CSS, and JavaScript, any advice on how to fix this would be greatly appreciated! Thank you in advance.