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

Moving through content on a single page

import React, { Component, useState } from "react"; import { Content, List, ListItem, Left, Right, Icon, Container, Header, Body, Button, Title, } from "native-base"; //Chapter One expor ...

Stop Caching with Jquery Infinite Scroll (IAS)

I am using jQuery + IAS to implement infinite scroll functionality on my website. However, I have encountered an issue where the cache is being disabled when making requests for new content. Specifically, the URL that is accessed to retrieve the next page ...

The ontrack listener for WebRTC peerConnection fails to fire

Primarily, the challenge I'm facing is unique from what I have come across in my research on "google". My setup involves using Spring Boot as a signaling server to establish connections between two different tabs of the browser or utilizing two peerCo ...

Tips for Accessing Values in a Dynamic Array in Vue.js

ArrayOrdered :[ { name :"PRODUCT 1", price :"20", amount:"10", Total 1:" ", discount : "" , Total 2:" " }, { name :"PRODUCT 2", price :"50", amount:"20", Total 1:" ", discount : "" , Total 2:" " }, { name :"PRODUCT 3", price :"15.5", amount:"10", Total ...

What is the best way to extract content between <span> and the following <p> tag?

I am currently working on scraping information from a webpage using Selenium. I am looking to extract the id value (text) from the <span id='text'> tag, as well as extract the <p> element within the same div. This is what my attempt ...

Navigating the Terrain of Mapping and Filtering in Reactjs

carModel: [ {_id : A, title : 2012}, {_id : B, title : 2014} ], car: [{ color :'red', carModel : B //mongoose.Schema.ObjectId }, { color ...

Struggling to perfectly align a Wufoo form within an iframe using CSS

I have integrated a web form from Wufoo into my webpage using an iframe. Custom CSS can be used to style the form and I am trying to center it within the iframe. Below is the HTML code of the form displayed in the iframe: <div id="container" class="lt ...

How to showcase images and text from an array of objects using React

I'm currently working on a React lightbox that is supposed to display a loop of objects containing images and text. However, I'm facing an issue where the images are not showing up with the corresponding text label as intended. It seems like I a ...

Issue when sending PHP Papa Parse object through AJAX

I am currently working on extracting a CSV local file in PHP using AJAX and have found success with Papa Parse. However, I am facing difficulties passing the results with AJAX. Despite trying various methods such as JSON.stringify, passing only the first f ...

What is the best way to ensure a floated image matches the height of its parent container?

I am facing an issue with setting the height of a logo within a footer div to match the height of the div itself. Using height: 100% doesn't seem to work as expected. Below is the relevant HTML: <footer> <img src="images/circle_logo.png" ...

After logging in, I am unable to redirect to another PHP page as the login form simply reloads on the same page

Lately, I've encountered an issue that has persisted for a few days. The login validation is functioning flawlessly. However, the problem arises when attempting to redirect to another page, specifically 'index.php', after logging in. Instead ...

Tips on avoiding blurring when making an autocomplete selection

I am currently working on a project to develop an asset tracker that showcases assets in a table format. One of the features I am implementing is the ability for users to check out assets and have an input field populated with the name of the person author ...

React's back button is experiencing functionality issues

I'm having an issue with my quiz page where the previous button is not functioning properly. The user should be able to change their answer before submitting, but after 5-6 clicks on the previous button, they are redirected to the next page without co ...

Issue: Unable to update reference: The primary argument includes an invalid key

Encountering a curious error on Firebase while working with Vue.js: The process goes smoothly the first time, and I can execute the following function multiple times without any issues. addSpace: function (newSpace) { let userId = firebaseApp.auth(). ...

Enhance your interface with stunning JQuery code snippets

We are in the process of developing a web application that will be fully 2.0 compliant (utilizing AJAX and more). Although I am primarily a rich-client and server-side developer, my knowledge of Javascript is limited to a functional level. However, based ...

Combining multer, CSRF protection, and express-validator in a Node.js environment

Within my node.js application, I am utilizing an ejs form that consists of text input fields in need of validation by express-validator, an image file managed by multer, and a hidden input housing a CSRF token (a token present in all other forms within the ...

Utilizing React JS to dynamically populate a table with data from an external JSON file

As a newcomer to the realm of React, I am facing challenges in displaying my JSON data in a table. class GetUnassignedUsers extends React.Component { constructor () { super(); this.state = { data:[] }; } com ...

Exploring color options in matplotlib for HTML designs

Is there a way to change color in a matplotlib plot using html in Python? I attempted plt.plot(x,y,marker=".",color="#e0e0e0") However, this resulted in data points displayed as ".", connected by lines of color #e0e0e0 which is not desired. I also experi ...

Converting PHP variables to JavaScript using AJAX and XML communication

In order to gain a deeper understanding, I am determined to tackle this task without relying on jQuery. This means I am willing to reinvent the wheel in order to fully comprehend how it functions. My research has led me to believe that AJAX is the key to a ...

Repeatedly triggering the Jquery Dialog Event

When I open a calendar plugin in jquery dialog, I encounter a recurring issue. Every time I close and reopen the dialog, my calendar event onDayClick triggers multiple times – twice, thrice, and so on. <div id="show_calendar"> <div class="c ...