Open Pop-up"> Open Pop-up" />
Easy Tutorial
❮ Div Scroll Android App Develop Learning ❯

JavaScript Pop-up

Category Programming Techniques

Here is a simple example of JavaScript pop-up code.

HTML Code:

<!-- Open the pop-up button -->
<button id="myBtn">Open Pop-up</button>

<!-- Pop-up -->
<div id="myModal" class="modal">

  <!-- Pop-up content -->
  <div class="modal-content">
    <span class="close">&times;</span>
    <p>Text in the pop-up...</p>
  </div>

</div>

CSS Code:

/* The pop-up (background) */
.modal {
    display: none; /* Hidden by default */
    position: fixed; /* Fixed position */
    z-index: 1; /* Place on top */
    left: 0;
    top: 0;
    width: 100%; 
    height: 100%;
    overflow: auto; 
    background-color: rgb(0,0,0); 
    background-color: rgba(0,0,0,0.4); 
}

/* Pop-up content */
.modal-content {
    background-color: #fefefe;
    margin: 15% auto; 
    padding: 20px;
    border: 1px solid #888;
    width: 80%; 
}

/* The close button */
.close {
    color: #aaa;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.close:hover,
.close:focus {
    color: black;
    text-decoration: none;
    cursor: pointer;
}

JavaScript Code:

// Get the modal
var modal = document.getElementById('myModal');

// Get the button that opens the modal
var btn = document.getElementById("myBtn");

// Get the <span> element that closes the modal
var span = document.querySelector('.close');

// When the user clicks the button, open the modal 
btn.onclick = function() {
    modal.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
    modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}

Live Demo


Pop-up with Header and Footer

HTML Code:

<!-- Open the pop-up button -->
<button id="myBtn">Open Pop-up</button>

<!-- Pop-up -->
<div id="myModal" class="modal">

  <!-- Pop-up content -->
    <div class="modal-content">
      <div class="modal-header">
        <span class="close">&times;</span>
        <h2>Pop-up Header</h2>
      </div>
      <div class="modal-body">
        <p>Text in the pop-up...</p>
        <p>Text in the pop-up...</p>
      </div>
      <div class="modal-footer">
        <h3>Pop-up Footer</h3>
      </div>
    </div>

</div>

CSS Code:

``` /* The pop-up (background) / .modal { display: none; / Hidden by default */ position: fixed; z-index: 1; padding-top: 100px; left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: rgb(0,0,0); background-color: rgba(0,0,0,0.4); }

/* Pop-up content */ .modal-content { position: relative; background-color: #fefefe; margin: auto; padding: 0; border: 1px solid #888; width: 80%; box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19); -webkit-animation-name: animatetop; -webkit-animation-duration: 0.4s; animation-name: animatetop; animation-duration: 0.4s }

/* Add Animation */ @-webkit-keyframes animatetop { from {top:-300px; opacity:0} to {top:0; opacity:1} }

@keyframes animatetop { from {top:-300px; opacity:0} to {top:0; opacity:1} }

/* The close button */ .close { color:

❮ Div Scroll Android App Develop Learning ❯