What is the best way to gradually show the contents of the second div starting from the bottom?

I want to create a scrolling effect where the contents of the second div are revealed as if they were fixed on the first div, similar to Conichi

View example in JSFiddle

.one {
  width: 768px;
  height: 700px;
  color: white;
  background-color: black;
  position: fixed;
  z-idex: -1;
}
.two {
  top: 700px;
  background-color: white;
  width: 768px;
  height: 700px;
  z-index:1;
  position: relative;
}
<body>
  <div class='one'>
    <p>
      What is Lorem Ipsum?
      Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
      Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. 
      It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. 
      It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </p>
  </div>
  <div class='two'>
    <p>
      What is Lorem Ipsum?
      Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
      Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. 
      It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. 
      It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </p>
  </div>
</body>

Answer №1

I successfully replicated the desired effect - check it out here

Cascading Style Sheets (CSS)

.container_1 {
     background-color: black;
   color: #fff;
   z-index:3;
 }
 .container_2 {
     background-color: white;
     z-index:2;
 }
 .container_3 {
     background-color: red;
   color: #CCC;
     z-index:1;
 }
 .container {
     overflow: hidden;
     width: 100%;
     height: 100%;
     position: fixed;
 }

jQuery Script

var height = 0,
    divs = 0;

$('.container').each(function(){
    var eachdiv = $(this).outerHeight();
    height += eachdiv;
    divs ++;
});

$('body').css('height',height);

$(window).on('scroll', function () {
    var scrollTop = $(window).scrollTop();

    if(scrollTop <= (height/divs)){
        $('.container_1').css('height',(height/divs)-scrollTop);
    } else if (scrollTop <= (height/divs) * 2) {
        $('.container_1').css('height',0); // if the window scroll goes too quick
        $('.container_2').css('height',(height/divs)*2-scrollTop);
    } else if (scrollTop <= (height/divs) * 3) {
        $('.container_2').css('height',0);
    }// and so on...

});

HTML Markup

<body>
  <div class="container container_1">
      <p>What is Lorem Ipsum?</p>
  </div>
  <div class="container container_2">
      <p>What is Lorem Ipsum?</p>
  </div>
  <div class="container container_3">
      <p>What is Lorem Ipsum?</p>
  </div>  
</body>   

Answer №2

If you want to implement a scrollspy feature on your website, you can utilize the Scrollspy component from Bootstrap. To see it in action, simply click on the link I have provided. Alternatively, you can include Bootstrap 4 and add the following code snippet:

body {
  position: relative;
}

<body data-spy="scroll" data-target="#navbar-example">
 ...
   <div id="navbar-example">
     <ul class="nav nav-tabs" role="tablist">
      ...
     </ul>
   </div>
  ...
</body>

To activate the scrollspy functionality using Javascript, you can use the following script:

$('body').scrollspy({ target: '#navbar-example' })

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

Vue's keydown event will only fire when elements are in an active state

Encountering a strange issue while attempting to listen for keydown events in Vue.js. The keydown event is attached to a div tag that surrounds the entire visible area: <template> <div class="layout-wrapper" @keydown="handleKey ...

What is the best way to tally the number of fields in a JSON file based on their values

I'm relatively new to JavaScript and have a question that may seem basic. I've been struggling to find the answer, possibly because I'm not using the correct terminology. My goal is to count the number of "active" fields in a JSON file that ...

I'm curious why I can only see the HTML code and not the three.js code as well

I attempted to run a sample three.js game today, but only the HTML code appeared. I've also installed three.js with npm and tried running it with the VSC Live Server, but it's not working. You can find the source code here. What should be my nex ...

Ways to substitute numerous instances of a string in javascript

I have experience in developing websites using reactjs. I usually implement restAPI's with java and work with liferay CMS. In one of my projects, I created a shortcode for accordion functionality like this: ('[accordion][acc-header]Heading 1[/ac ...

How to retrieve a Typescript variable within an ngIf conditional statement in Angular

How can I access a TypeScript variable inside an ngIf statement? TS: public data: string; HTML: <td> <div *ngIf="data === 'BALL' ; else noplay" >{{ play }}</div> <ng-template #noplay> {{ gotohome ...

How can a controller in AngularJS detect when the back button of the browser is pressed

I've created a browser trivia game where authenticated players can select a game type, triggering a socket.io event in my Node.js server. The view then transitions to a "Searching for game" screen with a loading icon as the server waits for enough pla ...

React Redux in conjunction with redux persist ensures that only a portion of the data remains stored persistently

This app is built using React Native with the Redux pattern and redux-persist to store data for offline use. However, only some of the data is persisting correctly. For example, the `auth` data is persisting and loading from `AsyncStorage` without any issu ...

Is there a way to have an HTML scrollbar automatically start at the bottom?

I'm having trouble ensuring that my chatbox's scrollbar automatically starts at the bottom so that new messages are visible without needing to scroll down. The current code I have in JQuery is not achieving this desired behavior. $('#chatbo ...

Is it the right time to dive into HTML5 & CSS3?

The excitement around HTML5 and CSS3 is hard to ignore. But when is the right time to start incorporating them into our projects? How close are we to fully utilizing their capabilities? Update: I'm not interested in following the principles of: Grac ...

Enhancing elements in Bootstrap Studio with borders

After exploring the interface of bootstrap-studio, it appears that there is no direct option to add a border to an element. However, this can still be achieved either through custom css code or by utilizing the predefined boostrap border classes. You may r ...

Tips for expanding a script that relocates a logo into a navigation consisting of several unordered lists

I recently implemented a jQuery script to center a logo within my main navigation bar only when the screen width is above 980 pixels. It was working perfectly fine with a single unordered list, but as soon as I added more unordered lists within the nav, it ...

What is the best way to extract an inner exception message from a JSON string using JavaScript?

I am struggling with error messages that have a nested structure like this: var data = {"message":"An issue has occurred.", "exceptionMessage":"An error occurred while updating the entries. Refer to the inner exception for more details.", "exceptionTyp ...

Guide to bringing API information into a Material UI Card

I've been working on a Recipe app that leverages data from the edamame API. I successfully imported Material UI cards into my .js file to pull data from the API and stored it in a const named recipes. However, I'm facing difficulties in getting t ...

Is the function replaceWith() a duplicate of replaceAll()?

As I delve into jQuery, I am discovering that some functions have similar counterparts. Take for example replaceWith() and replaceAll(). Based on their grammar definitions, they simply swap the content and the selected element. Similarly, there are appen ...

Getting an error response with a status of 200 when making a jQuery ajax call

After sending a jQuery ajax request to my express-powered node.js server, everything seemed to be in order. However, to my surprise, the response invoked the error callback instead of the success callback, despite receiving a status code of "200". It was d ...

One way to dynamically retrieve the ID of a jQuery tab using a foreach loop in PHP OOP

I have a category table and subcategory table in my database. Currently, I am attempting to showcase the "category" title as the tab and the corresponding "subcategory" name as the content within the tab. Despite searching for a solution on Google, I was ...

Is there a way to maintain attributes when using jQuery's .replaceWith() function?

As a beginner in jQuery, I find myself following patterns without truly understanding them in a Trac template. Please excuse my ignorance. In Trac 1.0.1 (and possibly other versions), there is a preview feature that utilizes jQuery.replaceWith() to insert ...

Styling the jQuery location picker input with background and focus effects

While working on a web app using jQuery locationpicker, I noticed that it's causing some styling issues with the input. Despite being able to adjust properties like width, padding, and border, changing the background requires using jQuery. Here is th ...

Problem with detecting collisions in Three.js

Currently, I am developing a simple game where players can add objects (cubes) to the scene at the position of a raycaster (mouse) click on a large ground plane. To prevent cubes from overlapping with each other, I have implemented basic collision detectio ...

Utilizing components of GLTF to create an InstancedMesh

I have been working on replicating a specific piece of code found in the three.js repository related to instanced rendering. While following this example, I came across a part that is confusing to me. Why do we need to copy THREE.InstancedBufferGeometry ...