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

  • Home
  • SEARCH
  • 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 8786705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:42:23+00:00 2026-06-13T21:42:23+00:00

I am trying to implement Lighbox effect on click of a Image using below

  • 0

I am trying to implement Lighbox effect on click of a Image using below code

        document.addEventListener('click',function(){
        var target = event.target || event.srcElement;
        if(target.className == "acv"){
            var id = target.id, src= target.src, 
            type = target.nodeName, imgW, imgH,
            temp = document.createElement('div'),
            sHeight = screen.availHeight,    
            sWidth = screen.availWidth,
            st = temp.style;
            var img = new Image();
            img.onload = function(){
                imgW = this.width;
                imgH = this.height;
            }
            img.src = src;
            var imgH = ( sHeight >= imgH + 20 ? imgH : (sHeight - 20) );
            var imgW =  ( sWidth >= imgW + 20 ? imgW : (sWidth - 20) );
            img.style.height = imgH;
            img.style.width = imgW;
            img.style.position = "relative";
            img.style.left = ( (sWidth - imgW) / 2 ) + "px";
            img.style.top = ( (sHeight - imgH) / 2 ) + "px";
            temp.appendChild(img);
            temp.id = "lightbox";
            st.width = sWidth + "px";
            st.height = sHeight + "px";
            st.backgroundColor = "Gray";
            st.zIndex = 123;
            st.position = "absolute";
            st.top = st.left = 0;
            st.margin = "0 auto";
            document.body.appendChild(temp);
        }
    });​

Please check the working fiddle here.

And I feel like I have done something wrong in this. Please correct and help me to improve the code.

  • 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-13T21:42:24+00:00Added an answer on June 13, 2026 at 9:42 pm

    Seems, if image already loaded onload event not fires, also it is mistake to try to use ImgW and ImgH initialised in event body to use outside of scope of event handler, at least, you shold also “fix” img in event handler, so far here the code which work fine for the fiddle:

    document.addEventListener('click', function() {
        var target = event.target || event.srcElement;
        if (target.className == "acv") {
            //     debugger;
            var id = target.id,
                src = target.src,
                type = target.nodeName,
                imgW, imgH, temp = document.createElement('div'),
                sHeight = screen.availHeight,
                sWidth = screen.availWidth,
                st = temp.style;
            var img = new Image();
            img.src = src + '?rand=' + Math.random();
            //HIDE before we sure that image loaded!
            img.style.display = "none";
            var pos_img = function(img) {
                var imgH = img.height,
                    imgW = img.width;
                imgH = (sHeight >= imgH + 20 ? imgH : (sHeight - 20));
                imgW = (sWidth >= imgW + 20 ? imgW : (sWidth - 20));
                img.style.height = imgH;
                img.style.width = imgW;
                img.style.position = "relative";
                img.style.left = ((sWidth - imgW) / 2) + "px";
                img.style.top = ((sHeight - imgH) / 2) + "px";
                //SHOW IMAGE BACK
                img.style.display = "block";
            };
            //image not loaded?
            if (img.complete === false) {
                img.addEventListener('load', function(e) {
                    console.log("LOAD", e, img);
                    //TODO remove EventListener ?
                    pos_img(img);
                });
            } else {
    
                pos_img(img);
            }
            temp.appendChild(img);
            temp.id = "lightbox";
            st.width = sWidth + "px";
            st.height = sHeight + "px";
            st.backgroundColor = "Gray";
            st.zIndex = 123;
            st.position = "absolute";
            st.top = st.left = 0;
            st.margin = "0 auto";
            document.body.appendChild(temp);
    
        }
    });​
    

    also i would to use window width/height for calculate positions.

    please note that the code above not give you production code, it is just to show where is logic mistake.
    you may be not ask the question if try to click on jslint button

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok well I'm trying implement something similar to the 'undo' function in many image
I am trying implement the Data transformation using Reflection 1 example in my code.
I'm trying implement server code of Server-Sent Events in a generic way that any
I'm trying implement a bracket in my program (using C#/.NET MVC) and I am
im trying to implement the recursive Ackermann-Peter-Function in x86 NASM-Assembly. The Function is defined
I am trying implement Unblock me Puzzle. i want to change image position from
I'm trying implement a way to recursively template using jsRender. The issue is, my
SetFocus I'm trying implement the above Se Focus code in a Class Library that
Trying to implement this gallery on my website. http://coffeescripter.com/code/ad-gallery/ It is noted in the
Found a really nice code for Accordion nav and am trying implement on our

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.