Jquery popup window with dimming. Create a jQuery popup modal window. jQuery modal windows

💖 Do you like it? Share the link with your friends

While talking about various website building techniques, it would be ridiculous not to talk about some ways to create modal windows. We will not discuss the purpose, usefulness and emerging problems of using pop-up and modal windows. Let's look at just one of the many examples of creating such windows.
There are situations when it is not possible to use special plugins, such as and, so it’s worth understanding how you can create your own.

Let's see how to do this:

HTML

Let's start by adding tags with the following attributes:

  • href - #?w=500 indicates the width of the window
  • rel – unique attribute for each window
  • class="poplight" – class for displaying a pop-up window
< a href= "#?w=500" rel= "popup_name" class = "poplight" >See Window in Action - Width = 500px

See Window in Action - Width = 500px

Next we need to create inline markup for the popup. You can place it anywhere on the page, for example at the bottom of the content. Note that the popup ID matches the rel attribute of the tag
This will link the link and popup together.

Heading

Any text you want

And so, we have figured out the placement of our window on the page, now let's decorate it using styles, give it, so to speak, a decent look.

CSS Layout

For greater clarity, I have written down some explanations for the style parameters of our window. Since popup windows can have different sizes, we do not specify the edges of the window in the CSS popup_block; calculating the required size is exactly the task for .

#fade ( display : none ; /* --default hidden--*/ background : rgba ( 7 , 87 , 207 , 0.8 ) ; position: fixed; left: 0; top: 0; width: 100%; height: 100%; opacity: .80; z-index: 9999; ) .popup_block ( display : none ; /*--hidden by default--*/ background : #fff ; padding : 20px ; border : 8px solid rgb (134 , 134 , 134 ) ; float : left ; font-size : 85 %; position: fixed; top: 50%; left: 50%; color: #000; max-width: 750px; min-width: 320px; height: auto; z-index: 99999; /*--CSS3 block shadow --*/ -webkit-box-shadow: 0px 0px 20px #000 ; -moz-box-shadow: 0px 0px 20px #000 ; box-shadow : 0px 0px 20px #000 ; /*--CSS3 corner rounding--* / -webkit-border-radius: 12px ; -moz-border-radius: 12px ; border-radius : 12px ; ) .popup_block p ( font-weight : 400 ; padding : 0 ; margin : 0 ; color : #000 ; line -height : 1.6 ; .popup_block h2 ( margin : 0px 0 10px ; color : rgb (43 , 43 , 43 ) ; font-weight : 400 ; text-align : center ; text-shadow : 1px 1px 2px #0D0C0C ; ) /* form a close button */ .close ( background-color : rgba ( 61 , 61 , 61 , 0.8 ) ; border : 2px solid #ccc ; height: 25px; line-height: 20px; position: absolute; right: -17px; font-weight: bold; text-align: center; text-decoration: none; padding: 0; top: -17px; width: 25px; -webkit-border-radius: 50% ; -moz-border-radius: 50% ; -ms-border-radius: 50% ; -o-border-radius: 50% ; border-radius : 50% ; -moz-box-shadow: 1px 1px 3px #000 ; -webkit-box-shadow: 1px 1px 3px #000 ; box-shadow : 1px 1px 3px #000 ; ) .close : before ( color : rgba (255 , 255 , 255 , 0.9 ) ; content : "X" ; font-size : 12px ; text-shadow : 0 -1px rgba (0 , 0 , 0 , 0.9 ) ; ) .close : hover ( background-color : rgba ( 252 , 20 , 0 , 0.8 ) ; ) .shadow ( box-shadow : 4px 4px 10px #857373 ; -webkit-box-shadow: 4px 4px 10px #857373 ; -moz- box-shadow: 4px 4px 10px #857373; padding: 0 ; ) /*--fixed positioning for IE6--*/ * html #fade ( position : absolute ; ) * html .popup_block ( position : absolute ; )

#fade ( display: none;/*--hidden by default--*/ background: rgba(7, 87, 207, 0.8); position: fixed; left: 0; top: 0; width: 100%; height: 100%; opacity: .80; z-index: 9999; ) .popup_block ( display: none; /*--hidden by default--*/ background: #fff; padding: 20px; border: 8px solid rgb(134, 134, 134); float: left; font-size: 85%; position: fixed; top: 50%; left: 50%; color: #000; max-width: 750px; min-width: 320px; height: auto ; z-index: 99999; /*--CSS3 box shadow--*/ -webkit-box-shadow: 0px 0px 20px #000; -moz-box-shadow: 0px 0px 20px #000; box-shadow: 0px 0px 20px #000; /*--CSS3 corner rounding--*/ -webkit-border-radius: 12px; -moz-border-radius: 12px; border-radius: 12px; ) .popup_block p ( font-weight: 400; padding: 0; margin: 0; color: #000; line-height: 1.6;).popup_block h2 ( margin: 0px 0 10px; color: rgb(43, 43, 43); font-weight: 400; text-align : center; text-shadow: 1px 1px 2px #0D0C0C; ) /* form a close button */ .close ( background-color: rgba(61, 61, 61, 0.8); border: 2px solid #ccc; height: 25px; line-height: 20px; position: absolute; right: -17px; font-weight: bold; text-align: center; text-decoration: none;padding: 0; top: -17px; width: 25px; -webkit-border-radius: 50%; -moz-border-radius: 50%; -ms-border-radius: 50%; -o-border-radius: 50%; border-radius: 50%; -moz-box-shadow: 1px 1px 3px #000; -webkit-box-shadow: 1px 1px 3px #000; box-shadow: 1px 1px 3px #000; ) .close:before ( color: rgba(255, 255, 255, 0.9); content: "X"; font-size: 12px; text-shadow: 0 -1px rgba(0, 0, 0, 0.9); ) .close:hover ( background-color: rgba(252, 20, 0, 0.8); ) .shadow ( box-shadow:4px 4px 10px #857373; -webkit-box-shadow:4px 4px 10px #857373; -moz- box-shadow:4px 4px 10px #857373; padding:0; ) /*--fixed positioning for IE6--*/ *html #fade ( position: absolute; ) *html .popup_block ( position: absolute; )

With the formation of the window and its appearance With using css, I think there won’t be any special difficulties. You can register styles directly on HTML page, between the and tags, or you can put it in a separate file of your styles, usually this is a style.css file, or something like that.

JQuery setup

For the modal window to work fully, you need to connect jQuery; those who are not familiar with the work of this library can read.

Well, we'll move on. You can do the most latest version JQuery from the library website, or use a separate file located in the depths of Google, connecting it to the document, in the section before the closing tag, placing the following line:

In the next step, we will look at the filling and functions of the jquery plugin to activate our pop-up window, the code contains some explanations for a better understanding of what we are doing.

JQuery plugin
$(document) . ready(function () ( //When clicking on a link with poplight class and href tag attribute with # $("a.poplight" ) . click(function () ( var popID = $(this) . attr("rel" ) ; //get the name of the window, it is important not to forget when adding new ones to change the name in the rel attribute of the link var popURL = $(this) . attr(" href" ) ; //get the size from the href attribute of the link //query and variables from href url var query= popURL. split ("?" ) ; var dim= query[ 1 ] . split ("&" ) ; var popWidth = dim[ 0 ] . split ("=" ) [ 1 ] ; //first value of the query string //Add a close button to the window $("#" + popID) . fadeIn() . css(( "width" : Number( popWidth ) ) ). ("#" + popID) . height() + 80 ) / 2 ; var popMargLeft = ($("#" + popID) . width() + 80 ) / 2 ; //Set the indentation value $("#" + popID) .css(( "margin-top" : - popMargTop, "margin-left" : - popMargLeft ) ) ; //Add a translucent darkening background $("body" ) . append("" ) ; //div container will be placed before the tag. $("#fade" ) . css(( "filter" : "alpha(opacity=80)" ) ) . fadeIn() ; //layer translucency, filter for stupid IE return false ; ) ) ; //Close the window and dim the background $(document) . on("click" , "a.close, #fade" , function () ( //closing by click outside the window, i.e. on the background... $("#fade , .popup_block" ) . fadeOut(function () ( $("#fade, a.close" ) . remove() ; //fades smoothly ) ) ; return false ; ) ) ; ) ) ;

$(document).ready(function())( //When clicking on a link with poplight class and href tag attribute with # $("a.poplight").click(function() ( var popID = $(this).attr("rel"); //get the name of the window, it is important not to forget to change the name in the rel attribute of the link when adding new ones var popURL = $(this).attr("href"); //get the size from the href attribute of the link //query and variables from the href url var query= popURL.split("?"); var dim= query.split( "&"); var popWidth = dim.split("="); //first value of the query string //Add a close button to the window $("#" + popID).fadeIn().css(( "width": Number(popWidth) )).prepend(""); //Determine the margin (margin) for center alignment (vertical and horizontal) - we add 80 to the height / width, taking into account padding + frame width defined in css var popMargTop = ($("#" + popID).height() + 80) / 2; var popMargLeft = ($("#" + popID).width() + 80) / 2; //Set the indentation value $("# " + popID).css(( "margin-top" : -popMargTop, "margin-left" : -popMargLeft )); //Add a translucent darkening background $("body").append(""); //div the container will be listed before the tag. $("#fade").css(("filter" : "alpha(opacity=80)")).fadeIn(); //layer translucency, filter for stupid IE return false; )); //Close the window and fade background $(document).on("click", "a.close, #fade", function() ( //close by clicking outside the window, i.e. on the background... $( "#fade , .popup_block").fadeOut(function() ( $("#fade, a.close").remove(); //fade out )); return false; )); ));

Conclusion:

In general, if you don’t go into the weeds and don’t burden yourself with unnecessary code gobbledygook, our wonderful modal window is ready to work, and is waiting for your thoughts translated into text, or any other useful information.
For those who would like to use a modal window to place videos or large-scale images in it, I still advised using special plugins like , since the above example of a modal window is intended more for light and not very heavy information, although this is not a problem if desired .

Next time I will definitely tell you and show you with an example, and for sure, many will be interested in learning about other third-party objects in the pop-up window. So don't get lost in the web and stay tuned!

Update: Version dm-modal.js v1.3 (01/15/2017)
Fixed: Replaced the obsolete .live() function, using href*=\\# syntax. The plugin now works with current versions of the jQuery library

That's all! I hope this was another useful lesson.

With all respect, Andrew

1. Modal window on jQuery “Simple Modal Box” 2. jQuery plugin “LeanModal”

Displaying content in modal windows. To see the plugin in action on the demo page, click on the link: Sign Up Form or Basic Content.

3. jQuery plugin “ToastMessage”

Pop-up messages. The plugin comes in two versions. In one case, messages disappear on their own after a certain time, in the second implementation, in order to close a message, you need to click on a button.

4. Content that pops up in a modal window

Plugin "Reveal". To see the plugin in action, click on the “Fire A Reveal Modal” button on the demo page.

5. Cute Dialog Boxes

Click on the cross on the demo page to see the plugin in action.

6. Mootools modal window, “MooDialog” plugin 7. jQuery popup panel on top of the screen 8. jQuery popup window

jQuery plugin for displaying a feedback form in a pop-up window.

10. MooTools plugin “LightFace” for implementing Facebook dialog boxes

Facebook style dialog boxes. In addition to static information, you can place images, frames, and Ajax requests in windows. Lots of settings for how the plugin works, a very powerful tool. Looks very stylish and functional. Follow the links on the demo page to see examples with different content.

11. jQuery modal window

A neat popup dialog in jQuery.

12. jQuery modal windows

Cute pop-up modal windows. Three styles. The demo page contains 3 links to open windows.

13. jQuery modal windows

Pop-up modal windows of several types. To see the plugin in action, click on the link on the demo page.

15. Message that pops up on top of the page

The message is displayed on top of the page, which in turn is dimmed. Click on "Click me" on the demo page to see a pop-up message. Clicking on the cross will close it. Implemented using jQuery.

16. Modal window “ModalBox” in javascript

Implement modern modal dialogs without using pop-ups and page reloads. On the demo page, click on the “Start Demo” button to see the script in action.

17. “Leightbox” plugin using the Prototype library

Plugin for displaying content in modal windows.

Quite often you can find modal windows on websites, and they are all used for different tasks. In fact, this is quite a powerful tool that allows you to make the site interface more interactive and convenient. For example, modal windows can be used for various forms, such as an authorization form, a feedback form, placing an order for a product, and you never know.

In this post we will look at an example of how to make a simple modal window using JQuery and CSS. The peculiarity of this example is that it is not required, well, with the exception of the JQuery library itself.

Place the modal window code on the page:

close Open modal window

As you can see from the markup, the block of the modal window itself is a div with the id= attribute modal_form which contains a span element with id= modal_close. This element will serve as a button to close the modal window; in addition, below the block there is a div block with the id= attribute overlay, which also serves to darken the background. The modal window will be opened by link, with the class modal.

CSS for modal window

#modal_form ( width: 300px; height: 300px; border-radius: 5px; border: 3px #000 solid; background: #fff; position: fixed; top: 45%; left: 50%; margin-top: -150px; margin-left: -150px; display: none; opacity: 0; z-index: 5; padding: 20px 10px; ) #modal_form #modal_close ( width: 21px; height: 21px; position: absolute; top: 10px; right: 10px; cursor: pointer; display: block; ) #overlay ( z-index:3; position:fixed; background-color:#000; opacity:0.8; -moz-opacity:0.8; filter:alpha(opacity=80) ; width:100%; height:100%; top:0; left:0; cursor:pointer; display:none; )

For modal_form we set a fixed width and height and then centered the position to the center of the screen. For the modal window background ( overlay) we set the size to fit the width of the screen, fill with transparency, and also hide it by default. Special moment with z-index, the modal should have the largest of all elements on the page, and the cover should have the largest of all elements except the modal itself.

Now to the most basic thing, this is the javascript code. Two main events will be used for the modal window: its opening - clicking on an element with the class modal, in our case this is a link, and closing the modal window is a click on the cover ( overlay), or click on the close button, in our case this is a span element with id= modal_close.

$(document).ready(function() ( $(".modal").click(function(event)( event.preventDefault(); $("#overlay").fadeIn(400, // animate the display of the cover function ()( // next show the mod window $("#modal_form") .css("display", "block") .animate((opacity: 1, top: "50%"), 200); )); )); // closing the modal window $("#modal_close, #overlay").click(function())( $("#modal_form") .animate((opacity: 0, top: "45%"), 200, // reduce the transparency function())( // after the animation $(this).css("display", "none"); // hide the window $("#overlay").fadeOut(400); // hide the background ) ); )); ));

Using animate we change the vertical position top, as well as transparency opacity, and with this we get an interesting effect. A similar effect is used both when opening a window and when closing it. The difference is that the order in which the properties are applied to the blocks changes, thereby visualizing the opening and closing of the window.


3. Example of a jQuery modal window called from a link (from Demo)

Most likely, you have already seen a pop-up message on the Internet more than once. modal window- registration confirmation, warning, reference Information, file download and much more. In this tutorial I will offer several examples on how to create the simplest modal windows.

Creating a simple pop-up modal window Let's start looking at the code for a simple modal window that will immediately appear
jQuery code


$(document).ready(function()
{
alert("Text in pop-up window");
});

Paste the code anywhere in the body of your page. Immediately after the page loads, without any commands, you will see a window that looks like this:


But the following code will be executed after the entire page is loaded into the browser. In our example, after loading the page with images, a simple pop-up window will pop up:


$(window).load(function()
{
alert("Page has completed loading!)");
});

Calling a jQuery modal window from a link with CSS The next step is to create a modal window when the link is clicked. The background will slowly darken.


You can often see that the login and registration forms are located in such windows. Let's get down to business

First, let's write the html part. We place this code in the body of your document.

Calling a modal window



Modal window text
Close
Text in a modal window.


CSS code. Either in a separate css file, or in the head.


body(
font-family:verdana;
font-size:15px;
}
.link (color:#fff; text-decoration:none)
.link:hover (color:#fff; text-decoration:underline)
#mask (
position:absolute;
left:0;
top:0;
z-index:9000;
background-color:#000;
display:none;
}
#boxes.window (
position:absolute;
left:0;
top:0px;
-top: 40px;
width:440px;
height:200px;
display:none;
z-index:9999;
padding: 20px;
overflow: hidden;
}
#boxes #dialog (
width:375px;
height:203px;
padding:10px;
background-color:#ffffff;
}
.top(
position:absolute;
left:0;
top:0;
width:370px;
height:30px;
background: #0085cc;
padding: 8px 20px 6px 10px;
}
.close(
float:right;
}
.content(
padding-top: 35px;
}

In the jQuery code, we will focus on the position of the modal window and the mask, in our case the gradual darkening of the background.

Attention! Don't forget to include the library in the head of the document!


Connecting the library from the Google website. Well, the jQuery code itself.

jQuery code


$(document).ready(function() (
$("a").click(function(e) (
e.preventDefault();
var id = $(this).attr("href");
var maskHeight = $(document).height();
var maskWidth = $(window).width();
$("#mask").css(("width":maskWidth,"height":maskHeight));
$("#mask").fadeIn(1000);
$("#mask").fadeTo("slow",0.8);
var winH = $(window).height();
var winW = $(window).width();
$(id).css("top", winH/2-$(id).height()/2);
$(id).css("left", winW/2-$(id).width()/2);
$(id).fadeIn(2000);
});
$(".window .close").click(function (e) (
e.preventDefault();
$("#mask, .window").hide();
});
$("#mask").click(function () (
$(this).hide();
$(".window").hide();
});
});

Modal windows are an integral part of modern web design; with their help, the developer can resort to the method of looping on one page and not redirecting the visitor to secondary pages. In this tutorial we'll look at how to create awesome modal windows with blurred backgrounds for your website using jQuery and CSS3. Thanks to these rules, we will create a blurred background when the window appears, which will tie the visitor’s eye only to the necessary information on the site.

Modal windows with blurred background in CSS3

The best economic news is only here: Drobakha

The window animation will be set in such a way that when you click on the button to appear, the window will animate from top to bottom, and the background blur will automatically increase.

Step 1. HTML

We will have a container that will contain: title, description, then we add a class for the button with the toggleModal class to open the modal window:

Heading

Description

Open article Title for window

Tabs are a very interesting and convenient thing when creating a website; they allow you to properly organize information, while saving some space on the site.

Close

Then we need to add the modal is-active class, this class will be responsible for calling the modal window, modal__header is responsible for the title and its styling of the window.

Step 2: CSS

Now let's move on to styling, the first step will be the button class, which will be responsible, as you guessed, for the buttons on the site, we set the correct display parameters for it:

Button ( background: none; background-clip: padding-box; display: inline-block; border: 0; user-select: none; -webkit-touch-callout: none; -webkit-appearance: button; -webkit-user -select: none; -moz-user-select: none; -ms-user-select: none; )

Container ( position: relative; margin:0 auto; max-width: 960px; box-sizing: border-box; padding-top: 40px; )

article ( background: #fff; padding: 20px; margin-bottom: 40px; border-radius: 5px; ) .modal ( display: none; position: fixed; top: 50%; width: 100%; height: auto; margin -top: -150px; background-color: $color-white; border-radius: 3px; z-index: 999; box-shadow: 0px 1px 3px 0px darken($color-bg, 10%); @media #( $small) ( left: 50%; margin-left: -260px; max-width: 520px; ) &.is-active ( display: block; animation: 1s linear slide; ) .inner ( position: relative; padding: 20px ; ) .modal__header ( border-bottom: 1px solid darken($color-bg, 5%); ) .modal__footer ( text-align: center; button ( display: inline-block; ) )

The styles are quite simple, they are stored in a separate file and should not cause difficulties when editing them for a developer who has at least once encountered CSS.

Step 3. JS

Our last, but no less important, will be to set the automatic background blur when the menu appears, as well as the clickability of links, small JS rules will help us with this:

$("body").addClass("is-blurred"); $(".toggleModal").on("click", function (event) ( event.preventDefault(); $(".modal").toggleClass("is-active"); $("body").toggleClass ("is-blurred"); ));

The result is wonderful modal windows that are pleasing to the viewer's eye and don't clutter up your design.



tell friends