EzAlex
Blockchain Visionary
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.
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
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.
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.
This is our jQuery script to trigger our Image modal.
Ful HTML
That ends this tutorial. Happy Coding :)
Download
Add the jQuery in either on the header tag or at the botton of your body tag.
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
- <img
id
=
"sampleImg"
src
=
"gardens.jpg"
alt
=
"An Example of a Beautiful Garden"
width
=
"300"
height
=
"200"
>
- <!-- Sample Modal -->
- <div
id
=
"sampleModal"
class
=
"modal"
>
- <span
class
=
"close"
id
=
"closeModal"
>
×
</
span
>
- <img
class
=
"modal-content"
id
=
"modalImage"
>
- <div
id
=
"desc"
></
div
>
- </
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.
- #sampleImg
{
- cursor
:
pointer
;
- transition
:
0.3s
;
- }
- #sampleImg
:
hover
{
opacity
:
0.7
;
}
- .modal
{
- display
:
none
;
- position
:
fixed
;
- z-index
:
1
;
- padding-top
:
100px
;
- left
:
0
;
- top
:
0
;
- width
:
100%
;
- height
:
100%
;
- overflow
:
auto
;
- background-color
:
rgb
(
0
,
0
,
0
)
;
- background-color
:
rgba
(
0
,
0
,
0
,
0.9
)
;
- }
- .modal-content
{
- margin
:
auto
;
- display
:
block
;
- width
:
80%
;
- max-width
:
700px
;
- }
- #desc
{
- margin
:
auto
;
- display
:
block
;
- width
:
80%
;
- max-width
:
700px
;
- text-align
:
center
;
- color
:
#ccc
;
- padding
:
10px
0
;
- height
:
150px
;
- }
- .modal-content
,
#desc
{
- -webkit-animation-name:
zoom;
- -webkit-animation-duration:
0.6s
;
- animation-name
:
zoom;
- animation-duration
:
0.6s
;
- }
- @-webkit-keyframes
zoom {
- from {
-webkit-transform:
scale
(
0
)
}
- to {
-webkit-transform:
scale
(
1
)
}
- }
- @keyframes
zoom {
- from {
transform
:
scale
(
0
)
}
- to {
transform
:
scale
(
1
)
}
- }
- .close
{
- position
:
absolute
;
- top
:
15px
;
- right
:
35px
;
- color
:
#f1f1f1
;
- font-size
:
40px
;
- font-weight
:
bold
;
- transition
:
0.3s
;
- }
- .close
:
hover
,
- .close
:
focus
{
- color
:
#bbb
;
- text-decoration
:
none
;
- cursor
:
pointer
;
- }
- @media
only screen and (
max-width
:
700px
)
{
- .modal-content
{
- width
:
100%
;
- }
- }
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.
- $(
document)
.ready
(
function
(
)
{
- $(
'#sampleImg'
)
.click
(
function
(
)
{
- var
src =
$(
this
)
.attr
(
'src'
)
;
- var
alt =
$(
this
)
.attr
(
'alt'
)
;
- $(
'#sampleModal'
)
.css
(
'display'
,
'block'
)
;
- $(
'#modalImage'
)
.attr
(
'src'
,
src)
;
- $(
'#desc'
)
.html
(
alt)
;
- }
)
;
- $(
'#closeModal'
)
.click
(
function
(
)
{
- $(
'#sampleModal'
)
.css
(
'display'
,
'none'
)
;
- }
)
;
- }
)
;
This is our jQuery script to trigger our Image modal.
Ful HTML
- <!DOCTYPE html>
- <html
>
- <head
>
- <meta
charset
=
"utf-8"
>
- <title
>
Create an Image Modal using jQuery</
title
>
- <script
src
=
"https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"
></
script
>
- <style
type
=
"text/css"
>
- #sampleImg {
- cursor: pointer;
- transition: 0.3s;
- }
- #sampleImg:hover {opacity: 0.7;}
- .modal {
- display: none;
- position: fixed;
- z-index: 1;
- padding-top: 100px;
- left: 0;
- top: 0;
- width: 100%;
- height: 100%;
- overflow: auto;
- background-color: rgb(0,0,0);
- background-color: rgba(0,0,0,0.9);
- }
- .modal-content {
- margin: auto;
- display: block;
- width: 80%;
- max-width: 700px;
- }
- #desc {
- margin: auto;
- display: block;
- width: 80%;
- max-width: 700px;
- text-align: center;
- color: #ccc;
- padding: 10px 0;
- height: 150px;
- }
- .modal-content, #desc {
- -webkit-animation-name: zoom;
- -webkit-animation-duration: 0.6s;
- animation-name: zoom;
- animation-duration: 0.6s;
- }
- @-webkit-keyframes zoom {
- from {-webkit-transform:scale(0)}
- to {-webkit-transform:scale(1)}
- }
- @keyframes zoom {
- from {transform:scale(0)}
- to {transform:scale(1)}
- }
- .close {
- position: absolute;
- top: 15px;
- right: 35px;
- color: #f1f1f1;
- font-size: 40px;
- font-weight: bold;
- transition: 0.3s;
- }
- .close:hover,
- .close:focus {
- color: #bbb;
- text-decoration: none;
- cursor: pointer;
- }
- @media only screen and (max-width: 700px){
- .modal-content {
- width: 100%;
- }
- }
- </
style
>
- </
head
>
- <body
>
- <img
id
=
"sampleImg"
src
=
"gardens.jpg"
alt
=
"An Example of a Beautiful Garden"
width
=
"300"
height
=
"200"
>
- <!-- Sample Modal -->
- <div
id
=
"sampleModal"
class
=
"modal"
>
- <span
class
=
"close"
id
=
"closeModal"
>
×
</
span
>
- <img
class
=
"modal-content"
id
=
"modalImage"
>
- <div
id
=
"desc"
></
div
>
- </
div
>
- <script
>
- $(document).ready(function(){
- $('#sampleImg').click(function(){
- var src = $(this).attr('src');
- var alt = $(this).attr('alt');
- $('#sampleModal').css('display', 'block');
- $('#modalImage').attr('src', src);
- $('#desc').html(alt);
- });
- $('#closeModal').click(function(){
- $('#sampleModal').css('display', 'none');
- });
- });
- </
script
>
- </
body
>
- </
html
>
That ends this tutorial. Happy Coding :)
Download
You must upgrade your account or reply in the thread to view hidden text.