banner



How To Create Popup Using Jquery

  • Updated date Apr 30, 2020
  • 55k
  • 0

This is the article explains the custom PopUp window in jQuery, without any plug-ins. Most of the time we willing to open a <div> on a click event.

Introduction

This article explains the use of a custom PopUp window in jQuery, without any plug-ins. Most of the time we are willing to open a <div> on a click event. This article explains how to make a PopUp window and how to use it. For this (PopUp ) window we need to understand some jQuery functions and CSS. The output will something like this:

output2

List of all jQuery functions for the PopUp window.

  • click
  • fadeIn/fadeOut
  • preventDefault
  • hide

click

This is the JavaScript event that can trigger on an element. For the use point of view, we use this function on any id, with "dot" and then function name. This function doesn't accept any arguments. I have used this function on a button. Example of click function:

  1. $( "#div" ).click( function  () {
  2.     alert("click " );
  3. });

fadeIn/fadeOut

The .fadeIn()/.fadeOut() methods are used to animate the opacity of the matched elements. For this (PopUp window) I have shown animation with some transaction.

Syntax- . fadeIn([duration][,easing][,complete])

.fadeOut([duration][,easing][,complete])

  • duration: A number or string value that will determine how long the animation will run.
  • easing:  A string indicating which easing function to use for the transaction
  • complete: This is a function type and it calls the function when the animation is complete.

preventDefault

This method helps to prevent all the actions from being revoked, if this method is called, the default action will not be triggered. I have used this method to rollback the animation when we open the PopUp window.

hide

This method helps to hide immediately, with no animation. And make the display none of that element. I have used this method to close the window.

JavaScript code and explanation

  1. $(document).ready( function  () {
  2.     $('#myButton' ).click( function  () {
  3. var  id = '#dialog' ;
  4. var  clgName = $( '#cllg' ).val();
  5.         $("#clg" ).val(clgName);
  6. var  maskHeight = $(document).height();
  7. var  maskWidth = $(document).width();
  8.         $('#mask' ).css({ 'width' : maskWidth, 'height' : maskHeight })
  9.         $('#mask' ).fadeIn(1000);
  10.         $('#mask' ).fadeTo( "slow" , 0.8);
  11. var  winH = $(window).height();
  12. var  winW = $(window).width();
  13.         $(id).css('top' , winH / 2 - $(id).height() / 2);
  14.         $(id).css('left' , winW / 2 - $(id).width() / 2);
  15.         $(id).fadeIn(2000);
  16.         $('.window .close' ).click( function  (e) {
  17.             e.preventDefault();
  18.             $('#mask' ).hide();
  19.             $('.window' ).hide();
  20.         });
  21.     });
  22. });

This JavaScript code works in the following 3 steps:

  1. Getting the size of the window for the fade effect
  2. Set the pop-up window
  3. Close the pop-up window

CSS Code

  1. #mask  {
  2. position : absolute ;
  3. padding-left : 60px ;
  4. padding-top : 80px ;
  5. padding-bottom : 80px ;
  6. padding-right : 50px ;
  7.            left:0 ;
  8.            top:0 ;
  9. z-index : 9999 ;
  10. background-color : #808080 ;
  11. display : none ;
  12.        }
  13. #boxes  .window {
  14. position : absolute ;
  15.            left:0 ;
  16.            top:0 ;
  17. width : 580px ;
  18. height : 300px ;
  19. display : none ;
  20. z-index : 9999 ;
  21. padding : 20px ;
  22. background-color : white ;
  23. border : 3px solid #79BBFD ;
  24.            border-radius:10px ;
  25.            -webkit-border-radius:10px ;
  26.            -moz-border-radius:10px ;
  27.        }
  28. #boxes #dialog  {
  29. padding : 10px ;
  30. width : 580px ;
  31. height : 300px ;
  32. background-color : #ffffff ;
  33. background-repeat : no-repeat ;
  34. margin-top : 20px ;
  35.        }
  36. #header Border{
  37. height : 25px ;
  38. width : 100% ;
  39. background-color : #0026ff ;
  40. color : white ;
  41. font-size : 18px ;
  42. padding-top : 3px ;
  43. margin-bottom : 20px ;
  44.        }
  45. #close {
  46. position : relative ;
  47. float : right ;
  48. text-decoration : none ;
  49. padding-right : 5px ;
  50. cursor : pointer ;
  51.        }

In this CSS code, we are able to design a perfect dialog box and a background color.

HTML Code

output1

When we enter the name of the college and hit the button, we will see that college name on the PopUp window.

output2

We can close this window either by the cross [X] button or by closing the window.

Summary

In this article, we are learned a custom pop-up window using JavaScript and CSS. In this article, we will also learn how to get and set the text value from the text box using jQuery. Thanks for reading this article.

How To Create Popup Using Jquery

Source: https://www.c-sharpcorner.com/UploadFile/736ca4/custom-popup-window-using-jquery/

Posted by: herveyonves1962.blogspot.com

0 Response to "How To Create Popup Using Jquery"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel