Is it possible to change the scrollbar position when the user reaches a certain point scrolling down a web page? For example once you reached half way down down the page the scrollbar would move automatically back to the top.
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
The three scrolling functions you'll want to concern yourself with are
window.scroll(x,y)
,window.scrollBy(dx,dy)
, andwindow.scrollTo(x,y)
.As David mentioned you'll need the scroll position to know where you are and use the
window.onscroll
event to fire off this calculation.(window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop) ought to give you the current scroll position in just about any browser.
Yup, I've seen it a few times. Here is some JS code:
50 is the amount of pixels you want to scroll by.
You can calculate the percentage of the current position of the scrollbar using the onscroll event, and if it reaches the 50 % the scroll position can be set to the top of the page with the scrollTo function:
You can check my example here.