Designing a preview feature for gallery images that automatically closes when clicked anywhere else

I'm currently working on a preview box that appears when you click on a gallery image. However, I'm facing an issue with it not disappearing when clicking outside of the box. I have tried multiple solutions without success. It seems like a while loop might be needed, but all my attempts resulted in infinite conditions.

The latest solution I attempted works for the preview display, but the closing functionality is still not functioning correctly.

Here is a DEMO

$('.portPic').click(function() {
  if ($(this).attr('data-src2')) {
    $('#clickedImg').attr('src', $(this).attr('data-src2'));
  } else {
    $('#clickedImg').attr('src', $(this).attr('src'));
  }
  $('#clickedImg').css('visibility', 'visible');
  $('#clickedImg').blur(function() {
    $('#clickedImg').css('visibility', 'hidden');
  });
});

Answer №1

I have implemented a similar feature with a pop-out menu that closes when the user clicks outside of it. This same concept can be applied in this scenario as well.

In my implementation, I utilized an overlay div that covers the entire screen with a slightly translucent opacity, creating a modal effect. You can assign an id to this overlay - for example, modal-overlay.

You can include this overlay statically in your page code, set its display property to none, and make it full-size using a CSS class.

<div id="modal-overlay" class="full-screen-overlay"></div>

Ensure the overlay has a higher z-index than the rest of your page, and your popup has a higher z-index than the overlay. When displaying the popup, also set the visibility of the modal-overlay to visible.

In your script code, add an event handler for when the modal div is clicked:

$('#modal-overlay').click(function() {
        $('#clickedImg').hide();
        $('#modal-overlay').hide();
    })

It's recommended to use the jQuery method .hide() for simplicity instead of manually setting visibility.

If you have multiple actions involved (like in a modal overlay), consider encapsulating the "show/hide" functionality in a method like hidePopup() or closePopup() and call it onClick to avoid code duplication.

For additional visual effects when opening the popup/overlay, you can utilize jQuery animations such as .fadeIn() or .slideDown(). Use fadeOut() and slideUp() to hide them.

These animations handle both showing and hiding, so there's no need to explicitly call .hide() or .show().

Check out this link to explore more on jQuery's animation capabilities. It's very useful and informative.

I hope this information proves helpful!

Answer №2

To properly display a pop-up image, consider creating an additional div with a fixed position just below the main image layer. This second div should have a lower z-index than the pop-up image. Add a click handler to this overlay div and handle your show/hide functions within it.

Answer №3

If you want to showcase your photos in a stylish way, consider using a modal photo gallery. Check out this resource for more information. You can also find helpful code examples on Codepen by searching "Bootstrap Gallery with Modal and Carousel."

I hope this suggestion proves useful!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

On the PostAd.js page, I am looking to remove the location bar, search bar, and sell button - only keeping the

I am looking to customize the PostAd.js page by displaying only the logo and hiding the location bar, search bar, and sell button. I have included my code below for reference. As a newcomer learning React.js, I am striving to showcase just the logo on the ...

Executing a Javascript function through Typescript in an Ionic application

I integrated a plugin into my ionic project, which includes both Java and JS code: cordova.define("cordova-sms-plugin.Sms", function(require, exports, module) { 'use strict'; var exec = require('cordova/exec'); var sms = {}; functio ...

Is it possible for a div nested within another div to still occupy space on the page despite being set

<html> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <script type="text/javascript"> $("select#choosebook").change(function(){ $(".title").slideDown(" ...

Adjustable columns that fill the rest of the screen's height below a navigation bar

I am facing an issue with my non-fixed navbar and two-column layout when testing the responsiveness on a mobile screen size. I need Column A to fill the remaining viewport's height, while Column B should move under Column A without hardcoding any valu ...

Encountering an error when trying to retrieve data from a three-dimensional array

I tried to implement a three-dimensional array in JavaScript, but I encountered an error message in Chrome: Error: Uncaught SyntaxError: Unexpected token [ This is the snippet of my JavaScript code: function ThreeDimensionalArray(iRows,iCols,iHig) { ...

"What is the best approach to adjusting the width of one div based on the width of another div using CSS Grid

As a beginner in programming, I'm trying to work with CSS Grid but facing some challenges. My goal is to create a component with two columns: the left column should have a width of minmax(570px, 720px), and the right column should be minmax(380px, 10 ...

Tips for preventing a dynamic textbox from shifting other elements as it expands

Trying to explain the question clearly, I have a table with a textbox at the bottom where users can expand it by grabbing the corner. However, when they do so, other fields also move along with it. The setup of this form in a two-column table was inherited ...

Is it possible to utilize AngularJs Directives in order to add a styling to a pseudo Element?

As I continue my journey to learn Angular, a roadblock has appeared while trying to create a directive. My goal is to construct a Directive that extracts the URL from a data-attribute ('background-image') and uses it as a background-image for a ...

Send an Ajax request to the controller and retrieve a file to display in an iframe

I need to display the contents of a byte[] array in an Iframe and provide the option for the user to download the file in PDF format. My view has a button connected to an ajax call that sends the byte[] to my actionresult, which returns a FileContentResul ...

Laravel 4 Data Cleaning for Improved Security

I am currently utilizing Laravel 4 to establish a RESTful interface for my AngularJS application. Right now, I am looking to update an object within my model named Discount Link. The method I am using involves: $data = Input::all(); $affectedRows ...

Initiating an audio call exclusively via SimpleWebRTC

I'm currently utilizing a jQuery plugin for WebRTC found here. My goal is to initiate an audio call only, but I am struggling to find a function within the plugin that allows for this. The code snippet I am using with autoRequestMedia set to false is ...

How to style a list item without a sublist using CSS

Is there a way in CSS to target list items that do not contain any unordered or ordered lists? The list item may have an anchor element within it. I am aware of how to achieve this using jQuery, but I am seeking a solution using only CSS or CSS3. ...

What are the best methods for cropping SVG images effectively?

Is it possible to crop a large SVG background that has elements rendered on top of it so that it fits the foreground elements? I am using svg.js but have not been able to find a built-in function for this. Can an SVG be cropped in this way? ...

The blur function in jQuery is not functioning as expected

I'm facing an issue where only the first of my 3 .blur's is working. Here is the jQuery code snippet: <script type='text/javascript'> $(document).ready(function() { $("#user_name").blur(function() { if ($( ...

What are the steps to modify the MIME type in order for the browser to correctly display a CSS

I'm encountering an issue where my CSS code is not rendering in the browser, and it seems to be related to MIME types (which I'm not familiar with). I am using EJS as a template engine. The error I see in the console is... Refused to apply sty ...

Storing user feedback in database using ajax

I have encountered a common issue in this thread, and although it has been discussed multiple times, I am still unable to solve my problem. My struggle lies in sending and fetching data from comment.php to insert.php. Below is the code from my comment.php ...

What is the best way to save images using Backand and Ionic?

What is the best method for saving profile pictures within the Users object? ...

Avoiding uib-popover from getting clipped by uib-carousel

I have a small widget with a popover that works fine when placed inside a div, but gets clipped when inserted into a carousel. Here's an example to illustrate what I mean: (The top image shows the widget in a div without clipping) https://i.sstatic. ...

Modify Bootstrap modal content with a click event

I have implemented a bootstrap modal on my website, but I am looking to completely change the content of the modal. Specifically, when I click a button within the modal, which is represented by <h4><a class="btn btn-default" style="color:#0B610B;" ...

"Successfully implementing AJAX POST functionality, but encountering issues where callbacks are not triggering

I have gone through numerous posts addressing this issue and attempted various solutions, however, none seem to work for me. My AJAX POST function correctly adds the email to my mailing list, but the success and error callbacks are not consistently firing, ...