Blur The Background When Opening A Modal Window

Blur The Background When Opening A Modal Window
File Size: 5.06 KB
Views Total:
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   

A minimal fast jQuery based modal window that uses CSS filters to blur the background (main content) when the modal is opened.

How to use it:

1. Create the HTML for the modal window.

<div class="modal">
  <div class="content">
    <div class="close"></div>
    <h1> Hello, World!</h1>
    <p> Modal Content Here </p>
  </div>
</div>

2. The necessary CSS/CSS3 styles for the modal window.

.modal {
  position: fixed;
  top: 0;
  left: 0;
  display: flex;
  width: 100%;
  height: 100vh;
  justify-content: center;
  align-items: center;
  opacity: 0;
  visibility: hidden;
}

.modal .content {
  position: relative;
  padding: 10px;
  width: 400px;
  height: 300px;
  border-radius: 8px;
  background-color: #fff;
  box-shadow: rgba(112, 128, 175, 0.2) 0px 16px 24px 0px;
  transform: scale(0);
  transition: transform 300ms cubic-bezier(0.57, 0.21, 0.69, 1.25);
}

.modal .close {
  position: absolute;
  top: 5px;
  right: 5px;
  width: 30px;
  height: 30px;
  cursor: pointer;
  border-radius: 8px;
  background-color: #7080af;
  clip-path: polygon(0 10%, 10% 0, 50% 40%, 89% 0, 100% 10%, 60% 50%, 100% 90%, 90% 100%, 50% 60%, 10% 100%, 0 89%, 40% 50%);
}

.modal.open {
  opacity: 1;
  visibility: visible;
}
.modal.open .content {
  transform: scale(1);
}

3. Apply the blur effect to any element you'd like to blur on modal open.

.container.blur {
  filter: blur(5px);
}

4. Inert the latest version of jQuery JavaScript library (slim build) at the end of the document.

<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha384-vk5WoKIaW/vJyUAd9n/wmopsmNhiy+L2Z+SBxGYnUkunIxVxAv/UtMOhba/xskxh" crossorigin="anonymous"></script>

5. Open the modal and blur the background by add the following CSS classes using jQuery.

$( '.modal' ).addClass( 'open' );

if ( $( '.modal' ).hasClass( 'open' ) ) {
  $( '.container' ).addClass( 'blur' );
} 

6. Enable the close button to dismiss the modal window.

$( '.close' ).click(function() {
  $( '.modal' ).removeClass( 'open' );
  $( '.cont' ).removeClass( 'blur' );
});

This awesome jQuery plugin is developed by Andrej Sharapov. For more Advanced Usages, please check the demo page or visit the official website.