I want to make an event occur when the page scroll is at the top of the page.
Also I need an if
statement for it.
I am a beginner with javascript so any help is appreciated.
I am looking for something like this:
if (at_the_top_of_the_page) {
do_the_event_here
}
I think this is the right struckture for it. But I don't know what is the right code. I know that it will be in javascript. But I really don't know how...
You can check if window. scrollY (the number of pixels the window has scrolled vertically) is equal to 0 . If you want to check if the window has been scrolled to its leftermost, you can check if window. scrollX (the number of pixels the window has scrolled horizontally) is equal to 0 .
Use jQuery to Detect if User has Scrolled to the Bottom or Top of a Page. $(window). scrollTop() – returns the current vertical position of the scroll bar. So if the scroll bar is at the very top, the value of $(window).
If you want to check whether the user has scrolled to the bottom of the page, you can use the scroll() jQuery event. The given code piece takes the top scroll of the window, so how much the page is scrolled down, it adds the height of the visible window and checks if it is equivalent to the height of the document.
You can check the document. readyState property. From MDN: Returns "loading" while the document is loading, "interactive" once it is finished parsing but still loading sub-resources, and "complete" once it has loaded.
Elements have a scrollTop
element that can be read or set. Make sure you're reading the correct element's scrollTop, the one that you're scrolling.
ex:
var div = document.getElementById('scrollable');
if(div.scrollTop==0){
//Top of element
}
Get the position of the scrollbar using this
function Getpostion(){
var vscroll = document.body.scrollTop;
alert(vscroll);
}
if vscroll is zero do your job. More details
document.body.scrollTop === 0;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With