How we can create an expanding modal overlay using HTML CSS JS? Solution: See this Morphing Modal Window With JavaScript and CSS, Fire Modal Overlay.
Previously I have shared some modal programs, but it is different from others it is an expanding modal overlay. Basically, A modal is a dialog box/popup window that is displayed on top of the current page. And Morphing involves automatically matching the basic look and feel of a website. And it is the combination of modal and morphing effect, it is a cool expanding modal overlay.
Today you will learn to create a Fire Modal Overlay. Basically, there are some text and a button to open the modal. When you will click on the button then the modal overlay will appear with a circular expanding effect. The modal window contains a bunch of text and a close button with a cross icon. When you will click on the close button the modal will disappear and the previous button will show again.
So, Today I am sharing Morphing Modal Window With JavaScript and CSS. There I have used a jQuery and a plugin for creating the program. And the layout and styles are based on HTML CSS. This is a cool modal overlay program that can be used on multiple sections like info, learn more, etc places. You can use this program on your website after some modifications.
If you are thinking now how this fire or expanding modal window actually is, then see the preview given below.
Preview Of Expanding or Fire Modal Overlay
See this video preview to getting an idea of how this expanding modal looks like.
Now you can see this program visually, also you can see it live by pressing the button given above. If you like this, then get the source code of its.
You May Also Like:
Morphing Modal Window Source Code
Before sharing source code, let’s talk about it. First I have created a section with a class named container and placed all items inside it. Inside the sections, I have placed a line of text and a button using Div and Hyperlink tags. Also, I have created another section inside the container section for overlay elements. This section contains a bunch of text and a close button.
Now using CSS I have placed all the items in the right place, as you can see in the preview. With CSS first I gave basic values like size, position, margin, padding, etc to all the elements. After that, I have used CSS transition and transform command to create animations. And used @media query to create a responsive design, the close button has an SVG icon.
jQuery handling here the open and close feature of the modal overlay. Also, it handling the circular expanding effect in every screen size. As you know there I have used a jQuery plugin named velocity.js and linked in the HTML file. There I have used if{} condition to declare the actions. Left all other things you will understand after getting the codes, I can’t explain all in writing.
For creating this program you have to create 3 files. First file for HTML, second for CSS, and the third file for JavaScript. Follow the steps to creating this program without any error.
index.html
Create an HTML file named ‘index.html‘ and put these codes given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
<!DOCTYPE html> <!--Code By Webdevtrick ( https://webdevtrick.com )--> <html lang="en" > <head> <meta charset="UTF-8"> <title>Morphing Modal Window | Webdevtrick.com</title> <meta name="viewport" content="width=device-width"> <script src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.3/modernizr.min.js" type="text/javascript"></script> <link href='https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,700' rel='stylesheet' type='text/css'> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css"> <link rel="stylesheet" href="style.css"> </head> <body> <section class="container"> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Non ipsum repudiandae sequi ut optio reiciendis consectetur, cum, a provident, iusto quod esse, perferendis iure iste! Quidem itaque, exercitationem aliquam, mollitia recusandae repellat dolores quibusdam minima quae reprehenderit ut, reiciendis officia. </p> <div class="cd-modal-action"> <a href="#" class="btn" data-type="modal-trigger">Fire Modal Window</a> <span class="cd-modal-bg"></span> </div> <div class="cd-modal"> <div class="cd-modal-content"> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ad modi repellendus, optio eveniet eligendi molestiae? Fugiat, temporibus! A rerum pariatur neque laborum earum, illum voluptatibus eum voluptatem fugiat, porro animi tempora? Sit harum nulla, nesciunt molestias, iusto aliquam aperiam est qui possimus reprehenderit ipsam ea aut assumenda inventore iste! Animi quaerat facere repudiandae earum quisquam accusamus tempora, delectus nesciunt, provident quae aliquam, voluptatum beatae quis similique in maiores repellat eligendi voluptas veniam optio illum vero! Eius, dignissimos esse eligendi veniam. </p> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ad modi repellendus, optio eveniet eligendi molestiae? Fugiat, temporibus! A rerum pariatur neque laborum earum, illum voluptatibus eum voluptatem fugiat, porro animi tempora? Sit harum nulla, nesciunt molestias, iusto aliquam aperiam est qui possimus reprehenderit ipsam ea aut assumenda inventore iste! Animi quaerat facere repudiandae earum quisquam accusamus tempora, delectus nesciunt, provident quae aliquam, voluptatum beatae quis similique in maiores repellat eligendi voluptas veniam optio illum vero! Eius, dignissimos esse eligendi veniam. </p> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ad modi repellendus, optio eveniet eligendi molestiae? Fugiat, temporibus! A rerum pariatur neque laborum earum, illum voluptatibus eum voluptatem fugiat, porro animi tempora? Sit harum nulla, nesciunt molestias, iusto aliquam aperiam est qui possimus reprehenderit ipsam ea aut assumenda inventore iste! Animi quaerat facere repudiandae earum quisquam accusamus tempora, delectus nesciunt, provident quae aliquam, voluptatum beatae quis similique in maiores repellat eligendi voluptas veniam optio illum vero! Eius, dignissimos esse eligendi veniam. </p> </div> </div> <a href="#" class="cd-modal-close">Close</a> </section> <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> <script src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/148866/velocity.min.js'></script> <script src="function.js"></script> </body> </html> |
style.css
Now create a CSS file named ‘style.css‘ and put these codes given here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
/* Code By Webdevtrick ( https://webdevtrick.com ) */ *, *::after, *::before { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; } html { font-size: 62.5%; } body { font-size: 1.6rem; font-family: "Source Sans Pro", sans-serif; color: #34383c; background-color: #ffffff; } body.overflow-hidden { overflow: hidden; } a { color: #ff4747; text-decoration: none; } .container { padding: 2em 5%; text-align: center; background-color: #ff4747; } .container p { margin: 2em 0; line-height: 1.6; color: #ffffff; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; } @media only screen and (min-width: 768px) { .container { padding: 4em 10%; } .container p { font-size: 1.8rem; line-height: 2; } } @media only screen and (min-width: 1170px) { .container { padding: 4em 20%; } } .cd-modal { position: fixed; top: 0; left: 0; width: 100%; height: 100%; visibility: hidden; opacity: 0; pointer-events: none; -webkit-transition: visibility 0s 0.3s, opacity 0.3s 0s, z-index 0s 0.3s; -moz-transition: visibility 0s 0.3s, opacity 0.3s 0s, z-index 0s 0.3s; transition: visibility 0s 0.3s, opacity 0.3s 0s, z-index 0s 0.3s; } .cd-modal::after { /* gradient overlay at bottom of modal window */ content: ''; position: absolute; left: 0; bottom: 0; width: 100%; height: 60px; pointer-events: none; background: transparent; background: -webkit-linear-gradient( bottom , #34383c, rgba(52, 56, 60, 0)); background: linear-gradient(to top, #34383c, rgba(52, 56, 60, 0)); } .cd-modal .cd-modal-content { height: 100%; width: 100%; padding: 3em 5%; text-align: left; overflow-y: auto; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; } .cd-modal p { color: #ffffff; line-height: 1.6; margin: 2em 0; } .modal-is-visible .cd-modal { z-index: 1; visibility: visible; opacity: 1; pointer-events: auto; -webkit-transition: visibility 0s 0s, opacity 0.3s 0s, z-index 0s 0s; -moz-transition: visibility 0s 0s, opacity 0.3s 0s, z-index 0s 0s; transition: visibility 0s 0s, opacity 0.3s 0s, z-index 0s 0s; } .modal-is-visible .cd-modal .cd-modal-content { -webkit-overflow-scrolling: touch; } @media only screen and (min-width: 768px) { .cd-modal .cd-modal-content { padding: 4em 10%; } } @media only screen and (min-width: 1170px) { .cd-modal .cd-modal-content { padding: 6em 20%; } .cd-modal p { font-size: 2rem; line-height: 2; } } .cd-modal-action { position: relative; } .cd-modal-action .btn, .cd-modal-action .cd-modal-bg { display: inline-block; height: 4em; background-color: #34383c; } .cd-modal-action .btn { width: 12.5em; border-radius: 5em; color: #ffffff; line-height: 4em; white-space: nowrap; font-weight: 700; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; -webkit-transition: color 0.2s 0.3s, width 0.3s 0s; -moz-transition: color 0.2s 0.3s, width 0.3s 0s; transition: color 0.2s 0.3s, width 0.3s 0s; } .cd-modal-action .btn.to-circle { width: 4em; color: transparent; -webkit-transition: color 0.2s 0s, width 0.3s 0.2s; -moz-transition: color 0.2s 0s, width 0.3s 0.2s; transition: color 0.2s 0s, width 0.3s 0.2s; } .cd-modal-action .cd-modal-bg { position: absolute; z-index: 1; left: 50%; top: 0; width: 4em; border-radius: 50%; opacity: 0; visibility: hidden; -webkit-transform: translateZ(0); -moz-transform: translateZ(0); -ms-transform: translateZ(0); -o-transform: translateZ(0); transform: translateZ(0); -webkit-transform: translateX(-2em); -moz-transform: translateX(-2em); -ms-transform: translateX(-2em); -o-transform: translateX(-2em); transform: translateX(-2em); -webkit-transition: visibility 0s 0.5s; -moz-transition: visibility 0s 0.5s; transition: visibility 0s 0.5s; } .cd-modal-action .cd-modal-bg.is-visible { opacity: 1; visibility: visible; } .cd-modal-close { position: fixed; z-index: 1; top: 20px; right: 5%; height: 50px; width: 50px; border-radius: 50%; background: rgba(0, 0, 0, 0.3) url(https://webdevtrick.com/wp-content/uploads/close-icon.svg) no-repeat center center; /* image replacement */ overflow: hidden; text-indent: 100%; white-space: nowrap; visibility: hidden; opacity: 0; -webkit-transform: translateZ(0); -moz-transform: translateZ(0); -ms-transform: translateZ(0); -o-transform: translateZ(0); transform: translateZ(0); -webkit-transform: scale(0); -moz-transform: scale(0); -ms-transform: scale(0); -o-transform: scale(0); transform: scale(0); -webkit-transition: -webkit-transform 0.3s 0s, visibility 0s 0.3s, opacity 0.3s 0s; -moz-transition: -moz-transform 0.3s 0s, visibility 0s 0.3s, opacity 0.3s 0s; transition: transform 0.3s 0s, visibility 0s 0.3s, opacity 0.3s 0s; } .no-touch .cd-modal-close:hover { background-color: rgba(0, 0, 0, 0.5); } .modal-is-visible .cd-modal-close { visibility: visible; opacity: 1; -webkit-transition: -webkit-transform 0.3s 0s, visibility 0s 0s, opacity 0.3s 0s; -moz-transition: -moz-transform 0.3s 0s, visibility 0s 0s, opacity 0.3s 0s; transition: transform 0.3s 0s, visibility 0s 0s, opacity 0.3s 0s; -webkit-transform: scale(1); -moz-transform: scale(1); -ms-transform: scale(1); -o-transform: scale(1); transform: scale(1); } @media only screen and (min-width: 768px) { .cd-modal-close { top: 70px; } } |
function.js
The final step, create a JavaScript file named ‘function.js‘ and put the codes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
// Code By Webdevtrick ( https://webdevtrick.com ) jQuery(document).ready(function($){ //trigger the animation - open modal window $('[data-type="modal-trigger"]').on('click', function(){ var actionBtn = $(this), scaleValue = retrieveScale(actionBtn.next('.cd-modal-bg')); actionBtn.addClass('to-circle'); actionBtn.next('.cd-modal-bg').addClass('is-visible').one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend', function(){ animateLayer(actionBtn.next('.cd-modal-bg'), scaleValue, true); }); //if browser doesn't support transitions... if(actionBtn.parents('.no-csstransitions').length > 0 ) animateLayer(actionBtn.next('.cd-modal-bg'), scaleValue, true); }); //trigger the animation - close modal window $('.container .cd-modal-close').on('click', function(){ closeModal(); }); $(document).keyup(function(event){ if(event.which=='27') closeModal(); }); $(window).on('resize', function(){ //on window resize - update cover layer dimention and position if($('.container.modal-is-visible').length > 0) window.requestAnimationFrame(updateLayer); }); function retrieveScale(btn) { var btnRadius = btn.width()/2, left = btn.offset().left + btnRadius, top = btn.offset().top + btnRadius - $(window).scrollTop(), scale = scaleValue(top, left, btnRadius, $(window).height(), $(window).width()); btn.css('position', 'fixed').velocity({ top: top - btnRadius, left: left - btnRadius, translateX: 0, }, 0); return scale; } function scaleValue( topValue, leftValue, radiusValue, windowW, windowH) { var maxDistHor = ( leftValue > windowW/2) ? leftValue : (windowW - leftValue), maxDistVert = ( topValue > windowH/2) ? topValue : (windowH - topValue); return Math.ceil(Math.sqrt( Math.pow(maxDistHor, 2) + Math.pow(maxDistVert, 2) )/radiusValue); } function animateLayer(layer, scaleVal, bool) { layer.velocity({ scale: scaleVal }, 400, function(){ $('body').toggleClass('overflow-hidden', bool); (bool) ? layer.parents('.container').addClass('modal-is-visible').end().off('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend') : layer.removeClass('is-visible').removeAttr( 'style' ).siblings('[data-type="modal-trigger"]').removeClass('to-circle'); }); } function updateLayer() { var layer = $('.container.modal-is-visible').find('.cd-modal-bg'), layerRadius = layer.width()/2, layerTop = layer.siblings('.btn').offset().top + layerRadius - $(window).scrollTop(), layerLeft = layer.siblings('.btn').offset().left + layerRadius, scale = scaleValue(layerTop, layerLeft, layerRadius, $(window).height(), $(window).width()); layer.velocity({ top: layerTop - layerRadius, left: layerLeft - layerRadius, scale: scale, }, 0); } function closeModal() { var section = $('.container.modal-is-visible'); section.removeClass('modal-is-visible').one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend', function(){ animateLayer(section.find('.cd-modal-bg'), 1, false); }); //if browser doesn't support transitions... if(section.parents('.no-csstransitions').length > 0 ) animateLayer(section.find('.cd-modal-bg'), 1, false); } }); |
That’s It. Now you have successfully created Morphing Modal Window With JavaScript and CSS, Expanding or Fire Modal Overlay. If you have any doubt or question comment down below.
Thanks For Visiting, Keep Visiting.
OMG, is not javascript & css, but also modernizr, jquery and velocity: at least 3 scripts to opena a modal!?!?!?