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 7658557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:13:03+00:00 2026-05-31T13:13:03+00:00

I am making an image viewer. When I click on an image, it fades

  • 0

I am making an image viewer. When I click on an image, it fades in the “overlay” Div with the image inside. But with the code below, when I click on the image, the div fades in but it immediately fades out without me re-clicking. What is wrong with me code?

I believe I need to put that “body click” line inside the first function but it doesn’t work. How can I make such that this “body click” happens only after the overlay fades in?

Thanks in advance.

$('.pic').click(function(){
        $('#overlay').html("<img class='enlarged_pic' src='" + (this).src + "'>").fadeIn();
        }); 

$('body').click(function(){
        $('#overlay').fadeOut();
        });     
  • 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-05-31T13:13:04+00:00Added an answer on May 31, 2026 at 1:13 pm

    You need to add e.stopPropagation() to your first handler. What happens is that the click event is bubbling from your .pic up to the body, triggering both handlers when you only want one. .stopPropagation() blocks the event from bubbling any higher up the DOM.

    $('.pic').click(function(e){ // don't forget that 'e'
        e.stopPropagation();
        $('#overlay').html("<img class='enlarged_pic' src='" + (this).src + "'>").fadeIn();
    }); 
    
    $('body').click(function(e){
        $('#overlay').fadeOut();
    });  
    

    http://jsfiddle.net/VQkNa/

    You may also want to make it so that clicking on the overlay itself doesn’t fade the overlay. This is easily done by detecting the target of a click:

    $('body').click(function(e) {
        if (e.target.id !== "overlay") {
            $('#overlay').fadeOut();
        };
    });​
    

    http://jsfiddle.net/VQkNa/1/

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

Sidebar

Related Questions

I'm making a simple Image Debugger Visualizer. Code is below. I'm not sure if
I'm making an image viewer using openGL and I've run into a situation where
I've been making an image uploader and I'm having a couple of issues. Code
I am making div with image and text. User can hover on this div
I'm doing an image viewer that pass pictures to next or previous, by clicking
I stubled upon a weird problem. The following code results in making the image
I'm making a image uploader but I have come across 2 errors. Which are:
I need help with making an image float onto the canvas in HTML5 and
How do I go about making an image or section of the page full
I'm looking for the fastest way of: merging (it means making one image from

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.