I have a simple overlay that I want to catch click events on. Problem is, I don't want any clicks that occur within the content of the overlay to trigger an event. For example, if you look at my code snippet below, clicking inside the .content
div should not trigger the click event listener that I set on the overlay, but it does.
Basically I want to just know when someone clicks the black background of the overlay. Is there anyway to do this with how I currently have my code?
const overlay = document.querySelector('.overlay')
overlay.onclick = function() {
console.log('click on overlay!')
}
body {
margin: 0;
}
.overlay {
position: fixed;
top: 0;
left: 0;
background-color: rgba(0,0,0,0.5);
width: 100%;
height: 100%;
z-index: 10;
}
.content {
width: 200px;
height: 200px;
position: absolute;
z-index: 11;
top: calc(50% - 100px);
left: calc(50% - 100px);
background: white;
display: flex;
align-items: center;
justify-content: center;
}
<div class="overlay">
<div class="content">Hello World</div>
</div>
Comments
Post a Comment