Adjusting the z-index of an element with a simple click

Trying to tackle the challenge of adjusting the z-index of "content" relative to a clicked "menu-item". Progress has been made for menu items, but coming up short on the rest. In essence, clicking #m1 should set Z-Index 10 for #c1 and so forth.

HTML

<div id="content" class="container">
    <div id="c1" class="content">content1</div>
    <div id="c2" class="content">content2</div>
    <div id="c3" class="content">content3</div>
    <div id="c4" class="content">content4</div>
</div>
<div id="menu" class="container">
    <div id="m1" class="menu-item"></div>
    <div id="m2" class="menu-item"></div>
    <div id="m3" class="menu-item"></div>
    <div id="m4" class="menu-item"></div>
</div>

CSS

/*global*/
.container{
    position: absolute;
    width: 300px;
    height: 100px;
}
/*content*/
.content{
    position: absolute;
    height: 100%;
    width: 75%;
    right: 0;
    background: #354458;
    text-align: center;
    color: #fff;
    line-height: 95px;
}
/*menu*/
.menu-item{
    position: absolute;
    width: 25%;
    height: 100%;
    background: green;
    cursor: pointer;
    transition: left 200ms ease-in-out;

}
.menu-item.closed{
    left: 0 !important;
}
#m1{
    left:0;
    background: #DB3340;

}
#m2{
    left: 25%; 
    background: #E8B71A;
}
#m3{
    left: 50%; 
    background: #1FDA9A;
}
#m4{
    left: 75%; 
    background: #28ABE3;
}

JQuery

$(document).ready(function(){
    var menu = $('.menu-item');

    menu.click(function(){
        $(this).siblings(menu).css('z-index', "initial");
        $(this).css('z-index', 11);
    });
    menu.click(function(){
        menu.toggleClass("closed");
    });
});

Explore the working example here: http://jsfiddle.net/8a3vqy5v/

Answer №1

There is no need to register multiple click events for the same element, as they function in the same way as a single binding would in your scenario.

To hide and show background content based on the menu index within the click event, you can refer to the code snippet below:

var index = $(this).index();
$('.content').hide();
$('.content').eq(index).show();

Snippet :

$(document).ready(function(){
var menu = $('.menu-item');

menu.click(function(){
$(this).siblings(menu).css('z-index', "initial");
$(this).css('z-index', 11);
        menu.toggleClass("closed");

        var index = $(this).index();
        $('.content').hide();
        $('.content').eq(index).show();
});

});
/*global*/
.container{
    position: absolute;
    width: 300px;
    height: 100px;
}
/*content*/
.content{
    position: absolute;
    height: 100%;
    width: 75%;
    right: 0;
    background: #354458;
    text-align: center;
    color: #fff;
    line-height: 95px;
}

/*menu*/
.menu-item{
    position: absolute;
    width: 25%;
    height: 100%;
    background: green;
    cursor: pointer;
    transition: left 200ms ease-in-out;
    
}
.menu-item.closed{
    left: 0 !important;
}
#m1{
    left:0;
    background: #DB3340;
    
}
#m2{
    left: 25%; 
    background: #E8B71A;
}
#m3{
    left: 50%; 
    background: #1FDA9A;
}
#m4{
    left: 75%; 
    background: #28ABE3;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="content" class="container">
    <div id="c1" class="content">content1</div>
    <div id="c2" class="content">content2</div>
    <div id="c3" class="content">content3</div>
    <div id="c4" class="content">content4</div>
</div>
<div id="menu" class="container">
    <div id="m1" class="menu-item"></div>
    <div id="m2" class="menu-item"></div>
    <div id="m3" class="menu-item"></div>
    <div id="m4" class="menu-item"></div>
</div>

Answer №2

To access the index of the clicked .menu-item element and choose the corresponding .content element based on that index, you can utilize the .eq() method:

Check out the Updated Example here

var $menu = $('.menu-item');

$menu.click(function() {
  $(this).css('z-index', 11).siblings($menu).add('.content').css('z-index', '');

  if (!$(this).hasClass('closed')) {
    $('.content').eq($(this).index()).css('z-index', 10);
  }
  $menu.toggleClass("closed");
});

If you encounter a strange transition issue with this approach, consider using the technique demonstrated in this example instead. This alternative method involves monitoring the transitionend event for smoother transitions.

Answer №3

For a scenario with a set number of elements (e.g. 4), you can streamline the process by creating individual rules for each element:

$('#m1').click(function() {
    $('.content').css('z-index', '');
    $('#c1').css('z-index', '11');
});
$('#m2').click(function() {
    $('.content').css('z-index', '');
    $('#c2').css('z-index', '11');
});
$('#m3').click(function() {
    $('.content').css('z-index', '');
    $('#c3').css('z-index', '11');
});
$('#m4').click(function() {
    $('.content').css('z-index', '');
    $('#c4').css('z-index', '11');
});

If you prefer a universal rule that can be applied to any #mx - #cx pair, you could extract the clicked element's id as a string and manipulate it by replacing the initial letter with 'c'.

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

An individual in a chat App's UserList experiencing issues with incorrect CSS values. Utilizing Jquery and socketio to troubleshoot the problem

Currently, I am testing a new feature in a chat application that includes displaying a user list for those who have joined the chat. The challenge is to change the color of a specific user's name on the list when they get disconnected or leave the cha ...

What is the best way to shorten a tweet that goes over 140 characters?

I have an algorithm for tweeting that eliminates @-mentions and will not post tweets under the following conditions: 1) if the question is different from the answer, 2) if the composed tweet exceeds 140 characters, and 3) if the tweet is potentially sensit ...

The website encountered an error in loading with the error message "ENOTFOUND" in Cypress

All my cypress tests were running smoothly until one day they all failed to visit the target site. The error message that I received was: cy.visit() failed trying to load: https://mywebsite.com/accounts/login/ We attempted to make an http request to this ...

What is the best way to extract a color box from a Bootstrap container?

After trying multiple solutions found online, I'm still struggling to break out of the bootstrap container as needed. The setup involves a .fluid-container for a full-width background behind a regular .container. The challenge arises when the first co ...

Issue: The module 'xdl' cannot be located while executing the command "npm start" in React Native

Recently, I delved into learning React Native through an online Udemy course. Everything was going smoothly until a few days back when I encountered an error message after running the simple "npm start" command. Despite trying various solutions like reinst ...

Width of the `div` element is expanding

I'm encountering an issue where a div container is automatically stretching to the full width of the page, instead of adjusting to fit the content within it. Below is the HTML code snippet: <!doctype html> <html> <!-- Head --> <h ...

Remove a comment from the page without needing to refresh the entire

Is there a way to enhance this code so that when a comment is deleted, the page does not refresh? It can be frustrating when deleting a comment causes the page to scroll back to the top. AJAX function deleteComment(pid){ $.ajax({ type: "PO ...

I find it curious that I need to include the bootstrap link in the body of the HTML file rather than in the head, unlike the bootstrap

Check out the Bootstrap documentation here which states that we should add the link at the end of the body just before its closing tag. As a newcomer, I've noticed linking being done in the head section for non-bootstrap codes, leading me to feel a bi ...

Fetching an input in a Symfony form instantly using the make:form command

I successfully created a form using the Symfony make:form command. My goal is to have the "serial number" field in my form auto-fill with the last 10 characters of the input in the "barcode" field as I type. This is the file where I define my form struct ...

Pause file uploads, display modal popup, then resume uploading

Is there a way to pause file uploading in order to display file requirements before proceeding? For example, when a user clicks on "upload file", I would like to show them a modal window with details about the file they need to upload. Once they click ok, ...

Utilizing unique symbols to dynamically add form elements to an HTML page with jQuery's append method

I am facing an issue with creating forms on my HTML page. Here is an example of how I am trying to do it: <form action="/tasks/<%= allTasks.e[0].id %>/delete" method="POST"> <button class="deleteTask">Delete</button> </f ...

Move your cursor over one container to see the impact on another

My goal is to create a hover effect on a box that triggers another div with easing effects. Below, you'll see that the .images{} class has a 0s infinite scroll initially, but when the .box:hover > .images{} selector is activated, it changes to a 10 ...

Change to the parent frame using webdriver

I am facing an issue while trying to switch to the parent frame or iFrame using webdriver.js. Although I have implemented a function that works, it only goes up to the second level of frames. When attempting to switch to the parent frame from a deeper fr ...

Dynamically update a dropdown menu with options based on the user's selection from a prior dropdown list using ajax and JSP Servlet

I am working on creating a real-time project for a consultancy and could use some assistance with developing a JSP page. Specifically, I need to allow the user to select a Client (Company name) from a dropdown list. Once a Client is selected, the HR list ...

The mobile button bar on iPhone is shown when scrolling or jumping up to the anchor, but it doesn't actually scroll or jump to the

My goal is to include a back-to-top button that remains fixed at the bottom of the browser window. Surprisingly, when clicked, it successfully scrolls to the top in desktop and Android browsers but fails to do so on iPhones. On an iPhone, clicking the butt ...

Tips for Resizing and Reviving Divs with jQuery

I have recently ventured into the world of web development to assist a family member with their website. My knowledge and experience are limited, but I am facing an interesting challenge. I am trying to manipulate certain divs as users scroll down the page ...

When I click the button, I would like the value of the button to be displayed in a textbox

I'm currently working on an interactive virtual keyboard project and need help with a function that will display the pressed button values in a text box located next to the keyboard. Here is the code snippet I've come up with so far: <script ...

Ways to merge a span element with a paragraph to function as one continuous block of text

I am working with a RichText editor and I am trying to allow users to create their own custom figcaption for a figure. The challenge is that the figure number must be generated separately. I want to merge the user's description of the figure (p tag) w ...

How can I incorporate popups in React using mapbox-gl?

Currently utilizing mapbox-gl within React, everything seems to be functioning properly except for the integration of mapbox-gl's Popups. I have the function let Popup, but I am uncertain about how to incorporate it. renderMap() { if (this.props. ...

Can a websocket be used to communicate with a server that is not the same as the one hosting the html5 website?

My goal is to establish communication between a hardware device and an HTML5 website. It seems like the best way to do this would be through WebSockets or possibly WebRTC over a WiFi network. Is that correct? I haven't come across any solutions invol ...