Ways to stop a scroll event once a certain amount of pixels have been scrolled

inputs :

  • 1 scroll down event scrolls 100 pixels.
  • An HTML element is positioned 5 pixels below the bottom of the page and not on screen.
  • A JavaScript function is triggered when at least 1 pixel of the HTML element comes on screen, and it:
  • automatically scrolls the page to bring the element fully into view.
  • prevents any other scroll events during the animation.

events :

  • the user performs a single scroll-down event (referred to as "the scroll event").

expected result :

  • After scrolling 6 pixels - where 1px of the element becomes visible - the scroll event should be halted, triggering the automatic scroll by the JavaScript function instead.

actual result :

  • Despite successfully preventing additional scroll events, there seems to be some conflict between the initial scroll event and the automatic scroll until the full 100px has been scrolled, resulting in a choppy transition.

I attempted to set "overflow:hidden" on the "body" during the animation, but this did not cancel the ongoing scroll event, which persisted even after the completion of the automatic scroll's animation (especially noticeable when using a laptop trackpad).

Any suggestions? Thank you!

P.S.: I am looking for solutions in pure native JavaScript.

Answer №1

To access the current vertical scroll position of a window, you can utilize the window.pageYOffset property.

For instance, consider the following scenario where the code enclosed within an if statement only executes when the scroll position is within the initial 100 pixels from the top of the page:

window.addEventListener("scroll", function(){
    if(window.pageYOffset < 100) {
        console.log("Greetings!");
    }
});

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

Having difficulty positioning breadcrumb items to float on the right side

As I create my Vue.js and Bootstrap application, I am working on a breadcrumb feature that presents some challenges. Specifically, I am trying to align the icons to the right while ensuring that the text "Files" remains floated to the left. <script s ...

Is there a way to implement a css/javascript property specifically for a div that is selected in the url?

Take for instance the scenario where I possess the URL example.com/#foo. In this case, CSS styling will be directed to the div with the id foo. If achieving this solely in CSS is not feasible, what methods in JavaScript or jQuery can be used efficiently ...

PHP: Unable to save uploaded image to directory

Seeking some assistance here. I have a script that inserts data into the database, including an image file uploaded to a folder named "uploads". The issue is that although the data goes into the database successfully, the image fails to upload to the folde ...

Navigation bar featuring various distinct webpages

Apologies for the novice question, but as a beginner, I'm seeking help. I've created a navigation bar with text such as About and Projects, but no links attached (i.e., no pages for users to navigate to). How can I add these missing pages so that ...

Switch up Three.js texture in externally imported Collada model

Currently, I am using the three.js collada loader to bring in an .dae file that includes a texture (.png) image. The challenge I am facing involves replacing this original .png file with a new texture generated from a canvas element and saved in .png forma ...

Tips for modifying the href attribute when a user clicks

Is there a way to dynamically change the value of this link <a href="/Countries/388/10">next</a> without having to refresh the page? For example, if a user clicks on the link, it should update to <a href="/Countries/388/20">next</a&g ...

Vue Filtering and Pagination Implementation

In Vue pagination, how can you control the number of array objects to be displayed based on a user-selected amount like 10, 15, or 25? I successfully implemented rendering 10 items per page and it's functioning smoothly. ...

Stopping videos in the JQuery Cycle Plugin triggers the stop event

Currently, I have a simple cycle set up with YouTube videos using the following code: <script type="text/javascript"> $(document).ready(function () { $('#cycle').cycle({ fx: 'fade', sp ...

interact with the server in order to make changes to a document

I need to create a console program that can communicate with a server without requiring users to log in. The program should have the capability to write to a file on the server, such as incrementing a number in a text file every time it is run. For example ...

"Enhancing User Experience with Stylish Drop-down Menu Hover

I'm working on a CSS drop-down menu that has a hover effect, but I'm facing an issue where the a element within the li is not taking up the full width of the li. I want it to utilize the entire width of the li, especially on hover. Does anyone ha ...

The parent's backdrop remains static and does not shift or change

I am looking to create a transition effect for when I hover over a link that will smoothly display text afterwards. This is the current code I have: p { border-radius: 1em; padding: 0.5em; background-color: chocolate; display: inline-bl ...

Retrieve various key-value pairs from the JSON data in the global market API using AJAX

I have recently developed a real-time API specifically designed for monitoring World Stock Markets. This API covers popular indices such as Nifty, Dow Jones, Nasdaq, and SGX Nifty. If you are interested in accessing this Real Time API, you can do so by vi ...

Center and align items vertically within a Bootstrap 4 div, or align them to the

Using Bootstrap 4, the .top block should span 100% width and the full height of the screen. The .top-title should be centered on the screen, while the .btn-scroll-down should appear at the bottom of the screen. The desired page layout can be viewed at thi ...

In a React component, clicking on a download anchor tag will open the file in a browser without automatically initiating the

I am currently working on a React component where I have implemented a download function to download a file upon clicking: <a key={file.attachmentId} className="item uploaded" href={Pathes.Attachments.getById(file.attachmentId)} target="_blank" ...

AngularJS showcases the full spectrum of available methods

I am struggling to create a method for composing a URL in my class. Here is what I have attempted: The createLink method takes an ID and returns the corresponding URL: console.log("before the method"); console.log($scope.createLink ) $scope.createLink = f ...

The NextJS homepage and API endpoint are combined on a single page

In my Next.js application, I have multiple pages and I want the component loaded at http://localhost:3000/someEndpoint to be exactly the same as the one loaded at http://localhost:3000/ I have already created an index.js file, but I am unsure how to creat ...

Is your Ajax response suddenly failing to work after the initial attempt?

Describing my predicament: The code snippet below is what I have been using to insert a custom-designed div into my webpage. Initially, the div is successfully added; however, it stops working after the first instance. $('#addanother').click(fu ...

What causes a CSS underline effect to not be applied consistently across all elements?

Currently, I am working on a VUEJS application and have encountered an issue with one of the components. The component applies an underline to each 'p' element with the class 'animal-subtitle' using CSS. While this effect works fine in ...

Transmitting a 2D array to the server and retrieving it using JavaScript

I used to send data to the server using a one-dimensional array in C Arduino language. Now, I need to figure out how to send a two-dimensional array instead. typedef struct { String Name; String addr; String ttl; }officeKownNetworks; officeKownNetwo ...

Creating a script for an automated clicker that targets a specific link on a webpage using Chrome

With my limited coding experience, I find myself at a loss on how to tackle this task. I am attempting to navigate Fortune Tech to catch up on news from the majority of 2016. However, I encountered a link that appears when you reach the end of the page, p ...