I grasped your desire to determine the amount of pixels that will be scrolled when a scroll event commences.
However, this is not solely a web or code issue. The scrolling action continues until the user decides to halt it.
This process is also influenced by the mouse wheel hardware. Different mice have varying wheels - some stop immediately upon releasing the finger, while others with softer gears allow for continued motion (in accordance with Newton's Law of Motion).
Web developers do not have control over future hardware performances such as predicting when a user will release a key or mouse button.
The only data available to us pertains to the hardware's current or completed actions like keyup, keydown, click, and scrolling.
These events provide insight into present activity and what occurred just before.
The only way to anticipate the duration of a scroll is through Machine Learning, by analyzing user scrolling patterns and understanding their scrolling device.
But why is this needed? One potential use case is developing a ML model to comprehend user scrolling behavior.
In a scenario where a user's scroll results in an offsetTop change between 200px to 1000px, can we predict this using JavaScript?
For this specific case, there is no necessity to obtain the scrolling pixels before halting. A basic scroll event is sufficient.
You might want to incorporate a small animation before reaching 1000px, which can be achieved directly through CSS and JavaScript by creating an event listener on scroll (e.g., if current offsetTop == 1000px then...)
This snippet informs you about the number of pixels scrolled during a scroll event (completed task).
const pageHeight=document.documentElement.scrollHeight;
console.log(pageHeight)
document.querySelector('.rem').innerHTML="You have scrolled "+window.pageYOffset+". Scroll ends after "+((pageHeight-window.innerHeight)-window.pageYOffset);
document.onscroll = () =>{
if(window.pageYOffset>1000){
var cl=document.getElementById("gm");
cl.classList.add("wow");
}
if(window.pageYOffset<1000){
var cl=document.getElementById("gm");
cl.classList.remove("wow");
}
document.querySelector('.rem').innerHTML="You have scrolled "+window.pageYOffset+". Scroll ends after "+((pageHeight-window.innerHeight)-window.pageYOffset);
};
.rem{
position:fixed;
top:0;
}
.demo{
height:5000px;
}
.wow{
background:#FFCB2B;
transition:2s;
}
<span class="rem" id="xy"></span>
<div class="demo" id="gm">
<div>
Indeed, I will create a simple machine learning JS script for tracking scroll events by studying user behavior.
Currently, I am focusing on training for unidirectional scrolling. Each scroll will either be upscroll or downscroll.
The first 10 scrolls will be dedicated to learning. Afterward, the script predicts upcoming scroll values based on the learned data.
The model continuously learns and improves, providing more realistic predictions with higher learning values.
const pageHeight=document.documentElement.scrollHeight;
const scrollPoints=[];
var prevScroll=0;
var currentScroll=0;
learnValue=0;
var scrollTimer = -1;
console.log(pageHeight)
document.querySelector('.rem').innerHTML="You have scrolled "+window.pageYOffset+". Scroll ends after "+((pageHeight-window.innerHeight)-window.pageYOffset);
document.onscroll = () =>{
if (scrollTimer != -1)
clearTimeout(scrollTimer);
scrollTimer = window.setTimeout("scrollFinished()", 300);
if(learnValue>=10){
var calculatedAvg=averageScroll()
document.querySelector('.rem').innerHTML="Your expected scroll pixels are "+calculatedAvg;
}
};
function averageScroll(){
let currentAvg=0
scrollPoints.forEach((item, index)=>{
if(index!=0)
currentAvg=(currentAvg+item)/(2)
else
currentAvg=item
});
return currentAvg;
}
function scrollFinished() {
learnValue=learnValue+1;
curPoint=window.pageYOffset
currentScroll=Math.abs(window.pageYOffset-prevScroll);
prevScroll=curPoint;
scrollPoints[learnValue]=currentScroll
console.log("Current Scroll was "+ currentScroll)
if(learnValue<=10)
document.querySelector('.rem').innerHTML="Current Scrolled pixels were "+currentScroll+" learning "+ learnValue + " of 10";
}
.rem{
position:fixed;
top:0;
}
.demo{
height:50000px;
}
<span class="rem" id="xy"></span>
<div class="demo">
<div>