Create PopUp Box through pure JavaScript
How to create popup box through pure JavaScript with ID and Class.
Simple copy the code and paste in your HTML file and check popup is working.
<style>
.container{margin:0 auto; width:1170px;}
.row{width:100%; display:flex; justify-content:space-between;}
.row button{background-color:#099; padding:5px 10px; border-radius:5px; font:16px/24px Arial, Helvetica, sans-serif; color:#fff; border:0; cursor:pointer;}
.modalbox{background-color:#fff; border:1px solid #000; border-radius:5px; padding:10px; position:absolute; top:25%; left:0; width:600px; transform:translate(100%); z-index:9;display:none;}
.modalbox.active{display:block;}
.closemodal{display:flex; margin-left:auto; font:bold 20px/28px Arial, Helvetica, sans-serif; color:#000; position:absolute; right:10px; top:10px; text-decoration:none}
.modalbox h2{font:20px/28px Arial, Helvetica, sans-serif; color:#000; font-weight:bold; display:block;}
.modalbox p{font:20px/28px Arial, Helvetica, sans-serif; color:#000; font-weight:normal; display:block;}
.hidden{display:none;}
.overlay{background-color:rgba(0, 0, 0, 0.5); position:fixed; width:100%; top:0; height:100%; display:none}
</style>
HTML :-
<div class="container">
<div class="row">
<button class="button" target_model="#model1">Click Me</button>
<button class="button" target_model="#model2">Click Me</button>
<button class="button" target_model=".model3">Click Me</button>
</div>
</div>
<div class="modalbox " id="model1"> <a href="#" class="closemodal">X</a>
<h2>What is Lorem Ipsum?</h2>
<p> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</div>
<div class="modalbox " id="model2"> <a href="#" class="closemodal">X</a>
<h2>What is Lorem Ipsum? 2</h2>
<p> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</div>
<div class="modalbox model3" > <a href="#" class="closemodal">X</a>
<h2>What is Lorem Ipsum? 3</h2>
<p> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</div>
JavaScript:- <script>
let clickme = document.querySelectorAll('.button');
function removeActiveClass(elements,className){
elements && document.querySelectorAll(elements).forEach((e)=> e.classList.remove(className))
}
clickme.forEach(function(e){
e.addEventListener('click',function(){
let target = e.getAttribute('target_model');
if(target){
removeActiveClass('.modalbox','active')
let els = document.querySelector(target)
els.classList.add('active');
let close_model = els.querySelector('.closemodal')
close_model && close_model.addEventListener('click',function(){
els.classList.remove('active')
});
}
});
});
</script>
Comments
Post a Comment