When an absolute positioned DIV is nested inside a relatively positioned DIV, it disappears when the page is scrolled

A unique feature of my DIV is that it remains fixed at the top of the page as you scroll. This is achieved by setting its position to fixed. However, within this main container, I have another div with a relative position. While the content in the relatively positioned DIV looks good, the absolutely positioned elements inside it disappear behind the fixed div after scrolling.

I attempted using z-index properties to resolve this issue but had no success. You can view the problematic code on JSFiddle: http://jsfiddle.net/c2vqd5fw/

The JavaScript code snippet for achieving the sticky behavior:

$(document).ready(function() {
    $(window).scroll(function(){
        var docViewTop = $(window).scrollTop();
        var offset = 0;

        // Implementation details...
        
        $('.toggle-switch').click(function(){
            $(this).children().toggleClass('off');
        });
    });
});

The CSS styles used in the implementation:

.stuff-above{
    /* Styles */
}
.stuff-below{
    /* Styles */
}
.scroll-wrapper{
    width:100%;
}

// More CSS styles...

If you engage with the empty box (when the blue element is hidden), the absolutely positioned content reappears. Any insights or suggestions on how to fix this behavior?

Answer №1

UPDATE: Although I didn't completely resolve the issue, I managed to revamp the toggle without utilizing absolute or relative positioning, resulting in a much cleaner functionality. For those interested in taking a look, you can find it here: http://jsfiddle.net/c2vqd5fw/

.toggle-switch .content{
width:150%;
height:32px;
transition: margin-left 0.4s ease-in 0s;
-moz-transition: margin-left 0.4s ease-in 0s;
-webkit-transition: margin-left 0.4s ease-in 0s;
-o-transition: margin-left 0.4s ease-in 0s;
}
.toggle-switch .content.off{
margin-left:-32px;
transition: margin-left 0.4s ease-in 0s;
-moz-transition: margin-left 0.4s ease-in 0s;
-webkit-transition: margin-left 0.4s ease-in 0s;
-o-transition: margin-left 0.4s ease-in 0s;
}
.toggle-switch .content #on{
background-color: #2FCCFF;
color: #FFFFFF;
}
.toggle-switch .content #off{
background-color: #EEEEEE;
color: #999999;
text-align: center;
}
.toggle-switch .content .option{
display: inline-block;
float: left;
width: 31px;
height: 32px;
padding: 0px 7px;
line-height: 32px;
font-size: 14px;
font-weight: bold;
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
}
.toggle-switch .slider{
    background-color:#bbbbbb;
display: inline-block;
float:left;
border-left:1px solid #999999;
border-right:1px solid #999999;
    
}
.toggle-switch .slider img{
    max-width:32px;
max-height:32px;
    min-width:32px;
min-height:32px;
}
<div id="liGroup" class="toggle-switch">
  <div class="content">
    <div id="on" class="option">On</div>
    <div class="slider">
      <img class="toggleImg" src="" alt="Linkedin"/>
    </div>
    <div id="off" class="option">Off</div>
  </div>
</div>

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

Encountering an unusual error while utilizing the Rails 3 form_for with the :remote option set to true

Encountering an error and seeking assistance: try { Element.update("status", "There seems to be an issue with this product."); } catch (e) { alert('RJS error:\n\n' + e.toString()); alert('Element.update(\"status\", &bsol ...

Creating HTML tables with consistent vertical lines and broken horizontal lines

Looking to style an HTML table with unique grid-lines. Wanting continuous horizontal lines (foreground) and interrupted vertical lines (background), overlaid by the horizontal lines. Different line heights, some 1px, others 2px for variety. Vertical lines/ ...

What could be causing the Material AngularJS (CSS) on my website to not function properly, unlike the demo that was

I am completely new to AngularJS and I want to incorporate the material design version of AngularJS for developing a form. I followed the beginner's guide and attempted to create something, but it didn't turn out like the one on the website. So, ...

Pressing the "Enter" key in a .Net WinForm Browser Control

How can I simulate the Enter key press event on a web page using the GeckoFX Web Control? I am unable to use SendKeys.Send({ENTER}) Is there a method to trigger the Enter key using JavaScript within a webpage? ...

What steps can I take to ensure the reset button in JavaScript functions properly?

Look at this code snippet: let animalSound = document.getElementById("animalSound"); Reset button functionality: let resetButton = document.querySelector("#reset"); When the reset button is clicked, my console displays null: resetButton.addEvent ...

Select various items simultaneously by pressing Ctrl and access them via form submission

Currently, I have a form with a list of days displayed. When a user clicks on a day, jQuery is used to populate a hidden field with the selected value. This allows me to reference the value in the post array. I now want to enhance this functionality by en ...

What are the best scenarios for implementing anonymous JavaScript functions?

I am currently exploring the reasons behind using anonymous JavaScript functions. Can you list out the distinctions between these functions? Describe scenarios where each function would be appropriate to use. var test1 = function(){ $("<div />" ...

Is Bootstrap Hover Class Inadvertently Included Without Cause?

I am in the process of creating a very basic navigation bar. Bootstrap has been incorporated into my project, along with my custom stylesheet. I believe there is only one bootstrap class on my page. Despite adding a customized hover effect, when hovering o ...

What is the best way to access the current value and name of a textbox in real-time using

How can I retrieve the value of a textbox in JavaScript using onblur and on keyup events, while also performing real-time checking for each individual textbox using "this keyword"? Here is my JSFiddle link. Can you assist me in modifying it? ...

Tap on the HTML5 video to exit the fullscreen mode

Objective I have successfully implemented a fullscreen video setup that triggers when a link is tapped on a mobile device. To maintain a clean aesthetic, I have hidden the HTML5 video controls using CSS. The desired functionality includes closing the full ...

Interact with a dropdown menu using Selenium

Typically, my approach with the Selenium Select object is as follows: val selectCode = new Select(driver.findElement(By.id("someID"))) selectCode.selectByValue("4") The issue arises when there is no value in the html code. Here is an example of a ...

The luminosity of MeshBasicMaterial in Three.js

In my current setup with threejs, I am utilizing MeshBasicMaterial for lighting effects. Each element contains a simulated point light within. I have assigned a variable named color, defined as rgb(random value, random value, random value). I am looking ...

JavaScript Page Search - Error in Finding Single Result

I am currently working on implementing a 'find in page' search box and have come across some JavaScript code that is working really well. Everything works great when there are multiple strings, as pressing enter cycles through the results and the ...

Sending objects as parameters to a class in JavaScript - an easy guide

Having trouble passing a parameter to the class for handling AJAX errors. The initial function is as follows: function GetLastChangePass(UserId) { var field = { id: UserId, } var fieldStringified = JSON.stringify(field) $.ajax({ ...

Error: The function imgPreload cannot be executed because $ is not recognized as a function

I'm encountering an issue with calling a JavaScript function. Here's the code snippet I'm using: <script type="text/javascript" src="<?php echo base_url().'assets/'?>js/jquery-1.9.0.min.js"></script> <script t ...

How can I create a jumping animation effect for my <li> element in JQuery?

I have a horizontal navigation bar with a "JOIN" option. My goal is to make the #jump item continuously jump up and down after the page has finished loading. Currently, I have this code which only triggers the jump effect once when hovering over the eleme ...

Displaying group field data in one page on JQGrid when pagination is enabled

Is there a way in JQGrid to display group data on one page? Specifically, if displaying all items from an order would exceed the list limit, I want to only show data up to the previous order without exceeding the limit. How can this be achieved? ...

Utilizing jQuery Mobile and PhoneGap to incorporate HTML files into data-role=page: A step-by-step guide

I am looking to load HTML files into a data-role=page using jQuery Mobile and PhoneGap. My project consists of several small HTML files with separate pages. In my implementation, I have: index.html: <!DOCTYPE html> <html> <head> ...

ClickAwayListener's callback function stops executing midway

I am currently utilizing Material-UI's ClickAwayListener in conjunction with react-router for my application. The issue I have come across involves the callback function of the ClickAwayListener being interrupted midway to allow a useEffect to run, on ...

Changing properties of JavaScript arrays

Currently using Express and dealing with an array of objects that I need to adjust the created property for better presentation, utilizing the dateFormat package. The array originates from a mongo query and is stored in a variable called stories. A sample ...