Adjusting the settings on my lightbox

Recently, I created a basic lightbox with the following code snippet:

Here is the jQuery code:

var $overlay =$('<div class="overlay"></div>');

$('body').append($overlay);

$('img').click(function(){
$overlay.show();
});

And here is the associated CSS:

   .overlay {
    width: 100%;
    height:100%;
    background:grey;
    display:none;
position: absolute;
top:0;
    }

It's quite simple as I have only written code for the overlay that appears when an image is clicked and triggers the lightbox.

Now, I have a couple of questions regarding this setup:

  1. If my webpage is longer than the screen, what code should I use to prevent scrolling when the lightbox is activated?

  2. Is there a way to adjust the size of the lightbox $overlay so that it only fills the visible part of the screen? I have images spread throughout the webpage and want the lightbox to fill only the current viewport when triggered.

Answer №1

After careful consideration, I have decided to share my insights with you.
Let's start with your JavaScript code. It seems like you have created separate .overlay elements for each image, which is unnecessary.
Instead, you can simplify it by creating a single overlay as follows:

<div class="overlay"><img src="#" width="500"/></div>

Then, when an image is clicked on your webpage, dynamically set the image source in the overlay like this:

$('img').click(function(){
    var src = $(this).attr('src');
    $('.overlay').fadeIn();
    $('.overlay img').attr('src',src).css('margin-top',($(window).height() - $('.overlay img').height()-20)/2);
});
$('.overlay').click(function(){
    $(this).fadeOut();
});

Now let's move on to addressing your main question.
To achieve the desired effect, you need to utilize CSS.
Start by setting the height and width of the body and html to 100%, and remove any margins to prevent scroll bars from appearing:

body, html{
    width:100%;
    height:100%;
    margin:auto;
}

Next, change position:absolute; to position:fixed; to make the overlay appear over the clicked image:

.overlay {
    width: 100%;
    height:100%;
    background:grey;
    display:none;
    position: fixed;
    top:0;
    text-align:center;
    cursor:pointer;
}

Add some additional CSS styles to enhance the appearance:

.overlay img{
    border-radius:5px;
    border:10px solid white;
    box-shadow: 0 0 5px black;
}

And voila!

Check out the JSFiddle Demo for a visual representation

Make sure to explore the code in this JSFiddle for a better understanding

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

I want to create a form with two dropdown menus placed side by side within a parent DIV, and I want them to collectively occupy the entire width using Bootstrap

Currently, I am working on a search form using Bootstrap. Everything is going well, but I am facing an issue where two multi-option elements need to be placed side by side and together occupy 100% of the width within their containing DIV. Just for clarity ...

Adjust the parent div's background when hovering over it

Here is the code snippet I am working with: <div class="thumb_image_holder_orange"> <img src="http://dummyimage.com/114x64/000/fff.png" /> </div> I want to change the background of the div when hovering over the image, rather than j ...

Transfer information to a php file through ajax

I'm facing an issue with receiving data sent via AJAX in a PHP file. The AJAX request seems to be sending data successfully as I receive a success message, but the PHP page shows an error "Notice: Undefined index: ab". Below is my jQuery code for sen ...

Developing single-page application frontends without utilizing node or npm

As a backend java developer with experience in spring boot, I am now exploring the world of single page applications. One aspect that appeals to me about SPA frameworks, particularly Vue, is things like model-binding and the use of components and template ...

"Troubleshooting the alignment problem in a Bootstrap 5 inline form

I am struggling to create a compact inline form on a web page featuring search filters and two buttons for submitting and resetting. Whenever I position the form fields above the buttons, they become misaligned. How can I ensure that everything stays in- ...

Clickable Parent URL in Bootstrap 5 Dropdown

I'm currently working on creating a Bootstrap 5 Navabar with a dropdown that appears on hover and allows the parent item to be clickable on screens larger than 992px. Successfully implementing the show on hover feature with the following JavaScript: ...

Sharing information between different components in React can be done using props, context, or a

When attempting to edit by clicking, the parent information is taken instead of creating a new VI. I was able to achieve this with angular dialog but I am unsure how to do it with components. This is done using dialog: <div class="dropdown-menu-item" ...

Transform Dynamic Array to JSON structure

I am currently developing a feature in my SvelteKit application that allows users to create custom roles for themselves. Users can input a role name and add it to an array, which is then displayed below. https://i.stack.imgur.com/oUPFU.png My goal is to ...

Receiving "this" as an undefined value within the TypeScript class

Currently developing a rest api using express.js, typescript, and typeorm. Initially thought the issue was with AppDataSource, but it seems to be functioning properly. Below is my controller class: import { RequestWithBody } from '@utils/types/reque ...

Issue: Unable to locate file 'socket.io-client/dist/socket.io.min.js'

Attempting to set up the Spika backend (node application), I ran into an issue while trying to start the server in standalone mode with the command: $ node src/server/main.js The error that popped up was: Error: Cannot find module 'socket.io-clien ...

Tips for saving a file to a mapped path in an ApiController using ASP .NET MVC4

I am currently working on a project that involves uploading a file with a progress bar. I have successfully implemented the progress bar functionality. The challenge I am facing is how to handle the file upload in segments within my ApiController. Below is ...

Methods used on a server and methods used on a client-side application

In my ASP.NET application using C# 2.0, I have created objects for a database with properties that can be called natively or through a RESTful JSON API. These objects are used to convert data into HTML for display on the website. On the site, there are se ...

Why can't the file upload button locate its CSS styles?

Check out this jFiddle demonstrating the issue. Some of the Angular code may appear broken in the example, but that's just due to it being pasted into jFiddle; it's not an actual problem I'm facing. The CSS styles aren't working on the ...

Invisible Dropdown Feature in Google Places Autocomplete

On my website, I have a Google Places autocomplete feature that seems to be fully functional, but it is not visible. Surprisingly, I know that it is working because when I type in "123 Main Street" and then navigate down using the arrow key and press enter ...

Show the JSON data retrieved from an AJAX request in an HTML table

After receiving the JSON response below from an AJAX call: {"items":[{"name":"MP 201SPF_1C","productNo":"123","commerceItemQty":1,"price":"$350.00","leaseOrNot":"false"},{"name":"MP 201SPF_1C","productNo":"456","commerceItemQty":4,"price":"$1,400.00","lea ...

When attempting to add an option to the fieldset in jtable and Java servlet, the entire column fails to display

Just started using jQuery jTable. I needed to include a dropdown menu in one of my jTable fieldset. However, after adding the dropdown feature which retrieves data from the database through my servlet, the entire column disappeared. I've attached a s ...

Both axios post request and JQuery $.ajax request are resulting in a 403 forbidden error when accessed from a mobile device

I am currently developing an application using Vue, wherein I am utilizing axios for making HTTP rest-api calls. Beneath is the code snippet: <!DOCTYPE html> <html> <head> <title>Vue Axios Example</title> </head&g ...

My navbar in bootstrap is having trouble staying aligned with the button and search box. What can I do to ensure everything stays in line?

<nav id="NavbarMaster" class="navbar NavCompression"> <!-- style="min-height:100px;" --> <div id="navToggler" class="container-fluid TitleBG TitleHeight"> <div id="navopener" class="Pointer" style="position:fixed;top:0;left:0; ...

Switch from using fetch to utilizing axios for making API requests

I used fetch for a 'get' request, but now I want to switch to axios. Can someone please help me convert this code to use axios instead? More details: I am fetching an image and using the blob method to display it to the user. I would like to ach ...

Try utilizing a variety of background hues for uib progressbars

Looking to incorporate the ui-bootstrap progressbar into my template in two different colors, background included. My initial idea was to stack two progress bars on top of each other, but it ended up looking too obvious and messy, especially in the corner ...