The hover functionality fails to activate when a z-index is applied

My issue revolves around 2 divs: one containing a link and the other a box-shaped container. The link is set with position:fixed; causing it to fly over the container div. To resolve this, I attempted to assign a negative z-index value to the link, but unfortunately, the hover state ceased to function unless I scrolled an equivalent amount as the height of the container div – typically around 3 scrolls before the hover state reactivated.

HTML

<div id="div-1">
 <div class="container"></div>
</div>

<!-- additional div elements -->

<div id="div-2">
 <a href="#">This is a link</a>
</div>

CSS

#div-2 a{
width:13%;
height:auto;
padding:0.5em 2.3em;
display:block;
position:fixed;
font-weight:500;
font-size:1.09em;
text-align: center;
background-color: none;
text-decoration:none;
outline:none;
z-index:0;}
#div-1{
...CSS properties...
}

An important note: The container remains hidden via JQuery unless a specific button is clicked.

JQuery code...

https://example.com/image.jpg

After trying various approaches, such as adjusting the z-index values for both the container and link, the issue persists.

Update: I am considering changing the CSS property "z-index", specifically when the container button is toggled on.

Therefore, the link will feature z-index:-9; only when the container is visible. When the container is toggled off, the z-index should either be removed or remain at default.

I'm struggling with implementing this concept using jQuery, and my current attempt still results in the z-index remaining active when the container is toggled off. How can I properly remove or reset the z-index value?

Any alternative solutions to this problem are welcomed.

Answer №1

It seems like you are a bit unsure about your requirements, but the images provided did help to some extent. It appears that you want the link above the container, although there seems to be some confusion about it?

The main objective here is to have the anchor at a lower index, so that when the container is toggled on or viewed, the link doesn't overlap on top of the container.

However, you still want the link to respond when hovered upon. It seems like you are puzzled as to why the hover effect isn't working when the container is open and the link is visible. Logically, one would expect to be able to hover over the visible part of the link at least.

  1. It's not related to jQuery or the .container, rather it's about the parent container of the .container which is referred to as #div-1. Due to its inherent block behavior, the #div-1 width remains 100%, even without any explicit styling.
    • Solution: Assign a smaller width to #div-1.
  2. You have a fixed position for the link but no coordinates specified. A fixed element requires defined positioning in order to function properly. Also, if there are other positioned elements with which you expect interaction, those elements should be positioned too. By setting position:relative for div-1, the z-index properties of the link and div-1 will function correctly.
    • Solution: Provide #div-2 with top and left or right along with bottom properties. Ensure #div-1 has a position property for proper functioning of z-index.

All specific details are documented within the source code.

PLUNKER

SNIPPET

<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" href="style.css">
  <script src="script.js"></script>
  <style>
    html,
    body {
      height: 100%;
      width: 100%;
    }
    #div-1 {
      overflow-y: auto;
      overflow-x: hidden;
      box-sizing: border-box;
      position: relative;
      z-index: 1;
      border: 1px solid red;
      width: 200px;
      /*Enable this and it will block link*/
      /*width:100%;*/
      height: 290px;
    }
    .container {
      /* This saves you an unnecessary step in jQuery */
      display: none;
      width: 200px;
      height: 290px;
      background: orange;
    }
    #div-2 a {
      width: 13%;
      height: auto;
      padding: 0.5em 2.3em;
      display: block;
      position: fixed;
      font-weight: 500;
      font-size: 1.09em;
      text-align: center;
      background-color: none;
      text-decoration: none;
      outline: none;
      /* It's not clear whether you want the link above or
      | below the container. If above, simply change to
      | z-index: 2
      */
      z-index: 0;
      /* If you have a fixed element give it coords, otherwise       
      | it doesn't know where it should stand and behavior
      | will be unexpected.
      */
      top: 10%;
      left: 125px;
    }
    #div-2 a:hover {
      background: red;
      color: white;
    }
    /* FLAG is just to test the accessibility of the link */
    #FLAG {
      display: none;
    }
    #FLAG:target {
      display: block;
      font-size: 48px;
      color: red;
    }
  </style>
</head>

<body>
  <button id='button-f'>F</button>
  <div id="div-1">
    <div class="container">Container is open</div>
  </div>

  <!-- other divs like 5 or 6 of 'em -->

  <div id="div-2">
    <a href="#FLAG">This is a link</a>
    <span id='FLAG'>This link is accessible now!</span>
  </div>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
  <script>
    /* This is the jQuery you need to accomplish what you want.
    | The rest was redundant and unnecessary.
    */
    $(document).ready(function() {

      $("#button-f").click(function(e) {
        $(".container").toggle();

      });

    });
  </script>
</body>

</html>

Answer №2

Have you attempted adding a z-index to #div-2?

To properly assign a z-index, make sure to also give it a position. Here is an example:

#div-2 a{
width:13%;
height:auto;
padding:0.5em 2.3em;
display:block;
position:fixed;
font-weight:500;
font-size:1.09em;
text-align: center;
background-color: none;
text-decoration:none;
outline:none;
z-index:2;
}

#div-1{
width:100%;
height:290px;
overflow-y:auto;
overflow-x: hidden;
box-sizing: border-box;
display: block;
position: relative;
z-index:1; 
 }

Answer №3

It's unclear what exactly is happening in your code, but upon reviewing the provided JavaScript, there seems to be an issue with the if section referring to 'button-f'. Do we really need this line? It also appears unnecessary to hide the 'container' in JS. Currently, you have to scroll to a certain height set by #div-1, which is not hidden. The amount of height you need to scroll is determined by that. Here are the changes I made to your code: 1. Moved the height attribute from div-1 to the .container class. 2. Included the missing a:hover class and removed some unnecessary CSS. If you have any other questions, feel free to ask in the comments LIVE ON FIDDLE

$(document).ready(function() {

 $("#button-f").click(function() {
     $(".container").toggle();
   });
  });

   
   button {
  width: 13%;
  height: auto;
}
#div-1{
width:100%;
overflow-y:auto;
overflow-x: hidden;
box-sizing: border-box;
display: block;
 }
.container {
   height:290px;
  display:none;
  border: 1px solid red;
   
}

#div-2 a {
  width: 13%;
  height: auto;
  padding: 0.5em 2.3em;
  display: block;
  positon:fixed;
  float:right;
  font-weight: 500;
  font-size: 1.09em;
  text-align: center;

  text-decoration: none;
  border-radius: 10px;
 

}

#div-2 a:hover {
  background: black;
  color: white;
}

   
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
 
<body>
 <button id="button-f">
    button
 </button>
 <div id="div-1">
   <div class="container">tagasdgasdgasdgas</div>
 </div>

 <!-- other divs like 5 or 6 of 'em -->

 <div id="div-2">
   <a href='#'>This is a link</a>
 </div>
</body>


 

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

Dynamically load scripts in angularJs

Having multiple libraries and dependencies in my angularjs application is posing a challenge as I want to load them all using just one script, given that three apps are utilizing these dependencies. Currently, I have this custom script: var initDependenci ...

Is node.js required for utilizing Angularjs?

Considering incorporating angular.js into my website's Image Editing Tool. Is node.js necessary for this integration? I'm a bit puzzled here. Are there instances where both nodejs and angularjs are used together, or can they operate independentl ...

Redux: The action was effectively triggered, but the state remained unformed

I'm currently working on a project to familiarize myself with Redux. I am using the Redux DevTools to monitor my two states: lists and todos. However, I am running into an issue where only todos are being displayed, despite trying various troubleshoot ...

What is the best way to align text alongside icons using ng-bootstrap in Angular 8?

I have a set of four icons positioned next to each other, but I want them to be evenly spaced apart. I tried using the justify-content-between class, but it didn't work. How can I achieve this? I'm creating a Progressive Web App (PWA) for mobile ...

CSS margin-left causing issues in Chrome Extension

My current situation is incredibly puzzling, as I am at a loss for what is happening. I developed a Firefox add-on using jQuery and CSS to revamp a website. When attempting to transfer the add-on to Chrome (which was relatively straightforward due to the s ...

Methods for altering the color of a div using addEventListener

Why doesn't the color change when I click on the div with the class "round"? Also, how can I implement a color change onclick? var round = document.querySelector(".round"); round.addEventListener("click", function() { round.style.backgroundCol ...

Fetching Data from Response Headers in Angular 4.3.3 HttpClient

(Text Editor: Visual Studio Code; TypeScript Version: 2.2.1) The main objective here is to fetch the headers of the response from a request Let's consider a scenario where we make a POST request using HttpClient within a service: import { Injec ...

Practical Tip for jQuery - Storing DOM Element in a Variable for Consistent Usage Across Your Codebase

As I was searching for some Jquery best practices, I came across this gem. Source: // Storing the live DOM element in a variable var elem = $("#elem"); // Setting an element's title attribute using its current text elem.attr("title", elem.te ...

Routing with nested modules in Angular 2 can be achieved by using the same

Encountering a common issue within a backend application. Various resources can be accessed through the following routes: reports/view/:id campains/view/:id suts/view/:id certifications/view/:id Note that all routes end with the same part: /view/:id. ...

Trigger f:ajax only when certain keys are pressed

Implementing f:ajax within h:inputText, I have created a functionality where a backing bean method is triggered by user input, with a time delay: <h:inputText ...> <f:ajax delay="500" event="keyup" listener="#{cc ...

Send an AJAX request to the server without waiting for a response using a JavaScript variable

My click counter is not sending variables to the server. I have tried finding examples on how to do this, but no matter what I attempt, the data is not being sent to the server. It seems like using AJAX would be the best option, but I must be doing someth ...

Utilizing dotenv: Incorporating the value of one environment variable into the name and value of another environment variable

When it comes to my testing framework, I rely on dotenv for handling environment variables across different test environments. Currenty, I'm looking for a way to incorporate the value of a specific environment variable into both the value and name of ...

`Hovering over a div triggers a continuous animation loop`

I've gone through various questions and solutions related to this issue, but unfortunately, none of them seem to work for me. It's possible that I might be overlooking something or my scenario is slightly unique. The main problem I'm facing ...

Uploading large files on Vuejs causes the browser to crash

Whenever I try to upload a file using my browser (Chrome), everything goes smoothly for files with a size of 30mb. However, if I attempt to upload a file that is 150mb in size, the browser crashes. The server's maximum upload size has been configured ...

The Ajax autocomplete feature was unable to find a matching HTTP resource for the requested URI

Seeking assistance with implementing autocomplete functionality on a textbox using data from a web method. The webmethod's browser operation is as follows: http://localhost/psa/DesktopModules/PsaMain/API/ModuleTask/GetIssuers?SearchString=e The resu ...

Can a browser still execute AJAX even if the window.location is altered right away?

Here is the situation I am facing: <script> jQuery.ajax{ url : 'some serverside bookkeeping handler', type : post, data : ajaxData }; window.location = 'Some new URL'; </script> Scenario: I n ...

Navigating to a particular div using a click event

I am trying to achieve a scrolling effect on my webpage by clicking a button that will target a specific div with the class "second". Currently, I have implemented this functionality using jQuery but I am curious about how to accomplish the same task using ...

Unsynchronized Request Sequencing

Seeking advice on enhancing a previously accepted answer related to chained ajax requests can lead to some interesting solutions. One proposed solution involves sequencing 3 ajax requests in an asynchronous chain: var step_3 = function() { c.finish() ...

Having difficulty updating the state with editorState retrieved from the server in ReactJs when using draftJs

Incorporating a rich text editor into React using draftJs has been successful. The editorState data is converted to raw data, stringified, and sent to the server for rendering on the front end. However, I am facing challenges in updating the editorState ...

Retrieve the value of a dynamically generated input element within a form object

I'm trying to figure out how to reference a dynamic 'name' of an input element in a form. Can anyone help? Here's an example using HTML: <form> <input type="text" name="qty1" value="input1" /> <input type="text ...