JQuery Lightbox

Live Demo
http://www.bibekshakya.com/demo/jquery/jquery-lightbox/

HTML Code (index.html)
<!DOCTYPE html>
<html lang="en">

<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <meta http-equiv="X-UA-Compatible" content="ie=edge">
 <title>JQuery Lightbox</title>
 <!-- using google font -->
 <link href="https://fonts.googleapis.com/css?family=Poppins:300,400" rel="stylesheet">
 <!-- using font awesome 5 -->
 <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ"
   crossorigin="anonymous">
 <!-- using custom css -->
 <link rel="stylesheet" href="css/main.css">
</head>

<body>
 <!-- hero block -->
 <section class="hero" style="background-image: url('images/img-intro.jpg')">
  <div class="container">
   <div class="content">
    <h1>Our Works</h1>
    <p>Hic adipisci asperiores quasi dolore enim, necessitatibus in!</p>
   </div>
  </div>
 </section>
 <main>
  <div class="container">
   <!-- image gallery -->
   <ul id="workGallery">
    <li>
     <a href="images/img1.jpg">
      <img src="images/img1.jpg" alt="Refferal Machine By Matthew Spiel">
     </a>
    </li>
    <li>
     <a href="images/img2.jpg">
      <img src="images/img2.jpg" alt="Space Juice by Mat Helme">
     </a>
    </li>
    <li>
     <a href="images/img3.jpg">
      <img src="images/img3.jpg" alt="Education by Chris Michel">
     </a>
    </li>
    <li>
     <a href="images/img4.jpg">
      <img src="images/img4.jpg" alt="Wanted: Copy McRepeatsalot by Chris Michel">
     </a>
    </li>
    <li>
     <a href="images/img5.jpg">
      <img src="images/img5.jpg" alt="Sebastian by Mat Helme">
     </a>
    </li>
    <li>
     <a href="images/img6.jpg">
      <img src="images/img6.jpg" alt="Refferal Machine By Matthew Spiel">
     </a>
    </li>
    <li>
     <a href="images/img7.jpg">
      <img src="images/img7.jpg" alt="Space Juice by Mat Helme">
     </a>
    </li>
    <li>
     <a href="images/img8.jpg">
      <img src="images/img8.jpg" alt="Education by Chris Michel">
     </a>
    </li>
   </ul>
   <!-- get in touch -->
   <div class="getInTouch">
    <h2>Get in Touch</h2>
    <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Iste omnis fugit distinctio consequatur quibusdam voluptatem,
     quisquam at qui ab neque praesentium nostrum, possimus sunt esse assumenda beatae aut asperiores inventore.</p>
    <a href="#" class="btn">Get Free Quote</a>
   </div>
  </div>
 </main>
 <!-- 
 <div id="overlay">
  <div class="wrap">
   <img src="#">
   <p>caption comes here</p>
   <a href="#" class="close">×</a>
  </div>
 </div> 
 -->
 <!-- using jquery cdn -->
 <script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
   crossorigin="anonymous"></script>
 <!-- using custom js -->
 <script src="js/main.js"></script>
</body>

</html>

SCSS Code (scss/base/_mixins.scss)
@mixin size($width, $height: $width) {
  width: $width;
  height: $height;
}

@mixin hide-text {
  overflow: hidden;
  text-indent: 101%;
  white-space: nowrap;
}

%listreset {
  margin: 0;
  padding: 0;
  list-style: none;
}

@mixin transform ($value) {
  -moz-transform: $value;
  -ms-transform: $value;
  -webkit-transform: $value;
  transform: $value;
}

@mixin transition ($value) {
  -moz-transition: $value;
  -ms-transition: $value;
  -webkit-transition: $value;
  transition: $value;
}

@mixin placeholder {
  &::-webkit-input-placeholder {
    @content
  }
  &::-moz-placeholder {
    opacity: 1;
    @content
  }
  &:-moz-placeholder {
    @content
  }
  &:-ms-input-placeholder {
    @content
  }
  &.placeholder {
    @content
  }
}

SCSS Code (scss/base/_reset.scss)
html {
  box-sizing: border-box;
}

*,
*:before,
*:after {
  box-sizing: inherit;
}

* {
  max-height: 1000000px;
  margin: 0;
  padding: 0;
}

body {
  color: $default-text-color;
  background: $default-background-color;
  font: 300 #{$default-font-size}/#{$default-line-height} $default-font-family;
  min-width: $default-min-width;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

img {
  max-width: 100%;
  height: auto;
  vertical-align: top;
}

.gm-style img {
  max-width: none
}

@media only screen and (min-width:1025px) {
  a[href^=tel],
  a[href^=skype],
  a[href^=sms] {
    cursor: default;
    pointer-events: none;
  }
}

/* HTML5 display-role reset for older browsers */

article,
aside,
details,
figcaption,
figure,
footer,
header,
hgroup,
menu,
nav,
section {
  display: block;
}

ol,
ul {
  list-style: none;
}

blockquote,
q {
  quotes: none;
}

blockquote:before,
blockquote:after,
q:before,
q:after {
  content: '';
  content: none;
}

table {
  border-collapse: collapse;
  border-spacing: 0;
}

SCSS Code (scss/base/_typography.scss)
h1,
h2,
h3,
h4,
h5,
h6 {
  font-family: $headings-font-family;
  font-weight: 400;
  margin: 0 0 10px;
  color: $headings-color;
}

h1 {
  font-size: $h1-font-size;
}

h2 {
  font-size: $h2-font-size;
}

h3 {
  font-size: $h3-font-size;
}

h4 {
  font-size: $h4-font-size;
}

h5 {
  font-size: $h5-font-size;
}

h6 {
  font-size: $h6-font-size;
}

p {
  margin: 0 0 20px;
}

a {
  color: $default-link-color;
  text-decoration: none;
  transition: color $animation-speed linear;
  &:hover,
  &:focus {
    text-decoration: none;
    color: darken($default-link-color, 15%);
  }
}

SCSS Code (scss/base/_variable.scss)
// colors
$black: #000;
$white: #fff;
$gray: #333;
$primary-color: #009432;

// font family
$primary-font: 'Poppins', sans-serif;

// Body
$default-text-color: $black  !default;
$default-background-color: $white !default;
$default-font-size: 18px !default;
$default-line-height: 1.7 !default;
$default-font-family: $primary-font !default;
$default-min-width: 320px;

// Links
$default-link-color: $primary-color;

// Headers
$h1-font-size: 36px !default;
$h2-font-size: 32px !default;
$h3-font-size: 22px !default;
$h4-font-size: 18px !default;
$h5-font-size: 17px !default;
$h6-font-size: 15px !default;

$headings-font-family: $primary-font !default;
$headings-color: $black !default;

// animation
$animation-speed: 0.3s; 

SCSS Code (scss/base/_main.scss)
@import 'base/variable';
@import 'base/mixins';
@import 'base/reset';
@import 'base/typography';

// css3 animation
@-webkit-keyframes lightboxZoom {
 from {transform: translate(-50%, -50%) scale(0);}
 to {transform: translate(-50%, -50%) scale(1);}
}

@keyframes lightboxZoom {
 from {transform: translate(-50%, -50%) scale(0);}
 to {transform: translate(-50%, -50%) scale(1);}
}

// general
.container {
 max-width: 1170px;
 padding: 0 15px;
 margin: 0 auto;
}

// hero block
.hero {
 background-size: cover;
 background-position: 50%;
 min-height: 300px;
 color: $white;
 position: relative;
 margin: 0 0 60px;
 &:after {
  content: '';
  background: rgba(0, 0, 0, 0.6);
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
 }
 .content {
  position: absolute;
  bottom: 30px;
  z-index: 1;
 }
 h1 {
  color: $white;
  margin: 0;
 }
}

// work gallery
#workGallery {
 font-size: 0;
 line-height: 0;
 letter-spacing: -4px;
 margin: 0 -10px 40px;
 li {
  display: inline-block;
  vertical-align: top;
  width: 25%;
  font-size: 18px;
  line-height: 1;
  letter-spacing: 0;
  padding: 0 10px 20px;
 }
 a {
  display: block;
  position: relative;
  &:before {
   content: '';
   background: rgba(0, 0, 0, 0.5);
   position: absolute;
   height: 100%;
   width: 100%;
   transform: scale(0);
   transition: transform 0.3s cubic-bezier(0.3, 1, 0.8, 1);
  }
  &:after {
   content: '\f00e';
   font-family: 'Font Awesome 5 Free';
   font-weight: 900;
   font-size: 30px;
   line-height: 1;
   margin: -10px 0 0;
   color: $white;
   position: absolute;
   top: 50%;
   left: 50%;
   transform: translate(-50%) scale(0);
   transition: transform 0.4s cubic-bezier(0.3, 1, 0.8, 1);
  }
  &:hover {
   &:before {
    transform: scale(1);
   }
   &:after {
    transform: translate(-50%) scale(1);
   }
  }
 }
}

// get in touch
.getInTouch {
 position: relative;
 text-align: center;
 padding: 30px 0 60px;
 max-width: 800px;
 margin: 0 auto;
 &:after {
  content: '';
  background: #eee;
  height: 1px;
  position: absolute;
  top: 0;
  left: 30%;
  right: 30%;
 }
 h2 {
  margin: 0 0 15px;
 }
 p {
  margin: 0 0 30px;
 }
 .btn {
  @include transition(0.3s);
  border: 1px solid;
  padding: 5px 25px;
  display: inline-block;
  vertical-align: top;
  border-radius: 25px;
  &:hover {
   box-shadow: inset 0 50px 0 0 $primary-color;
   color: $white;
  }
 }
}

// overlay
#overlay {
 background: rgba(0, 0, 0, 0.7);
 position: fixed;
 top: 0;
 bottom: 0;
 left: 0;
 right: 0;
 z-index: 2;
 display: none;
 text-align: center;
 .wrap {
  border: 8px solid $white;
  border-radius: 3px;
  background: $white;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
    animation-name: lightboxZoom;
    animation-duration: 0.3s;
  img {
   margin: 0 0 10px;
  }
  p {
   margin: 0 0 5px;
  }
 }
 .close {
  @include transition(0.3s);
  @include size(30px);
  color: $white;
  cursor: pointer;
  background: $black;
  border-radius: 50%;
  font-size: 26px;
  line-height: 1;
  position: absolute;
  top: 10px;
  right: 10px;
  padding: 3px;
  &:hover {
   transform: rotate(180deg);
  }
 }
}

// responsive
@media screen and (max-width: 1023px) {
 // work gallery
 #workGallery li {
  width: 33.33%;
 }
}

@media screen and (max-width: 767px) {
 // general
 body {
  font-size: 14px;
 }
 // hero block
 .hero {
  min-height: 160px;
  margin: 0 0 20px;
 }
 .hero .content {
  bottom: 0;
 }
 .hero h1 {
  font-size: 26px;
 }
 // work gallery
 #workGallery {
  margin: 0 -10px 10px;
 }
 #workGallery li {
  width: 50%;
 }
 // get in touch
 .getInTouch {
  padding: 10px 0 40px;
 }
 .getInTouch h2 {
  font-size: 22px;
  margin: 0 0 5px;
 }
 .getInTouch p {
  margin: 0 0 20px;
 }
 // overlay
 #overlay .wrap {
  width: 75%;
 }
 #overlay .close {
  @include size(25px);
  font-size: 22px;
 }
}

Images
Note: Use your own images. Make sure you update your images path in index.html

Compiled CSS Code (css/main.css)
Note: Only use this compiled CSS if you don't have idea in SCSS. Otherwise ignore this CSS.
html {
  box-sizing: border-box; }

*,
*:before,
*:after {
  box-sizing: inherit; }

* {
  max-height: 1000000px;
  margin: 0;
  padding: 0; }

body {
  color: #000;
  background: #fff;
  font: 300 18px/1.7 "Poppins", sans-serif;
  min-width: 320px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale; }

img {
  max-width: 100%;
  height: auto;
  vertical-align: top; }

.gm-style img {
  max-width: none; }

@media only screen and (min-width: 1025px) {
  a[href^=tel],
  a[href^=skype],
  a[href^=sms] {
    cursor: default;
    pointer-events: none; } }
/* HTML5 display-role reset for older browsers */
article,
aside,
details,
figcaption,
figure,
footer,
header,
hgroup,
menu,
nav,
section {
  display: block; }

ol,
ul {
  list-style: none; }

blockquote,
q {
  quotes: none; }

blockquote:before,
blockquote:after,
q:before,
q:after {
  content: '';
  content: none; }

table {
  border-collapse: collapse;
  border-spacing: 0; }

h1,
h2,
h3,
h4,
h5,
h6 {
  font-family: "Poppins", sans-serif;
  font-weight: 400;
  margin: 0 0 10px;
  color: #000; }

h1 {
  font-size: 36px; }

h2 {
  font-size: 32px; }

h3 {
  font-size: 22px; }

h4 {
  font-size: 18px; }

h5 {
  font-size: 17px; }

h6 {
  font-size: 15px; }

p {
  margin: 0 0 20px; }

a {
  color: #009432;
  text-decoration: none;
  transition: color 0.3s linear; }
  a:hover, a:focus {
    text-decoration: none;
    color: #004818; }

@-webkit-keyframes lightboxZoom {
  from {
    transform: translate(-50%, -50%) scale(0); }
  to {
    transform: translate(-50%, -50%) scale(1); } }
@keyframes lightboxZoom {
  from {
    transform: translate(-50%, -50%) scale(0); }
  to {
    transform: translate(-50%, -50%) scale(1); } }
.container {
  max-width: 1170px;
  padding: 0 15px;
  margin: 0 auto; }

.hero {
  background-size: cover;
  background-position: 50%;
  min-height: 300px;
  color: #fff;
  position: relative;
  margin: 0 0 60px; }
  .hero:after {
    content: '';
    background: rgba(0, 0, 0, 0.6);
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0; }
  .hero .content {
    position: absolute;
    bottom: 30px;
    z-index: 1; }
  .hero h1 {
    color: #fff;
    margin: 0; }

#workGallery {
  font-size: 0;
  line-height: 0;
  letter-spacing: -4px;
  margin: 0 -10px 40px; }
  #workGallery li {
    display: inline-block;
    vertical-align: top;
    width: 25%;
    font-size: 18px;
    line-height: 1;
    letter-spacing: 0;
    padding: 0 10px 20px; }
  #workGallery a {
    display: block;
    position: relative; }
    #workGallery a:before {
      content: '';
      background: rgba(0, 0, 0, 0.5);
      position: absolute;
      height: 100%;
      width: 100%;
      transform: scale(0);
      transition: transform 0.3s cubic-bezier(0.3, 1, 0.8, 1); }
    #workGallery a:after {
      content: '\f00e';
      font-family: 'Font Awesome 5 Free';
      font-weight: 900;
      font-size: 30px;
      line-height: 1;
      margin: -10px 0 0;
      color: #fff;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%) scale(0);
      transition: transform 0.4s cubic-bezier(0.3, 1, 0.8, 1); }
    #workGallery a:hover:before {
      transform: scale(1); }
    #workGallery a:hover:after {
      transform: translate(-50%) scale(1); }

.getInTouch {
  position: relative;
  text-align: center;
  padding: 30px 0 60px;
  max-width: 800px;
  margin: 0 auto; }
  .getInTouch:after {
    content: '';
    background: #eee;
    height: 1px;
    position: absolute;
    top: 0;
    left: 30%;
    right: 30%; }
  .getInTouch h2 {
    margin: 0 0 15px; }
  .getInTouch p {
    margin: 0 0 30px; }
  .getInTouch .btn {
    -moz-transition: 0.3s;
    -ms-transition: 0.3s;
    -webkit-transition: 0.3s;
    transition: 0.3s;
    border: 1px solid;
    padding: 5px 25px;
    display: inline-block;
    vertical-align: top;
    border-radius: 25px; }
    .getInTouch .btn:hover {
      box-shadow: inset 0 50px 0 0 #009432;
      color: #fff; }

#overlay {
  background: rgba(0, 0, 0, 0.7);
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 2;
  display: none;
  text-align: center; }
  #overlay .wrap {
    border: 8px solid #fff;
    border-radius: 3px;
    background: #fff;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    animation-name: lightboxZoom;
    animation-duration: 0.3s; }
    #overlay .wrap img {
      margin: 0 0 10px; }
    #overlay .wrap p {
      margin: 0 0 5px; }
  #overlay .close {
    -moz-transition: 0.3s;
    -ms-transition: 0.3s;
    -webkit-transition: 0.3s;
    transition: 0.3s;
    width: 30px;
    height: 30px;
    color: #fff;
    cursor: pointer;
    background: #000;
    border-radius: 50%;
    font-size: 26px;
    line-height: 1;
    position: absolute;
    top: 10px;
    right: 10px;
    padding: 3px; }
    #overlay .close:hover {
      transform: rotate(180deg); }

@media screen and (max-width: 1023px) {
  #workGallery li {
    width: 33.33%; } }
@media screen and (max-width: 767px) {
  body {
    font-size: 14px; }

  .hero {
    min-height: 160px;
    margin: 0 0 20px; }

  .hero .content {
    bottom: 0; }

  .hero h1 {
    font-size: 26px; }

  #workGallery {
    margin: 0 -10px 10px; }

  #workGallery li {
    width: 50%; }

  .getInTouch {
    padding: 10px 0 40px; }

  .getInTouch h2 {
    font-size: 22px;
    margin: 0 0 5px; }

  .getInTouch p {
    margin: 0 0 20px; }

  #overlay .wrap {
    width: 75%; }

  #overlay .close {
    width: 25px;
    height: 25px;
    font-size: 22px; } }


Video Tutorial


Updated JQuery Code (js/main.js)
// create element
var $overlay = $('<div id="overlay"></div>');
var $wrap = $('<div class="wrap"></div>');
var $image = $('<img>');
var $caption = $('<p></p>');
var $close = $('<a href="#" class="close">×</a>');

// append method
$overlay.append($wrap);
$wrap.append($image);
$wrap.append($caption);
$wrap.append($close);
$('body').append($overlay);

// click on each images
$('#workGallery').on('click', 'a', function(event) {
 event.preventDefault();

 var imageSrc = $(this).attr('href');
 $image.attr('src', imageSrc);

 var captionText = $(this).children().attr('alt');
 $caption.text(captionText);

 $overlay.show();
});

// click on close icon
$close.click(function() {
 $overlay.hide();
 return false;
});

Share this

Related Posts