Continue scrolling once you reach the fixed-positioned div

live demo

html

<div class="one"></div>
<div class="two"></div>
<div class="three"></div>
<div class="four"></div>

css

.container{  
    margin: 0;
    padding: 0;
    margin-top: 101px;    
}
div{
    height: 100px;
    width: 100%;
}
.one{
    background: red;
    position: fixed;
    top: 0;
}
.two{
    background: green;
}
.three{
    background: blue;
    height: 500px;
}
.four{
    background: black;
}

Question:

How can I make the green div scroll normally without being hidden inside the red div, while keeping the blue div and all subsequent divs visible when scrolling?

Is there a way to achieve this using jQuery or pure CSS?

Attempted jQuery solution:

I tried the following code but it's not working as expected. Can you please suggest a better approach?

$(window).scroll(function(){
   var topHeight = 200;
    var scrollHeight = $(window).scrollTop();
    var totalHeight = $('.three').height() + $('.four').height();
    if (scrollHeight == topHeight){
        $('body').css('margin-top',totalHeight);
    }
});

Answer №1

Just a little bit more to go - add position: relative to the CSS styling of div.three

.three{
    background: blue;
    height: 500px;
    position: relative;
}

Also, remember to apply this to other non-scrolling divs like div.four, and so on.

Check out the working example on jsFiddle: http://jsfiddle.net/damoiser/nUwJa/1/

Answer №2

Here is a JavaScript function that you can use to make an element follow the user as they scroll down the page. Check out the demo here

var windw = this;
$.fn.followTo = function ( pos ) {
    var $this = this,
        $window = $(windw);

    $window.scroll(function(e){
        if ($window.scrollTop() > pos) {
            $('.one').css({
                position: 'absolute',
                top: pos
            });
        } else {
            $('.one').css({
                position: 'fixed',
                top: 0
            });
        }
    });
};

$('.one').followTo(100);

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

quickest method for attaching a click listener to dynamically added elements in the document object model

When adding multiple elements to the DOM, how can we ensure that these elements also have a click handler attached to them? For instance, if there is a click handler on all elements with the "canvas-section" class, and new "canvas-section" elements are con ...

Change the color of the menu icon based on the specified HTML class or attribute

I'm trying to create a fixed menu that changes color depending on the background of different sections. Currently, I am using a data-color attribute but I am struggling with removing and adding the class to #open-button. Adding the class works fine, ...

Is it possible for me to fetch the image and asynchronously load it into a div using AJAX

I'm trying to figure out how to load images from the href links into a div using ajax. Can anyone provide some guidance on this? I've been searching and it seems like the load() function may not work for loading images in this way? <ul id ...

Transitioning from curl to jQuery's $.ajax() method

Recently, I encountered a challenge while trying to switch the curl code used for an API known as TextRazor to jquery's AJAX due to certain limitations on the platform. Despite exploring various solutions suggested by the community in response to simi ...

Creating a multi-column layout with HTML and CSS

I'm currently exploring the most effective method to design this concept: https://i.stack.imgur.com/SOQp4.png The black square symbolizes small icons, accompanied by a heading and paragraph for each specific section. The icons need to align with the ...

The table width is malfunctioning on Firefox when viewed in HTML

I have been puzzled by the fact that I am unable to adjust the width of the table th in Firefox browser to the smallest value. However, in Chrome browser, this functionality works perfectly. I simply want to divide the values of my td into three rows as sh ...

Hide the border on a table with CSS

I recently installed a Wordpress Template and customized the table's background and text colors. However, I am struggling to remove the border around the table. Despite searching for solutions, my limited CSS knowledge has not led me to a fix that wor ...

Insert title into PDF document

I am currently working on a react application that generates PDF documents easily. The libraries I have utilized are: jspdf html2canvas Below is the code snippet that demonstrates my approach: index.js: <div className="App"> < ...

Centering an icon vertically and introducing animation upon hovering over an image

I'm having difficulty with vertically centering an icon that drops down on image hover, and I want to make its transition smoother. Currently, it's dropping down too abruptly and no matter what transition property I apply (ease, ease-out, etc.), ...

A pop-up window for selecting a file within an <a> tag

Is there a way to create an Open File dialog box on a link within my webpage? I attempted <input name="uploadedfile" type="file"> However, it only functions as a button and does not allow for the selection of multiple files. I would like somethin ...

Slider that allows range selection after midday

Currently facing a dilemma using the noUiSlider plugin. I have set up a time range picker starting from 6 am to 6 am the following day. Everything works smoothly until it reaches 23:59, but I need it to display 1:00, 2:00 instead of 25:00, 26:00 for the ...

Ways to reduce the size of a Select box when the option text is too lengthy

How can I reduce the size of the select box by splitting the lines of the options I attempted to modify the CSS with: select { width:100px; } However, only the select element was affected. The options remained the same size. My goal is to have ...

Tips for adjusting the maximum width and content-based width for columns within an HTML/CSS table

I'm working on a simple HTML/CSS code featuring a container with two adjustable slots, controlled by a handler in the middle to modify the space each slot occupies. The first slot contains a table with fixed columns, automatic columns, and limited col ...

What is the best way to extract the essential data from the returned value without any unnecessary additions?

Looking for a way to extract specific data from an api response in the format of x[[seconds:cost[[x? I am using php and javascript. How can I separate out the seconds and cost values only? For example, if the response looks like this: x[[16413:2.60[[x I ...

Difficulty with linking a CSS property to an HTML element using JavaScript

I'm currently working on building a custom carousel from scratch. Instead of using pre-made code snippets, I decided to challenge myself by creating one using setTimeout in JavaScript. However, I seem to be encountering an issue that I can't quit ...

Positioning a div inside another div using CSS

I'm having issues with a nested div causing trouble for me. <div style="border:solid 20px #ccff33;border-radius:10px;height:300px;width:300px;margin:20px;display: inline-block"> <img src="images/f35.jpg" style="margin-right:10px" /> ...

Can CSS modules be used in conjunction with outside libraries?

I've incorporated a Slider library () into my React project. While regular CSS allows me to style the slider properly, I am facing issues when using css modules. The tsx file looks like this: import styles from './styles.module.css'; . . . ...

Refrain from showing content beneath a certain element

Is there a way to hide all content that appears after a specific element, such as a particular class of div? The issue I am facing involves using a 1&1 webpage builder with a restrictive layout-template enforced by my boss. I am trying to remove the foote ...

What is the best method to remove the gap between my two horizontal div elements?

Is there a way to eliminate the small space between my two horizontal sections here? I've already attempted using margin: 0px; and padding: 0px; on the container, but it doesn't seem to be effective. Any assistance would be greatly appreciated! ...

CSS Animation Effect on Transparent PNG Image

Today, while working on my website, an interesting CSS effect came to mind. I vividly remember seeing it a few months ago, but unfortunately, I couldn't find it among my bookmarks. The website featured a captivating design with a prominent logo that ...