Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8605933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:58:07+00:00 2026-06-12T02:58:07+00:00

I have never used a lightbox before, but recently started thinking about using a

  • 0

I have never used a lightbox before, but recently started thinking about using a similar feature on my website. I found a ton of jquery libraries and various add-ons, but I am a big fan of writing my own code. When I started looking under the hood, I was surprised to realize that it appears to simply be a hidden html element that is displayed when a Javascript event listener is triggered. Am I right about this? Is there more than meets the eye?

Just wondering how it works. Thank you in advance for your consideration.

UPDATE

Great answers! I went with Ana’s response because the other considerations regarding the design go without saying. As far as the mechanisms, it really does appear to be a wonderfully understated device. Thanks to all who read, commented, and replied…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:58:09+00:00Added an answer on June 12, 2026 at 2:58 am

    No, there really is not much more to it. However, I don’t really see the point of having the HTML element there from the beginning unless you don’t want to use JavaScript for your lightbox, which I don’t think it is the case here judging from your tags and which I’m not a fan of using in real projects, even though I think it is really cool that it can be done.

    If you’re using JavaScript anyway in order to display the lightbox (which means that if JavaScript is disabled, your lightbox won’t get displayed even if it is there, loaded right from the beginning… so why load it if you can’t display it?), then it’s probably better to just create the lightbox element (and everything in it, and then append it to the page) only when you first want the lightbox to open.

    What I mean by that is that you attach a click handler to the links (actually, I would attach it to the container an then check what was clicked, if it is a link, see what link it is and go further) and check whether you have a lightbox element. If you don’t, then you create it on the spot. If you already do, then you simply display it with whatever you need in it for that particular link that was clicked.

    A basic lightbox example for an image gallery.

    The HTML structure of the gallery would be:

    <section class='gallery' id='gallery'>
      <a href='images/large_0.jpg' class='thumb'>
        <img class='thumb-img' src='images/small_0.jpg'>
      </a>
      <!-- as many more as you wish -->
    </section>
    

    The CSS:

    /* gallery with thumbnails */
    .gallery { text-align: center; }
    .gallery .thumb, .gallery .thumb-img {
      display: inline-block;
      width: 10em;
      height: 5.6em;
    }
    /* lightbox */
    .lightbox {
      z-index: 999; /* some ridiculously large value to make sure it's on top */
      position: fixed;
      top: 0; right: 0; bottom: 0; left: 0;
      background: rgba(0,0,0,.5);
      cursor: pointer;
      text-align: center;
    }
    .lightbox:before { /* strictly for vertical centering of large image */
      display: inline-block;
      height: 100%;
      vertical-align: middle;
      content: '';
    }
    /* add/ remove this class to toggle display */
    .hidden { display: none; }
    .large { /* the large image */
      max-width: 100%;
      max-height: 100%;
      vertical-align: middle;
    }
    

    The JavaScript:

    var g = document.getElementById('gallery');
    
    String.prototype.endsIn = function(suffixes) { /*just to check the extension*/
      for(i in suffixes) {
        if(this.indexOf(suffixes[i], this.length - suffixes[i].length) !== -1)
          return true;
      }
      return false;
    };
    
    g.addEventListener('click', function(e){
      var target = e.target, lnk, ext = ['.jpg', '.png'], lightbox, large;
      if(!target.classList.contains('thumb-img')) return;
      else {
        lnk = target.parentNode.href;
        if(!lnk.endsIn(ext)) return;
        else {
          lightbox = document.getElementById('lightbox');
          if(lightbox == null) {
            lightbox = document.createElement('div');
            lightbox.setAttribute('id', 'lightbox');
            lightbox.classList.add('lightbox');
            lightbox.innerHTML = "<img src='"+ lnk +"' id='large' class='large'>";
            document.body.appendChild(lightbox);
            lightbox.addEventListener('click', function(ev) {
              var target = ev.target, next, links = g.querySelectorAll('.thumb'), 
                  len = links.length, large = document.getElementById('large');
              if(target.id == 'lightbox') lightbox.classList.add('hidden');
              else if(target.id == 'large') {
                for(var i = 0; i < len; i++) {
                  if(links[i].href == large.src) {
                    next = links[(i++)%len].href;
                    while(!next.endsIn(ext)) next = links[(i++)%len].href;
                    large.src = links[i%len].href;
                    break;
                  }
                }
              }
            }, false);
          }
          else {
            lightbox.classList.remove('hidden');
            large = document.getElementById('large');
            large.src = lnk;
          }
          e.preventDefault();
        }
      }
    }, false);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have never used DotNetNuke before. I'm thinking about giving it a try to
I have never used Prototype before. But now when I'm using Rails, it seems
I have never used anonymous functions in PHP before, but I found a piece
I have never used Try-catch in my code before, but now I need to
G'day, I have never really used excel formulas before but need to convert a
I need some help with Oracle's RLS feature. I have never used it before
I have never used list before. I have created a website with over 30
I have never used CRON or anything like that, rails etc.. before, but I
I have never used namespaces for my code before. (Other than for using STL
I have never used Heroku before, but our client wants us to use it.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.