Angular JS Create an Image Modal using CSS

Step 1 : Add our Angular JS

In the header tag or at the bottom of body tag add the ff to include our Angular JS.
  1. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
This is the Angular JS that we need to include in order to make use of Angular JS. Note: It is in CDN so you need internet connection for it to work.

Step 2 : Create our Image and our Modal

In the body part of your html, add the ff.
  1. <img id="sampleImg" ng-src="{{ imgSrc }}" alt="{{ alt }}" width="300" height="200" ng-click="showModal()">
  2.  
  3. <!-- Sample Modal -->
  4. <div class="modal" ng-show="myModal">
  5. <span class="close" ng-click="closeModal()">&times;</span>
  6. <img class="modal-content" ng-src="{{ imgSrc }}">
  7. <div id="desc">{{ alt }}</div>
  8. </div>
Using Angular JS, we have set the src of the img in our controller as well as its alt text which we are going to use in the modal as well.

Step 3 : Add Style

Include the ff style.
  1. #sampleImg {
  2. cursor: pointer;
  3. transition: 0.3s;
  4. }
  5.  
  6. #sampleImg:hover {opacity: 0.7;}
  7.  
  8. .modal {
  9. position: fixed;
  10. z-index: 1;
  11. padding-top: 100px;
  12. left: 0;
  13. top: 0;
  14. width: 100%;
  15. height: 100%;
  16. overflow: auto;
  17. background-color: rgb(0,0,0);
  18. background-color: rgba(0,0,0,0.9);
  19. }
  20.  
  21. .modal-content {
  22. margin: auto;
  23. display: block;
  24. width: 80%;
  25. max-width: 700px;
  26. }
  27.  
  28. #desc {
  29. margin: auto;
  30. display: block;
  31. width: 80%;
  32. max-width: 700px;
  33. text-align: center;
  34. color: #ccc;
  35. padding: 10px 0;
  36. height: 150px;
  37. }
  38.  
  39. .modal-content, #desc {
  40. -webkit-animation-name: zoom;
  41. -webkit-animation-duration: 0.6s;
  42. animation-name: zoom;
  43. animation-duration: 0.6s;
  44. }
  45.  
  46. @-webkit-keyframes zoom {
  47. from {-webkit-transform:scale(0)}
  48. to {-webkit-transform:scale(1)}
  49. }
  50.  
  51. @keyframes zoom {
  52. from {transform:scale(0)}
  53. to {transform:scale(1)}
  54. }
  55.  
  56. .close {
  57. position: absolute;
  58. top: 15px;
  59. right: 35px;
  60. color: #f1f1f1;
  61. font-size: 40px;
  62. font-weight: bold;
  63. transition: 0.3s;
  64. }
  65.  
  66. .close:hover,
  67. .close:focus {
  68. color: #bbb;
  69. text-decoration: none;
  70. cursor: pointer;
  71. }
  72.  
  73. @media only screen and (max-width: 700px){
  74. .modal-content {
  75. width: 100%;
  76. }
  77. }

Step 4 : Add our Angular JS Script

Lastly, we add our angular js script.
  1. var app = angular.module('app', []);
  2.  
  3. app.controller('myCtrl', function($scope){
  4. $scope.myModal = false;
  5. $scope.imgSrc = 'gardens.jpg';
  6. $scope.alt = 'An Example of a Beautiful Garden';
  7.  
  8. $scope.showModal = function(){
  9. $scope.myModal = true;
  10. }
  11.  
  12. $scope.closeModal = function(){
  13. $scope.myModal = false;
  14. }
  15. });
In here, we have declared the value for our imgSrc and alt since we already know what they are. Upon clicking the the image image, the showModal function is triggered and will show our myModal modal.

Full HTML

  1. <!DOCTYPE html>
  2. <html ng-app="app">
  3. <meta charset="utf-8">
  4. <title>Create an Image Modal using Angular JS</title>
  5. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
  6. <style type="text/css">
  7. #sampleImg {
  8. cursor: pointer;
  9. transition: 0.3s;
  10. }
  11.  
  12. #sampleImg:hover {opacity: 0.7;}
  13.  
  14. .modal {
  15. position: fixed;
  16. z-index: 1;
  17. padding-top: 100px;
  18. left: 0;
  19. top: 0;
  20. width: 100%;
  21. height: 100%;
  22. overflow: auto;
  23. background-color: rgb(0,0,0);
  24. background-color: rgba(0,0,0,0.9);
  25. }
  26.  
  27. .modal-content {
  28. margin: auto;
  29. display: block;
  30. width: 80%;
  31. max-width: 700px;
  32. }
  33.  
  34. #desc {
  35. margin: auto;
  36. display: block;
  37. width: 80%;
  38. max-width: 700px;
  39. text-align: center;
  40. color: #ccc;
  41. padding: 10px 0;
  42. height: 150px;
  43. }
  44.  
  45. .modal-content, #desc {
  46. -webkit-animation-name: zoom;
  47. -webkit-animation-duration: 0.6s;
  48. animation-name: zoom;
  49. animation-duration: 0.6s;
  50. }
  51.  
  52. @-webkit-keyframes zoom {
  53. from {-webkit-transform:scale(0)}
  54. to {-webkit-transform:scale(1)}
  55. }
  56.  
  57. @keyframes zoom {
  58. from {transform:scale(0)}
  59. to {transform:scale(1)}
  60. }
  61.  
  62. .close {
  63. position: absolute;
  64. top: 15px;
  65. right: 35px;
  66. color: #f1f1f1;
  67. font-size: 40px;
  68. font-weight: bold;
  69. transition: 0.3s;
  70. }
  71.  
  72. .close:hover,
  73. .close:focus {
  74. color: #bbb;
  75. text-decoration: none;
  76. cursor: pointer;
  77. }
  78.  
  79. @media only screen and (max-width: 700px){
  80. .modal-content {
  81. width: 100%;
  82. }
  83. }
  84. </style>
  85. </head>
  86. <body ng-controller="myCtrl">
  87.  
  88. <img id="sampleImg" ng-src="{{ imgSrc }}" alt="{{ alt }}" width="300" height="200" ng-click="showModal()">
  89.  
  90. <!-- Sample Modal -->
  91. <div class="modal" ng-show="myModal">
  92. <span class="close" ng-click="closeModal()">&times;</span>
  93. <img class="modal-content" ng-src="{{ imgSrc }}">
  94. <div id="desc">{{ alt }}</div>
  95. </div>
  96.  
  97. var app = angular.module('app', []);
  98.  
  99. app.controller('myCtrl', function($scope){
  100. $scope.myModal = false;
  101. $scope.imgSrc = 'gardens.jpg';
  102. $scope.alt = 'An Example of a Beautiful Garden';
  103.  
  104. $scope.showModal = function(){
  105. $scope.myModal = true;
  106. }
  107.  
  108. $scope.closeModal = function(){
  109. $scope.myModal = false;
  110. }
  111. });
  112. </body>
  113. </html>
That ends this tutorial. Happy Coding :)

Add new comment