Include a border around the image on the overlay, along with a close button or link

Looking for some help with my code that opens images in an overlay div. I am struggling to add a border to the image object, as it only displays the raw image without any border. How can I modify this code to include a border around the image and also add a close button at the top right corner?

$(document).ready(function(){
    $('a.lightbox').click(function(e){

        $('body').css('overflow-y',' hidden');

        $('<div id="overlay"></div>')
        .css('top', $(document).scrollTop())
        .css('opacity', '0')
        .animate({'opacity': '0.6'}, 'slow')
        .appendTo('body');

        $('<div id= "lightbox"></div>')
        .hide()
        .appendTo('body');

        $('<img>', {
            src: $(this).attr('href'),
            load: function() {
                positionLightboxImage();
            },
            click : function() {
                removeLightbox();
            }
        }).appendTo('#lightbox');
        return false;
    });
});

function positionLightboxImage() {
  var top = ($(window).height() - $('#lightbox').height()) / 2;
  var left = ($(window).width() - $('#lightbox').width()) / 2;
  $('#lightbox')
    .css({
      'top': top + $(document).scrollTop(),
      'left': left
    })
    .fadeIn();
}

function removeLightbox() {
  $('#overlay, #lightbox')
    .fadeOut('slow', function() {
      $(this).remove();
      $('body').css('overflow-y', 'auto'); // show scrollbars!
    });
}


<a class="lightbox" href="<?php echo $row_searchHH['imageURL']; ?>"><img src="<?php echo $row_searchHH['imageURL']; ?>" width="90" height="90" style="margin-left:10px" /></a>

Answer №1

Enhance this block with a stylish border:

  $('#lightbox')
    .css({
      'top': top + $(document).scrollTop(),
      'left': left
    })

For instance:

  $('#lightbox')
    .css({
      'top': top + $(document).scrollTop(),
      'left': left,
      'border': '2px solid black'
    })

Answer №2

Take a look at this fiddle for a demonstration If you decide to place the img inside the div then you can customize the style of the image

CSS

.imageclass{
width:98%;
height:98%;
    position:absolute;
    left:1%;
    top:1%;
}
.parentdivclass{
background:rgba(15, 15, 15, .5);
    width:200px;
    height:200px;
    position:absolute;
}
.closebuttonclass
{
left:95%;
top:0.2%;
    width:5%;
    height:5%;
    position:absolute;
}

You are able to position your close button image on the top right corner of your div !!

Therefore, your final div structure should resemble this

HTML

<div class='parentdivclass'>
<img class='imageclass' src='' />
<img class='closebuttonclass' src='' />
</div>

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

Retrieve all findings related to an ongoing loop update issue within react-table

Check out this Playground Sandbox where custom hooks for react-table have been set up to allow for customized search and row selection features. However, after selecting a single row, some unexpected behavior occurs, including the select All option not f ...

Using Angular to create a dynamic form with looping inputs that reactively responds to user

I need to implement reactive form validation for a form that has dynamic inputs created through looping data: This is what my form builder setup would be like : constructor(private formBuilder: FormBuilder) { this.userForm = this.formBuilder.group({ ...

Explore and Conceal Images with Jquery

Currently, I am utilizing jQuery to showcase images prior to uploading. However, when newer files are uploaded, I wish to either remove or conceal the previous upload files. Here is my jQuery script: $(function() { // Allowing multiple image ...

Can you outline the distinctions between React Native and React?

Recently delving into the world of React sparked my curiosity, leading me to wonder about the distinctions between React and React Native. Despite scouring Google for answers, I came up short on finding a comprehensive explanation. Both React and React N ...

The Nivo Slider is stealthily concealing my CSS Menu

Although this question has been previously asked, I am still unable to make it work with the solutions provided; The website in question is I have tried changing the z-index on all menu items to 99999, but they are still appearing below. <div id="sli ...

Managing state in React hooks: Updating nested object state using useState()

In my application, there is a component that is passed a prop in the following structure: const styles = { font: { size: { value: '22', unit: 'px' }, weight: 'bold', ...

php generate a new file within a specific folder

In the following code snippet, a check is performed for the existence of a directory named 'dat'. If the directory does not exist, it is created. This functionality works correctly; however, the goal is to write a file to this directory that can ...

The identity becomes blurred once information is transmitted to a modal

I'm having an issue with a recipe table that belongs to a specific user. When the pencil icon on each row is clicked, a modal should display, showing the recipe details and allowing the user to edit and save the updated version. However, I've enc ...

The challenges of updating AngularJS partial templates and handling refresh in 2-way binding

I've encountered an issue with a partial template that's loaded outside of my ng-view, complete with its own controller. Here's a breakdown. Basic template layout <html ng-app="myApp"> ... <div ng-include src="'myPartia ...

What is the process for combining JSON objects using the "id" key as the basis for merging

Having this json structure, my goal is to merge it based on the articleId. [{ "articleId": "45", "isA": false, "flags": { "isDema": fals ...

When working with HTML, how can you prevent the scrollbar from automatically scrolling to the bottom when new data is continuously added to a <textarea> element using AJAX calls?

Is there a way to prevent a textarea on an HTML page from automatically scrolling to the top when new data is entered, causing inconvenience for manual scrolling? ...

Using jQuery to reveal or conceal content based on a particular class

Being a beginner in jquery, I have a question that might seem silly but I've searched everywhere for an answer without success. So, here it goes: I'm trying to display different content based on the option selected in a dropdown form. According ...

Unable to detect hover (etc) events after generating div elements with innerHTML method

After using the code below to generate some divs document.getElementById('container').innerHTML += '<div class="colorBox" id="box'+i+'"></div>'; I am encountering an issue with capturing hover events: $(".colorB ...

Please only choose the immediate children of the body element

<body> <div> <div class='container'></div> </div> <div class='container'></div> </body> Is there a way to use jQuery to specifically target the second container div dire ...

Utilize various CSS styles for text wrapping

I'm struggling to figure out where to begin with this problem. My goal is to create a three-column row that can wrap below each other if they cannot fit horizontally. The height of the row should adjust to accommodate the items while maintaining a fix ...

Tips on setting a background image to a div element

I'm facing an issue with a div setup like this: https://i.sstatic.net/4nuyO.png My goal is to remove a specific section of the circle shape displayed below: https://i.sstatic.net/09IWX.png The desired outcome is to achieve a final shape similar to ...

Learn how to dynamically change an image when a list item is clicked using only CSS!

When I click on an object, I want to change the image to a different one. The code is organized as follows: HTML : <div class="games-platform-item pt-item"> <ul class="games-sub-menu clearfix"> ...

Suggestions for displaying images of varying resolutions in a circular format similar to Google's style

What is the best way to display dynamic images with varying resolutions in a circle like Google? I have tried using the Bootstrap class "img-circle", but it only works for images with a resolution of 304*236. If the image resolution is different, it appear ...

The innerHTML inside Angular components appears scrambled, with only the final innerHTML rendering correctly

When working on my Angular project (version 8), I encountered an issue where a list of static HTML content retrieved from a database is not rendering correctly in the parent HTML. Strangely, only the last div with innerHTML is being rendered correctly, whi ...

Can the color of the expanded navigation bar in Bootstrap 4 be customized?

I'm currently in the process of developing a Bootstrap website and was wondering whether it is feasible to add a background color to the expanded navbar specifically on small screens. Feel free to refer to the images below for a visual representation ...