Winceptor created this userscript, which uses JavaScript to enable smooth page scrolling. I just made it better.
当前为
This script enhances the scrolling experience on web pages by implementing smooth scrolling. It calculates and animates scroll positions frame-by-frame, making the scrolling look natural and controlled.
You can customize the behavior of the smooth scrolling by modifying the settings in the script:
Smoothscroll.Smoothness: Controls how smooth the scrolling effect is. Increase for smoother scrolling.Smoothscroll.Acceleration: Determines the rate of acceleration for the scrolling. Adjust for faster or slower acceleration.Smoothscroll.Debug: A debug level setting (currently unused).Scroll and Animation Functions:
ScrollSubpixels(element, newvalue): Manages subpixel scrolling values.ScrollPixels(element, newvalue): Manages pixel-level scrolling values and resets subpixels.AnimateScroll(target, refreshrate): The core function that animates the scrolling effect, adjusting scroll positions based on the smoothness setting and current scroll deltas.RequestAnimationUpdate(cb): Uses requestAnimationFrame to schedule the next frame of the animation, ensuring smooth updates based on the frame time.Control Functions:
Smoothscroll.Stop(target): Stops the scrolling animation for a given target element.Smoothscroll.Start(target, scrollamount): Starts the scrolling animation for a given target element and scroll amount.Utility Functions:
CanScroll(element, dir): Checks if an element can scroll in a given direction.HasScrollbar(element): Checks if an element has a visible scrollbar.Scrollable(element, dir): Combines the above checks to determine if an element is scrollable in a given direction.GetPath(e): Retrieves the event path (compatibility for different browsers).GetTarget(e): Determines the target element for the scroll event.GetStyleProperty(el, styleprop): Retrieves a style property value of an element.Event Handlers:
StopScroll(e): Stops scrolling on all elements in the event path.StartScroll(e, target): Initiates the scroll animation based on the wheel event’s delta values and the target element’s properties.WheelEvent(e): Handles mouse wheel events, starting the scroll animation if a valid target is found.ClickEvent(e): Stops scrolling on click events.Initialization:
Init(): Initializes the smooth scrolling functionality, attaching the wheel and mouse down event listeners to the document. Ensures only one instance of the smooth scrolling functionality is active.