I am trying to add a popup box to a website page that prompts users to click a button to access the rest of the site. The idea is that when they click the button in the popup box the entire popup becomes invisible and unable to interact with.
I tried to accomplish this by adding a .click css property that would make the popup opacity 0 and then adding the .click with the javascript. Genuinely not sure whats wrong so any help appreciated
let popup = document.querySelector('.popup');
let close_button = document.querySelector('.close_button');
close_button.onclick = function() {
popup.classList.add('.click');
}
.popup {
display: flex;
align-items: center;
justify-content: center;
transition: 0.2s;
}
.popup .click {
opacity: 0;
pointer-events: none;
}
.warning {
display: flex;
flex-direction: column;
background-color: blue;
padding: 1vh;
margin-top: 5vh;
}
.heading {
font-size: 60px;
color: azure;
padding-bottom: 1vh;
}
.message-box {
background-color: azure;
padding: 1vh;
}
.message {
font-size: 24px;
padding-bottom: 1vh;
}
.button-box {
display: flex;
align-items: center;
justify-content: center;
}
.close_button {
font-size: 24px;
background-color: blue;
color: azure;
border: 0px;
padding: 1vh;
border-radius: 5px;
transition: opacity 0.3s;
}
.close_button:hover {
opacity: 0.7;
}
.close_button:active {
opacity: 1;
}
<div class="popup">
<div class="warning">
<div class="heading">Warning</div>
<div class="message-box">
<div class="message">This website contains mature content not suited for minors</div>
<div class="button-box">
<button class="close_button" type="button">I am 18+</button>
</div>
</div>
</div>
</div>
2
Answers
The line
popup.classList.add('.click');
is incorrect. It should bepopup.classList.add('click');
Update the CSS selector to
.popup.click
to align the class addition and selector targeting.Might as well use a
<dialog>
. I added another<button>
for users that are under 18, which will make the browser jump back to the previous page (this feature will not work in the Snippet). Details are commented in the example below.