• We just launched and are currently in beta. Join us as we build and grow the community.

Create an Image Modal using jQuery

EzAlex

Blockchain Visionary
E Rep
0
0
0
Rep
0
E Vouches
0
0
0
Vouches
0
Posts
94
Likes
93
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
Step 1 : Add jQuery Library

Add the jQuery in either on the header tag or at the botton of your body tag.

  1. https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js

This is the dependency to use jQuery and it is in CDN means you need internet connection for it to work.

Step 2 : Creating our Image and Modal

  1. <img

    id

    =

    "sampleImg"

    src

    =

    "gardens.jpg"

    alt

    =

    "An Example of a Beautiful Garden"

    width

    =

    "300"

    height

    =

    "200"

    >

  2. <!-- Sample Modal -->
  3. <div

    id

    =

    "sampleModal"

    class

    =

    "modal"

    >
  4. <span

    class

    =

    "close"

    id

    =

    "closeModal"

    >

    &times;

    </

    span

    >
  5. <img

    class

    =

    "modal-content"

    id

    =

    "modalImage"

    >
  6. <div

    id

    =

    "desc"

    ></

    div

    >
  7. </

    div

    >

In here we have set our smaller image that will serve as a link to open our image modal.

Add CSS

Add the following css.

  1. #sampleImg

    {
  2. cursor

    :

    pointer

    ;
  3. transition

    :

    0.3s

    ;
  4. }

  5. #sampleImg

    :

    hover

    {

    opacity

    :

    0.7

    ;

    }

  6. .modal

    {
  7. display

    :

    none

    ;
  8. position

    :

    fixed

    ;
  9. z-index

    :

    1

    ;
  10. padding-top

    :

    100px

    ;
  11. left

    :

    0

    ;
  12. top

    :

    0

    ;
  13. width

    :

    100%

    ;
  14. height

    :

    100%

    ;
  15. overflow

    :

    auto

    ;
  16. background-color

    :

    rgb

    (

    0

    ,

    0

    ,

    0

    )

    ;
  17. background-color

    :

    rgba

    (

    0

    ,

    0

    ,

    0

    ,

    0.9

    )

    ;
  18. }

  19. .modal-content

    {
  20. margin

    :

    auto

    ;
  21. display

    :

    block

    ;
  22. width

    :

    80%

    ;
  23. max-width

    :

    700px

    ;
  24. }

  25. #desc

    {
  26. margin

    :

    auto

    ;
  27. display

    :

    block

    ;
  28. width

    :

    80%

    ;
  29. max-width

    :

    700px

    ;
  30. text-align

    :

    center

    ;
  31. color

    :

    #ccc

    ;
  32. padding

    :

    10px

    0

    ;
  33. height

    :

    150px

    ;
  34. }

  35. .modal-content

    ,

    #desc

    {
  36. -webkit-animation-name:

    zoom;
  37. -webkit-animation-duration:

    0.6s

    ;
  38. animation-name

    :

    zoom;
  39. animation-duration

    :

    0.6s

    ;
  40. }

  41. @-webkit-keyframes

    zoom {
  42. from {

    -webkit-transform:

    scale

    (

    0

    )

    }
  43. to {

    -webkit-transform:

    scale

    (

    1

    )

    }
  44. }

  45. @keyframes

    zoom {
  46. from {

    transform

    :

    scale

    (

    0

    )

    }
  47. to {

    transform

    :

    scale

    (

    1

    )

    }
  48. }

  49. .close

    {
  50. position

    :

    absolute

    ;
  51. top

    :

    15px

    ;
  52. right

    :

    35px

    ;
  53. color

    :

    #f1f1f1

    ;
  54. font-size

    :

    40px

    ;
  55. font-weight

    :

    bold

    ;
  56. transition

    :

    0.3s

    ;
  57. }

  58. .close

    :

    hover

    ,
  59. .close

    :

    focus

    {
  60. color

    :

    #bbb

    ;
  61. text-decoration

    :

    none

    ;
  62. cursor

    :

    pointer

    ;
  63. }

  64. @media

    only screen and (

    max-width

    :

    700px

    )

    {
  65. .modal-content

    {
  66. width

    :

    100%

    ;
  67. }
  68. }

In the CSS we initially define the modal display as none and if the user clicks the smaller image, it will change its display.

Step 3 : Add jQuery Script

Lastly, we add our jQuery script.

  1. $(

    document)

    .ready

    (

    function

    (

    )

    {
  2. $(

    '#sampleImg'

    )

    .click

    (

    function

    (

    )

    {
  3. var

    src =

    $(

    this

    )

    .attr

    (

    'src'

    )

    ;
  4. var

    alt =

    $(

    this

    )

    .attr

    (

    'alt'

    )

    ;

  5. $(

    '#sampleModal'

    )

    .css

    (

    'display'

    ,

    'block'

    )

    ;
  6. $(

    '#modalImage'

    )

    .attr

    (

    'src'

    ,

    src)

    ;
  7. $(

    '#desc'

    )

    .html

    (

    alt)

    ;
  8. }

    )

    ;

  9. $(

    '#closeModal'

    )

    .click

    (

    function

    (

    )

    {
  10. $(

    '#sampleModal'

    )

    .css

    (

    'display'

    ,

    'none'

    )

    ;
  11. }

    )

    ;
  12. }

    )

    ;

This is our jQuery script to trigger our Image modal.

Ful HTML

  1. <!DOCTYPE html>
  2. <html

    >
  3. <head

    >
  4. <meta

    charset

    =

    "utf-8"

    >
  5. <title

    >

    Create an Image Modal using jQuery</

    title

    >
  6. <script

    src

    =

    "https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"

    ></

    script

    >
  7. <style

    type

    =

    "text/css"

    >
  8. #sampleImg {
  9. cursor: pointer;
  10. transition: 0.3s;
  11. }

  12. #sampleImg:hover {opacity: 0.7;}

  13. .modal {
  14. display: none;
  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. .modal-content {
  27. margin: auto;
  28. display: block;
  29. width: 80%;
  30. max-width: 700px;
  31. }

  32. #desc {
  33. margin: auto;
  34. display: block;
  35. width: 80%;
  36. max-width: 700px;
  37. text-align: center;
  38. color: #ccc;
  39. padding: 10px 0;
  40. height: 150px;
  41. }

  42. .modal-content, #desc {
  43. -webkit-animation-name: zoom;
  44. -webkit-animation-duration: 0.6s;
  45. animation-name: zoom;
  46. animation-duration: 0.6s;
  47. }

  48. @-webkit-keyframes zoom {
  49. from {-webkit-transform:scale(0)}
  50. to {-webkit-transform:scale(1)}
  51. }

  52. @keyframes zoom {
  53. from {transform:scale(0)}
  54. to {transform:scale(1)}
  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. .close:hover,
  66. .close:focus {
  67. color: #bbb;
  68. text-decoration: none;
  69. cursor: pointer;
  70. }

  71. @media only screen and (max-width: 700px){
  72. .modal-content {
  73. width: 100%;
  74. }
  75. }
  76. </

    style

    >
  77. </

    head

    >
  78. <body

    >

  79. <img

    id

    =

    "sampleImg"

    src

    =

    "gardens.jpg"

    alt

    =

    "An Example of a Beautiful Garden"

    width

    =

    "300"

    height

    =

    "200"

    >

  80. <!-- Sample Modal -->
  81. <div

    id

    =

    "sampleModal"

    class

    =

    "modal"

    >
  82. <span

    class

    =

    "close"

    id

    =

    "closeModal"

    >

    &times;

    </

    span

    >
  83. <img

    class

    =

    "modal-content"

    id

    =

    "modalImage"

    >
  84. <div

    id

    =

    "desc"

    ></

    div

    >
  85. </

    div

    >

  86. <script

    >
  87. $(document).ready(function(){
  88. $('#sampleImg').click(function(){
  89. var src = $(this).attr('src');
  90. var alt = $(this).attr('alt');

  91. $('#sampleModal').css('display', 'block');
  92. $('#modalImage').attr('src', src);
  93. $('#desc').html(alt);
  94. });

  95. $('#closeModal').click(function(){
  96. $('#sampleModal').css('display', 'none');
  97. });
  98. });
  99. </

    script

    >
  100. </

    body

    >
  101. </

    html

    >

That ends this tutorial. Happy Coding :)


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,700

323,708

Top