Open Animated Popup Windows Using JavaScript

Introduction

 
In this article, we will see how to open a popup window that has some animation. In my Popup Windows in ASP.NET MVC  you saw the modal and modeless popup windows for MVC application and in that article, these windows were very simple windows. In this article, we will not open the popup windows directly but we will open them by incrementing the height and width of the window and then finally show the full-length window.
 
In such a situation where we are showing only titles on one page and then we need to show the whole summary of the title surely so, many developers use the popup windows to display the description. Consider the case when we have only a products list on our page with only product titles as a hyperlink and when the user clicks on any hyperlink we need to fetch the data from the database and display the data in another window. As usual, we will use a popup but it's very effective because the popup is opened immediately but in this article, we will see how to show this popup animated. Using this animated concept we can attract our users.
 
Step 1:
Create a new website add the page to the application.
 
Step 2:
Paste the following scripts to open our popup windows.
  1. <script>  
  2. var winheight = 100  
  3. var winsize = 100  
  4. var x = 5  
  5.   
  6. function openwindow(thelocation) {  
  7.      temploc = thelocation  
  8.      if (!(window.resizeTo && document.all) && !(window.resizeTo && document.getElementById)) {  
  9.           window.open(thelocation)  
  10.           return  
  11.      }  
  12.      win2 = window.open("""""scrollbars")  
  13.      win2.moveTo(0, 0)  
  14.      win2.resizeTo(100, 100)  
  15.      go2()  
  16. }  
  17.   
  18. function go2() {  
  19.      if (winheight >= screen.availHeight - 3)  
  20.           x = 0  
  21.      win2.resizeBy(5, x)  
  22.      winheight += 5  
  23.      winsize += 5  
  24.      if (winsize >= screen.width - 5) {  
  25.           win2.location = temploc  
  26.           winheight = 100  
  27.           winsize = 100  
  28.           x = 5  
  29.           return  
  30.      }  
  31.      setTimeout("go2()", 50)  
  32. }  
  33. </script>  
  34. <script language="JavaScript">  
  35. function expandingWindow(website) {  
  36.      var windowprops = 'width=100,height=100,scrollbars=yes,status=yes,resizable=yes'  
  37.      var heightspeed = 2; // vertical scrolling speed (higher = slower)  
  38.      var widthspeed = 7; // horizontal scrolling speed (higher = slower)  
  39.      var leftdist = 10; // distance to left edge of window  
  40.      var topdist = 10; // distance to top edge of window  
  41.   
  42.      if (window.resizeTo && navigator.userAgent.indexOf("Opera") == -1) {  
  43.           var winwidth = window.screen.availWidth - leftdist;  
  44.           var winheight = window.screen.availHeight - topdist;  
  45.           var sizer = window.open("""""left=" + leftdist + ",top=" + topdist + "," + windowprops);  
  46.           for (sizeheight = 1; sizeheight < winheight; sizeheight += heightspeed)  
  47.                sizer.resizeTo("1", sizeheight);  
  48.           for (sizewidth = 1; sizewidth < winwidth; sizewidth += widthspeed)  
  49.                sizer.resizeTo(sizewidth, sizeheight);  
  50.           sizer.location = website;  
  51.      } else  
  52.           window.open(website, 'mywindow');  
  53. }  
  54. </script> 
In the above sample we have two scripts to open animated popup windows having two functions that use the same code which you are using to open the popup window like window.open and all the things.
 
Step 3:
Now put two hyperlinks on your page to call the function of the above script onclick of the hyperlink like below.
  1. <a href="javascript:openwindow('http://www.c-sharpcorner.com')">C-Sharpcorner</a>  
  2. <br />  
  3. <a href="#" onClick="expandingWindow('http://www.c-sharpcorner.com');return false">C-Sharpcorner</a> 
You can pass a different URL as per your requirement to these functions. As we discussed above, need to show the details of the product here; you can pass the URL of your own page and onload event of the page you can fetch the data and display on the page.
 
Step 4:
Run your application and click on the hyperlink and see the animated popup window.
 

Conclusion

 
Using simple JavaScript we can make our website attractive. I hope you really enjoyed this article.


Similar Articles