Creating a Scroll Window That Influences the Scroll Div Content

How can I sync scrolling in Windows with the scrolling of a div content, such as on WordPress editor pages?

Let's say I have the following elements:

  1. Header
  2. Article
  3. Footer

The div content has the following style:

.my-div {
   overflow-y: auto;
   height: 576px;
}

Within the my-div, there is an article that is 1000 characters long, causing a scroll to appear.

My goal is to have the scrolling effect when I reach the article section inside the my-div, where the scroll from scrolling down the window will also impact the scrolling within the div content.

<article>
    <div class="my-div">
        <!-- my content here -->
    </div>
</article>

So essentially, when I scroll down the window to the bottom, it should automatically trigger the scroll within my div content.

Once the scroll within 'my-div' content is complete, it should then smoothly guide me to the footer section without any interruptions.

I have been actively searching for a solution to this issue but haven't come across one yet.

Thank you for your assistance.

translation provided by: Google

Answer №1

This JavaScript code ensures that the element identified by YOUR_ELEMENT scrolls with the window.

const lastScrollTop = window.addEventListener("scroll", function() {
   const st = window.pageYOffset || document.documentElement.scrollTop;
   if (st > lastScrollTop){
     document.getElementById('YOUR_ELEMENT').scrollTop++;
   } else {
      document.getElementById('YOUR_ELEMENT').scrollTop--
   }
   lastScrollTop = st <= 0 ? 0 : st; // Adjust for mobile or negative scrolling
}, false);

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

What is the most effective design pattern for managing constant strings in Typescript or Javascript?

In my current project, I have implemented a view that can switch between two modes: view mode and edit mode. With the use of Angular directives, I am able to modify the behavior of the page depending on the selected mode. In C programming, it is common pra ...

Press on the button that is currently in your field of view

I have a web page with multiple buttons inside div elements. I am looking to automate the process of clicking the "Buy" button that is currently visible on the screen when the user presses the B key. $(document).keydown(function(e) { if (e.keyCode == ...

Arranging two div elements side by side with spacing between them using CSS

Can anyone help me with aligning two divs side by side using CSS? I attempted a few methods on my own but seem to be stuck. Any suggestions would be greatly appreciated! CSS: .posts{ display: flex; flex-direction: row; } .posts .col-md-6{ pa ...

Navigating to the specific item that a directive is linked to

I am looking to develop a directive that can be applied to elements to adjust their maximum height to be equal to the height of the window minus the distance from the top of the element to the top of the window. I have attempted it in the following manner ...

What is the best method for transmitting all parameters via a URL?

$(document).ready(function () { $("#submitButton").click(function () { var name = $("#name").val(); var age = $("#age").val(); var gender = $('input:radio[name=gender]:checked').val(); v ...

Transmitting data from the backend to AngularJS during page load

After diving into Angular JS for the first time, I've hit a roadblock at an essential stage. My setup consists of an AngularJS front end and Grails backend. Check out the code snippets below along with my query. URL Mapping entry: as "/graph"(cont ...

The distinction between using document.getElementById and document.getElementsByClassName in JavaScript

One thing that stands out is why does document.getElementsById function as expected here <div id="move">add padding</div> <button type="button" onclick="movefun()">pad</button> <script> function movefun() { document.get ...

Navigation Bar Revamp

Check out the colorful navigation bar on this page: The navigation bar appears distorted when I zoom in/out. Is there a way to resolve this issue? ...

Error: Attempting to display API data in a CardView using NativeScript-Vue results in a TypeError stating that property 'endpoint_link_1' is undefined

Greetings, I am a beginner in NativeScript-Vue and JavaScript. I do not have extensive experience with heavy Javascript coding. I am facing an issue with displaying data fetched from an API in CardViews. Below is the code snippet I attempted: <template ...

Tips for displaying dynamic content in VueJS?

I'm currently working on an app that allows users to choose which type of vuetify element they want to display on the page. There are 4 options available for selection. My goal is to render the corresponding vuetify component when a user clicks on the ...

Importing modules from another module in Node.js

I'm currently working on a nodejs app that consists of two files, index.js and ping.js. The main functionality is in index.js which handles routing and other tasks, while ping.js utilizes phantomJS. In my index.js file, I have the following line of co ...

The shadow cast by the point light in Three.js seems to be misplaced

Take a look at this JS fiddle to view my code. I'm encountering an issue where there is a gap between the object and its shadow. Interestingly, this gap does not occur with spot lights. Any suggestions on how I can resolve this? Highlighted code snip ...

Trouble with JavaScript confirm's OK button functionality in Internet Explorer 11

Having trouble with the OK button functionality on a JavaScript confirm popup in IE11. For one user, clicking OK doesn't work - nothing happens. It works for most other users though. Normally, clicking OK should close the popup and trigger the event h ...

Verifying a user's name when navigating a route

Hey everyone, I've been working on this project for the past 3 hours and could really use some help. I created an express webapp with an admin page. The register and login functionalities are all set up using passport. I have a function to check if th ...

I'm having trouble modifying the style of a different component using Nuxt.js's scoped style

Can someone assist me in understanding how functions? The documentation indicates that it only changes the style within a specific component zone, but what if I need to modify the style of another component based on the current component on the page? For ...

Issue with displaying PDF files on Google Chrome due to a software glitch

Recently, I encountered a puzzling issue on Google Chrome. I am not sure if the error lies with Google or within my code. When I set the div as display: none; and then show it again, the PDF view only shows a grey background. However, if I zoom in or out, ...

Position the spinner in the center of the user's screen

I created my own spinner: '''' #spinner-bg-loading{ position: absolute; left: 50%; top: 25%; width: 80px; height: 80px; margin: -75px 0 0 -75px; border: 16px solid #FFFFFF; border-radius: 50%; border-top: 16px solid #1 ...

JavaScript | Calculating total and separate scores by moving one div onto another div

I have a fun project in progress involving HTML and Javascript. It's a virtual zoo where you can drag and drop different animals into their designated cages. As you move the animals, the total count of animals in the zoo updates automatically with the ...

Trouble with background images appearing on screen

My folder structure looks like this: main directory index.html css image(directory) I have attempted to use the following code: background-image: url("../images/vegies.jpg") no-repeat; ("images/vegies.jpg") no-repeat; ...

Finding the value of an input without having to submit it first and searching for it within a datalist

> Here is an example of HTML code <label>Person</label> <input name="PersonID" type="text" id="PersonID"> <label>Car Plate Number</label> <input name="PersonsCarPlateNumber" list="PersonsCarPlateNumbe ...