Adjust the z-Index of the list item element

I am attempting to create an effect where, upon clicking an icon, its background (width and height) expands to 100% of the page. However, I am struggling with ensuring that the 'effect' goes underneath the this.element and above everything else. I have experimented with changing the position and zIndex properties, but it seems working with a list is a bit more complex.. Below is the code snippet along with a demo.

Html

     <ul>
        <li style="z-index:1;">
            <img src=".png" style="width:100px; height:100px; background-color:#11D1AC;">
        </li>
        <li style="z-index:1;">
            <img src=".png" style="width:100px; height:100px; background-color:#787FA7;">
        </li>
        <li style="z-index:1;">
            <img src=".png" style="width:100px; height:100px; background-color:#FF7059;">
        </li>
    </ul>

Js

$('li').on('click', function () {
            var elementoPos = $(this).find('img').position();
            $(this).parent().css("zIndex", 3);
            var elementColor = $(this).find('img').css('backgroundColor');


            var square = document.createElement('div');
            $(square).css({
                "position": "absolute"
                , "zIndex": "2"
                , "width": "50"
                , "height": "50"
                , "left": elementoPos.left + 50 / 2
                , "top": elementoPos.top + 50 / 2
                , "backgroundColor": elementColor
            });
            $('body').append($(square));

            $(square).animate({
                "width": "100vw"
                , "height": "100vh"
                , "left": "0"
                , "top": "0"
            }, function () {
                $(square).animate({
                    opacity: 0
                }, 1500);
            });
            //$('ul').slideUp('slow');

        });

Check out the demo: https://fiddle.jshell.net/w4f3aj1z/6/

Answer №1

The issue was caused by a dynamically created div.

To resolve this, consider using $(square).remove(); at the end of the animation.

$(document).on('click','li', function () {
            var elementoPos = $(this).find('img').position();
            $(this).parent().css("zIndex", 3);
            var elementColor = $(this).find('img').css('backgroundColor');


            var square = document.createElement('div');
            $(square).css({
                "position": "absolute"
                , "zIndex": "2"
                , "width": "50"
                , "height": "50"
                , "left": elementoPos.left + 50 / 2
                , "top": elementoPos.top + 50 / 2
                , "backgroundColor": elementColor
            });
            $('body').append($(square));

            $(square).animate({
                "width": "100vw"
                , "height": "100vh"
                , "left": "0"
                , "top": "0"
            }, function () {
                $(square).animate({
                    opacity: 0
                }, 1500, function() { $(square).remove(); });

            });
            //$('ul').slideUp('slow');

        });

Answer №2

To make the adjustment you desire, simply switch from using

$(this).parent().css("zIndex", 3);
to employing this code instead:
$(this).css({"z-index": 3,"position":"relative"});
. I hope this meets your specifications.

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

Possible revision: "Methods for updating Bootstrap language settings?"

Currently, I am working on a project using ASP.NET Core in combination with Bootstrap. Recently, I successfully integrated localization support by following the guidance provided in this documentation. However, I encountered an issue where the Bootstrap fr ...

What steps should be taken to manage expired sessions with the combination of spring-security and jQuery?

In my application, I am utilizing spring-security and jQuery. The main page loads content dynamically into tabs through Ajax. Everything seems to be functioning correctly, but occasionally the login page pops up inside one of the tabs. When I enter my cred ...

What is the procedure for iterating through the square brackets of a JSON array?

Here's the data I have: $json_data_array = '[ { "id": 1, "value": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bfd7cdffcbdacccb91dcd0d2">[email protected]</a>", ...

Divergent behavior of jQuery AJAX when used with a standard form versus when called in a popup within a div

Why does the jQuery AJAX work perfectly when submitting a form using the normal method, but not when submitting the same form within a popup? Below is the code for reference. Form submission script: $("#submit").click(function (e) { /* $('form&a ...

Error: Attempting to access the 'HotelInfo' property of an undefined variable led to an uncaught TypeError

//initiating an AJAX request to access the API jQuery(document).ready(function() { jQuery.ajax({ url:"http://localhost:8080/activitiesWithRealData?location=%22SEA%22&startDate=%2205-14-16%22&endDate=%2205-16-16%22&a ...

What is the process for triggering property decorators during runtime?

Wondering about dynamically invoking a property decorator at runtime. If we take a look at the code snippet below: function PropertyDecorator( target: Object, // The prototype of the class propertyKey: string | symbol // The name of th ...

I'm encountering an issue with implementing a basic "remember me" feature on my PHP login page

I am in the process of implementing a basic "remember me" feature in my login.php file: <?php session_start(); if(isset($_SESSION["comp"])){ header("location view.php");//to redirect to the view page if already logged retur ...

Encountering a 422 ERROR while attempting to send a POST request

Below is the code snippet I am currently using: const url = new URL("https://api.chec.io/v1/products"); const headers = { "X-Authorization": `${process.env.NEXT_PUBLIC_CHEC_PUBLIC_KEY_SECRET}`, "Accept": "appl ...

Ways to restrict the maximum length in Draft.js

Is there a way to control the maximum number of characters in draft js? I know how to get the length of the state, but is there a method to prevent the component from being updated past a certain point? var length = editorState.getCurrentContent().getPla ...

Is there a way to dynamically fetch and run JavaScript code from the server without resorting to the use of the

In my current project, I am developing a unique PHP framework that empowers PHP developers to effortlessly craft ExtJS interfaces containing forms, grids, tabpanels, and menus exclusively through PHP classes. To illustrate, creating a TabPanel in this fra ...

Struggling to connect CSS to HTML on Github Pages

I'm new to using Github, and I noticed that when I open the website, my HTML code is displaying without the associated CSS. Here is a snippet of my HTML code: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

Can Ajax be utilized to call a PHP script that contains another Ajax function?

I have set up a checkbox that triggers an Ajax call to another PHP script once checked. In this PHP script, there are three text boxes and a button. When the button is pressed, another Ajax call is made to execute yet another PHP script, all within the sam ...

Tips for preserving drop down selections when refreshing the page

I am currently working on a program with 2 drop-down menus and 2 buttons. My goal is to have the first button disabled and second enabled when both dropdowns are selected and the "start" button is clicked. Additionally, I want the dropdowns to be disabled ...

Attempting to display numerous react components on various divs instead of just one ID

Currently, I am facing a challenge in creating multiple react comment boxes. The package I am using is based on #id instead of class, which resulted in only rendering one box instead of all. To resolve this issue, I attempted to switch from GetelemntbyID t ...

Scheduled tasks on Google Cloud Platform's App Engine are failing to run over the weekend

I am facing an issue with running a cron job in my node/express application using the node-cron library. The application is hosted on Google Cloud App Engine. My aim is to automate sending emails every day at 9 AM, but the cron seems to only work from Mon ...

What is the best way to include an API key in the response to an Angular client application?

I'm working on transferring my API key from NodeJS to an Angular client application using $http, but I am unclear on the approach. Below is a snippet from my NodeJS file: // http://api.openweathermap.org/data/2.5/weather var express = require(' ...

authorization for certain express routes using passport.js

Securing certain express routes with Passport.js authentication Steps for authenticating specific routes in Passport.js Looking for a method to authenticate particular routes using Passport.js Your assistance is greatly appreciated... ...

Initiate a click on a radio button while also retaining the selected option when the page is

This is a unique question. In my scenario, there are two radio buttons: "radio1" and "radio2." I have successfully implemented the following actions individually: Automatically triggering a click on "radio1" upon page load. This ensures that the button ...

JavaScript or jQuery for filtering table rows

I have limited experience with JavaScript and jQuery, but I am working with a table containing various entries. My goal is to implement a filtering system using a list on the left side of the table. Here is an example I put together: http://jsfiddle.net/B ...

Learn how to implement JavaScript code that allows a video to start playing only upon clicking a specific button

Within the confines of a div lies a <video autoplay> <source src='myvid'> </video>. This div initially has a display ='none' setting in its CSS. Upon receiving a click event, the display property changes from none to b ...