{"id":5952,"date":"2020-11-20T18:38:00","date_gmt":"2020-11-20T18:38:00","guid":{"rendered":"https:\/\/azoora.com\/blog\/?p=5952"},"modified":"2020-11-22T19:32:09","modified_gmt":"2020-11-22T19:32:09","slug":"tutorial-how-to-create-a-lightbox-effect-without-using-plugins-in-js-and-css","status":"publish","type":"post","link":"https:\/\/azoora.com\/blog\/code\/tutorial-how-to-create-a-lightbox-effect-without-using-plugins-in-js-and-css\/","title":{"rendered":"Tutorial: How to create a Lightbox Effect without using plugins in JS and CSS"},"content":{"rendered":"\n<p><strong>Lightboxes <\/strong>are amazing! We remember the first time seeing them in the <strong>jQuery <\/strong>days and thinking that someone really spent a lot of time building this, but over the years, things got easy and we&#8217;ve come to realize that it can be made in vanilla <strong>JavaScript <\/strong>and some <strong>CSS<\/strong> easily.<\/p>\n\n\n\n<p>So, today we wanted to show you <strong>how you can build<\/strong> your own image <strong>Lightbox <\/strong>effect <strong>without using any plugins<\/strong>!<\/p>\n\n\n\n<p>The end result is this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-attachment-id=\"5953\" data-permalink=\"https:\/\/azoora.com\/blog\/code\/tutorial-how-to-create-a-lightbox-effect-without-using-plugins-in-js-and-css\/attachment\/tutorial-lightbox-javascript\/#main\" data-orig-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/Tutorial-Lightbox-Javascript.gif\" data-orig-size=\"512,367\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Tutorial-Lightbox-Javascript\" data-image-description=\"\" data-medium-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/Tutorial-Lightbox-Javascript-300x215.gif\" data-large-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/Tutorial-Lightbox-Javascript.gif\" loading=\"lazy\" width=\"512\" height=\"367\" src=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/Tutorial-Lightbox-Javascript.gif\" alt=\"\" class=\"wp-image-5953\"\/><\/figure>\n\n\n\n<h2 id=\"heading-html-structure\">HTML Structure<\/h2>\n\n\n\n<p>We will start by laying out the HTML building blocks of our application.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div class=\"container\">  &lt;div class=\"col\">    &lt;img src=\"https:\/\/images.unsplash.com\/photo-1605347220242-04d3b97ceee9?ixlib=rb-1.2.1&amp;ixid=eyJhcHBfaWQiOjEyMDd9&amp;auto=format&amp;fit=crop&amp;w=1950&amp;q=80\" onClick=\"openLightbox(this)\" \/>  &lt;\/div>  &lt;div class=\"col\">    &lt;img src=\"https:\/\/images.unsplash.com\/photo-1605306030698-6e966cc142b4?ixlib=rb-1.2.1&amp;ixid=eyJhcHBfaWQiOjEyMDd9&amp;auto=format&amp;fit=crop&amp;w=1950&amp;q=80\" onClick=\"openLightbox(this)\" \/>  &lt;\/div>  &lt;div class=\"col\">    &lt;img src=\"https:\/\/images.unsplash.com\/photo-1593642634402-b0eb5e2eebc9?ixlib=rb-1.2.1&amp;auto=format&amp;fit=crop&amp;w=1950&amp;q=80\" onClick=\"openLightbox(this)\" \/>  &lt;\/div>&lt;\/div>&lt;!-- Actual Lightbox -->&lt;div id=\"lightbox\" class=\"lightbox hidden\">  &lt;div onClick=\"closeLightbox()\" class=\"close\">&#x274c;&lt;\/div>  &lt;div class=\"lightbox-content\">    &lt;img id=\"lightbox-image\" \/>  &lt;\/div>&lt;\/div><\/code><\/pre>\n\n\n\n<p>The top part contains the layout the user will see, in this case a container with three columns, each containing one image.<\/p>\n\n\n\n<p>The image has an&nbsp;<code>onClick<\/code>&nbsp;function, which calls the&nbsp;<code>openLightbox<\/code>. (We will create this in a bit)<\/p>\n\n\n\n<p>Then at the bottom, we have the actual Lightbox. Inside the Lightbox, we add a simple emoji powered close button, which&nbsp;<code>onClick<\/code>&nbsp;calls the&nbsp;<code>closeLightbox<\/code>&nbsp;function.<\/p>\n\n\n\n<p>And inside the <strong>Lightbox <\/strong>we also see an empty image which we&#8217;ll use to place our image in.<\/p>\n\n\n\n<h2 id=\"heading-adding-some-styling-to-our-lightbox\">Adding some styling to our Lightbox<\/h2>\n\n\n\n<p>Let&#8217;s make our application look a little bit better by adding some basic styling to it.<\/p>\n\n\n\n<p>First, we will use\u00a0<code>flexbox<\/code>\u00a0to center our columns, and next we add a border and box-shadow to make the image pop more.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.container {  display: flex;  flex-wrap: wrap;  background: url(\"https:\/\/images.unsplash.com\/photo-1558051815-0f18e64e6280?ixlib=rb-1.2.1&amp;ixid=eyJhcHBfaWQiOjEyMDd9&amp;auto=format&amp;fit=crop&amp;w=1949&amp;q=80\") no-repeat center center;  background-position: cover;  min-height: 100vh;  justify-content: center;  align-items: center;}.container .col {  width: 30%;  margin: 1.6%;}.container .col img {  cursor: pointer;  border: 5px solid #fff;  box-shadow: 0 0 1rem #aaaaaa;  max-width: 100%;  max-height: 100%;}<\/code><\/pre>\n\n\n\n<p>As for our Lightbox goes, we need it to span over the whole page, and be fixed starting from the top.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.lightbox {  position: fixed;  z-index: 1;  left: 0;  top: 0;  width: 100%;  height: 100%;  background-color: rgba(0, 0, 0, 0.5);  overflow: auto;  opacity: 1;  visibility: visible;  transition: all 0.3s ease;}<\/code><\/pre>\n\n\n\n<p>We are using opacity and visibility so we can animate the fade-in and fade-out effect.<\/p>\n\n\n\n<p>Now let&#8217;s add the hidden class.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.lightbox.hidden {  opacity: 0;  visibility: hidden;}<\/code><\/pre>\n\n\n\n<p>And to top it up we style the button, content and image inside the Lightbox.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.lightbox .close {  position: absolute;  right: 2.5%;  top: 2.5%;  font-size: 2rem;  cursor: pointer;}.lightbox-content {  display: flex;  margin: 5%;  align-items: center;  justify-content: center;}.lightbox-content img {  max-width: 100%;  max-height: 100%;  border: 5px solid #fff;}<\/code><\/pre>\n\n\n\n<h2 id=\"heading-javascript-lightbox-effect\">JavaScript Lightbox effect<\/h2>\n\n\n\n<p>On to our JavaScript, this is the part that will hook everything up and make it work.<\/p>\n\n\n\n<p>What we want to happen:<\/p>\n\n\n\n<ul><li>User clicks on an image. We get the src of the image to append it to our Lightbox image and remove the hidden class from our Lightbox<\/li><li>User clicks the close button. We re-add the hidden class to our Lightbox.<\/li><\/ul>\n\n\n\n<p>Let&#8217;s define the variables we need to make it work:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const lightbox = document.getElementById('lightbox');const lightboxHolder = document.getElementById('lightbox-image');<\/code><\/pre>\n\n\n\n<p>We define our actual Lightbox element, and the image element inside it.<\/p>\n\n\n\n<p>Now let&#8217;s create the function that will show the Lightbox.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>openLightbox = (element) => {  lightboxHolder.src = element.src;  lightbox.classList.remove(\"hidden\");};<\/code><\/pre>\n\n\n\n<p>Wait, that&#8217;s it? Yes, we retrieve the src of the image the user clicked on and add it to our Lightbox. Then we remove the hidden class, and the user sees our Lightbox!<\/p>\n\n\n\n<p>Now we just need the close function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>closeLightbox = () => lightbox.classList.add(\"hidden\");<\/code><\/pre>\n\n\n\n<p>And now we have a fully functional <strong>Lightbox<\/strong>, <strong>without using any plugins<\/strong>.<\/p>\n\n\n\n<p>You can try this <strong>Lightbox <\/strong>out on the following <strong>Codepen<\/strong> below &#8211;<\/p>\n\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_gOMqvEK\" src=\"\/\/codepen.io\/anon\/embed\/gOMqvEK?height=250&amp;theme-id=1&amp;slug-hash=gOMqvEK&amp;default-tab=result\" height=\"250\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed gOMqvEK\" title=\"CodePen Embed gOMqvEK\" class=\"cp_embed_iframe\" style=\"width:100%;overflow:hidden\">CodePen Embed Fallback<\/iframe><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Lightboxes are amazing! We remember the first time seeing them in the jQuery days and thinking that someone really spent a lot of time building this, but over the years, things got easy and we&#8217;ve come to realize that it can be made in vanilla JavaScript and some CSS easily. So, today we wanted to [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":5953,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[4,145,12,62],"tags":[25,56,94,146,136,116,87,110],"jetpack_featured_media_url":"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/Tutorial-Lightbox-Javascript.gif","jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/p7FQPL-1y0","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts\/5952"}],"collection":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/comments?post=5952"}],"version-history":[{"count":2,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts\/5952\/revisions"}],"predecessor-version":[{"id":6371,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts\/5952\/revisions\/6371"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/media\/5953"}],"wp:attachment":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/media?parent=5952"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/categories?post=5952"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/tags?post=5952"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}