How come every time I click on a different lightbox link, it always shows the same content

I'm currently facing an issue where different contents are supposed to be displayed in different lightbox links, but instead, when I click on any link, only the last content appears in all other lightboxes. It seems like there is some overlapping happening.

<div class="servicos lightbox"> 
    <a><h3> BOX ONE </h3></a> 
</div>
<div class="background"></div>
<div class="box">
    <div class="close">
        <h4>X</h4>
    </div>
    <h1> HERE WE SEE 1° CONTENT </h1>
</div>
<div class="servicos lightbox"> 
    <a><h3> BOX TWO </h3></a> 
</div>
<div class="background"></div>
<div class="box">
    <div class="close">
        <h4>X</h4>
    </div>
    <h1> HERE WE SEE 2° CONTENT </h1>
</div>
$(document).ready(function () {
    $('.lightbox').click(function () {
        $('.background, .box').animate({
            'opacity': '.50'
        }, 500, 'linear');
        $('.box').animate({
            'opacity': '1.00'
        }, 500, 'linear');
        $('.background, .box').css('display', 'block');
    });

    $('.close').click(function () {
        $('.background, .box').animate({
            'opacity': '0'
        }, 500, 'linear', function () {
            $('.background, .box').css('display', 'none');
        });;
    });

    $('.background').click(function () {
        $('.background, .box').animate({
            'opacity': '0'
        }, 500, 'linear', function () {
            $('.background, .box').css('display', 'none');
        });;
    });
});
.background {
    display: none;
    position: fixed;
    z-index: 102;
    width: 100%;
    height: 100%;
    text-align: center;
    top: 0;
    left: 0;
    background-image:url(../imagens/bg_litghbox.gif);
    z-index:105;
    overflow: auto;
}
.box {
    position:absolute;
    width: 70%;
    height:auto;
    background-color:#FFFFFF;
    z-index:106;
    padding:14px;
    border-radius:1em;
    -moz-border-radius:1em;
    -webkit-border-radius:1em;
    box-shadow: 2px 2px 2px #333333;
    -moz-box-shadow: 2px 2px 2px #333333;
    -webkit-box-shadow: 2px 2px 2px #333333;
    display:none;
    overflow:auto;
}
.close {
    float:right;
    cursor:pointer;
}

Answer №1

  1. To effectively target the elements with classes of `.box` and `.background` and apply animations specifically to them, you can use the jQuery method `.nextUntil`.

  2. Another method is to group these elements within a container and then utilize the `siblings()` function in jQuery to select them.

  3. If the styling for the background of each lightbox is consistent, it might be beneficial to have a single element with a class of `.background` positioned at the end of the HTML structure.

$(document).ready(function () {
            $('.lightbox').click(function () {
                var $targets = $(this).nextUntil('.lightbox', '.box, .background');
                $targets.animate({
                    'opacity': '.50'
                }, 500, 'linear')
                
                $targets.filter('.box').animate({
                    'opacity': '1.00'
                }, 500, 'linear');
                
                $targets.css('display', 'block');
            });
    
            $('.close').click(function () {
                $('.background, .box').animate({
                    'opacity': '0'
                }, 500, 'linear', function () {
                    $('.background, .box').css('display', 'none');
                });;
            });
    
            $('.background').click(function () {
                $('.background, .box').animate({
                    'opacity': '0'
                }, 500, 'linear', function () {
                    $('.background, .box').css('display', 'none');
                });;
            });
        });
.background {
            display: none;
            position: fixed;
            z-index: 102;
            width: 100%;
            height: 100%;
            text-align: center;
            top: 0;
            left: 0;
            background-image:url(../imagens/bg_litghbox.gif);
            z-index:105;
            overflow: auto;
        }
        .box {
            position:absolute;
            width: 70%;
            height:auto;
            background-color:#FFFFFF;
            z-index:106;
            padding:14px;
            border-radius:1em;
            -moz-border-radius:1em;
            -webkit-border-radius:1em;
            box-shadow: 2px 2px 2px #333333;
            -moz-box-shadow: 2px 2px 2px #333333;
            -webkit-box-shadow: 2px 2px 2px #333333;
            display:none;
            overflow:auto;
        }
        .close {
            float:right;
            cursor:pointer;
        }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
        <div class="servicos lightbox"> 
            <a><h3> BOX ONE </h3></a> 
        </div>
        <div class="background"></div>
        <div class="box">
        ...
        

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

The challenge of jQuery arises when trying to simultaneously set focus and scrollTop

When attempting to set focus on a text input box using the document ready function, I encountered an issue. The input box is located within a fieldset about 50px down the page, causing the page header to not be visible when the input box gains focus. I t ...

Can HTML be injected into a Knockout custom element?

I am currently developing a custom knockout element and I would like to inject some HTML into it, similar to what can be done with polymer. My goal is to achieve something along the lines of: ko.components.register('hello-world', { template ...

What is the process for changing the background color with Angular?

It should be a straightforward task, but for some reason, it just won't cooperate. Within my controller: $scope.projects = [ //... { background: "#ffffcc" }, //... ]; In the HTML: <div ng-repeat="project in projects" ng-style="{ ...

"Combine elements into groups and calculate their sum using the 'groupBy

Below is an example of an array: let transactions = [{ date: 2019, amount: 3000 }, { date: 2019, amount: 5500 }, { date: 2020, amount: 2300 } ] I am looking to calculate the total amount for each year, resulting in the fo ...

Instability detected in the image when hovering over and off

Here is a snippet of code from my HTML file: <img id="id1" src="photo1" usemap="#ir" > <img src="photo2" id="id2"/> <span> <map name="ir" id="ir"> <area alt="" title="my title" href="my link" shape="poly" coords="13,15,29 ...

Axios failing to retrieve nested data in get request

Having an issue with getting a 2d array using axios in my code. When I try to console.log it, it returns empty. Any suggestions? Here's the piece of code causing trouble: let orig = [] axios .get(<endpoint url here>) .then ...

Convert the provided text into a clickable button

I've been attempting to mirror this function: and: I'm currently utilizing JS and Vue to develop my web application. Any advice on navigating through the current dilemma I find myself in would be greatly appreciated. So far, I have successfully ...

Extracting information from the database using PUG

There is a situation with the data in my mongo database that I can't seem to control. When exporting data from my application to the database, it seems to update the html tags (I believe the term is 'encoding' but I am not entirely sure). &a ...

Omit the tag from the submission section

Currently, I am utilizing a <p> tag as a submit button/area by setting a specific id in jquery. <p id="xyz"></p> My requirement is to insert an input field inside this <p> tag to include a particular value that will be submitted u ...

What is the best way to create a TypeScript interface or type definition for my constant variable?

I'm facing challenges in defining an interface or type for my dataset, and encountering some errors. Here is the incorrect interfaces and code that I'm using: interface IVehicle { [key: number]: { model: string, year: number }; } interface IV ...

Lack of intellisense support for .ts files in Visual Studio Code

Currently, I am using Visual Studio Code 1.17.2 on Arch Linux to kickstart my work with Node.js/Angular4. To avoid confusion caused by loosely typed code, I have decided to switch to TypeScript for my NodeJS server as well. This is why my main file is name ...

What is the best way to create space between floated elements and a cleared element?

I have a webpage with various floated boxes and a footer positioned at the bottom. The issue arises when trying to create space between the boxes and the footer without using margins. #main { max-width: 700px; margin: 0 auto; } .box { width: 46.6 ...

Angular dropdown within another dropdown

I am encountering an issue with a dropdown within another dropdown on my HTML page. The structure is as follows: Dropdown |_ First Dropdown | |_ aaa | |_ bbb | |_ ccc |_ Second Dropdown |_ ddd Even though everything appears ...

Using Sequelize to send data from the client-side to the server-side

I am currently developing a project for a fictional library database and website interface. I am facing an issue where only 2 out of the 4 new loan form inputs are being passed to the req.body. Even though all items have a name attribute, it seems like onl ...

Calculating the mean value of a multidimensional array that has been sorted in JavaScript

Check out the structure of my JSON File below. { "questions": ["Question1", "Question2"], "orgs": ["Org1", "Org2", "Org3"], "dates": ["Q1", "Q2", "Q3"], "values": [ [ [5, 88, 18], [50, 83, 10], ...

What is the best way to incorporate a BigDecimal from Scala.js into my JavaScript project?

Presented below is an example of a Scala object: @JSExportTopLevel("Calculator") object Calculator { @JSExport def calculate(): BigDecimal = 3.14 } The exported singleton method can be invoked from a JavaScript application, but the result m ...

What could be causing my textarea-filling function to only be successful on the initial attempt?

Programming: function updateText() { $("body").on("click", ".update_text", function(event) { $(this).closest("form").find("textarea").text("updated text"); event.preventDefault(); }); } $(document).ready(function() { updateText(); }); H ...

Learn how to bypass the problem of self-signed certificates in request-promise

In my node application, I am utilizing the request-promise module to make API calls. You can find more information about it here. import request from 'request-promise'; let options = { method: GET, json: true, ...

The data from the cascading drop-down list cannot be shown at this time due to a server error (500)

Having trouble with your cascading dropdown list project that uses asp.net, jQuery and ajax? Encounter a 500 error (Internal Server Error)? Need some assistance, please! :( https://i.sstatic.net/QrFVA.png Check out the function in my controller below: ...

Having trouble with Jquery CSS transform not functioning properly in Internet Explorer 8?

When it comes to utilizing CSS3 features that are not supported by older browsers such as IE8, I tend to apply them using jQuery instead. However, I have noticed that the CSS transform in jQuery does not work... Here is my code: http://codepen.io/vincentc ...