jquery.scrollto

repository·master·Indexed 25 days ago

https://github.com/flesler/jquery.scrollto

A lightweight, cross-browser, and highly customizable jQuery plugin for performing animated scrolling within a scrollable element or the window. Version 2.1.3 requires jQuery 1.8 or higher. It supports various target formats including selectors, DOM elements, coordinates, and the 'max' position. Key features include axis control, animation interrupts, and a global defaults configuration via $.scrollTo.defaults.

Tokens
1.6K
Snippets
4
Records
10
Agent score
37%

What's inside jquery.scrollto

  1. Stop a jQuery.scrollTo animation

    master

    Since the plugin uses standard jQuery animations, you can stop an ongoing scroll by calling $.stop() or $.finish() on the element that was being scrolled (including $(window)).

    You can use the fail() callback to execute code when the animation is stopped (for example, if interrupt: true is set).

  2. Install jQuery.scrollTo

    master

    The plugin requires jQuery 1.8 or higher. You can install it via several package managers or use a public CDN.

    npm

    npm install jquery.scrollto

    bower

    bower install jquery.scrollTo

    Composer (Packagist)

    php composer.phar require --prefer-dist flesler/jquery.scrollto "*"

    Public CDN (jsdelivr)

    <script src="https://cdn.jsdelivr.net/npm/jquery.scrollto@2.1.3/jquery.scrollTo.min.js"></script>

    Public CDN (cdnjs)

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-scrollTo/2.1.3/jquery.scrollTo.min.js"></script>
  3. Configure jQuery.scrollTo settings

    master

    The settings object allows you to customize the scrolling behavior. You can also include any options supported by jQuery's $.animate().

    Plugin-specific settings:

    • axis: The axes to animate: xy (default), x, y, yx
    • interrupt: If true, cancels the animation if the user scrolls. Default is false
    • limit: If true, the plugin will not scroll beyond the container's size. Default is true
    • margin: If true, subtracts the margin and border of the target element. Default is false
    • offset: Added to the final position (number or {left, top})
    • over: Adds a % of the target dimensions (e.g., {left:0.5, top:0.5})
    • queue: If true, scrolls one axis and then the other. Default is false
    • onAfter(target, settings): Callback triggered when the animation ends.
    • onAfterFirst(target, settings): Callback triggered after the first axis scrolls when queue is true.

    jQuery animate() settings:

    • duration: Duration of the animation (default is 0, which is instantaneous)
    • easing: Name of an easing equation (e.g., "swing")
    • fail(): Callback triggered when the animation is stopped (e.g., via interrupt)
    • step(): Callback triggered for every animated property on every frame
    • progress(): Callback triggered on every frame
  4. Troubleshoot jQuery.scrollTo issues

    master

    Error: Cannot read property 'propHooks' of undefined This occurs if you are using the slim version of jQuery. The slim version does not include the effects module required by this plugin. Switch to the full version of jQuery.

    Issue: The element doesn't scroll Verify that the element is actually scrollable by temporarily setting overflow: auto in your CSS. If no scrollbar appears, the issue is likely in your HTML/CSS structure rather than the plugin.

  5. Handle onAfter callback with requestAnimationFrame

    master

    The onAfter callback is triggered before the scroll event fires. If you need to perform DOM manipulations that depend on the final scroll position, it is recommended to wrap your logic in requestAnimationFrame to ensure it runs on the next tick.

    $.scrollTo(100, {
      onAfter: function() {
        requestAnimationFrame(function() {
            $(".result").addClass("selected");
        });
      }
    });
  6. Use the jQuery.scrollTo API

    master

    The plugin's signature resembles $(element).animate(). Use it to scroll a specific element to a target position.

    Signature: $(element).scrollTo(target[,duration][,settings]);

    • element: The scrollable element. Use $(window) to scroll the entire window.
    • target: The destination position. Supported formats include:
      • A number: 250
      • A string with px: "250px"
      • A percentage string: "50%"
      • A relative step string: "+=50px"
      • An object: {left:250, top:"50px"}
      • The string "max" to scroll to the end.
      • A selector string: ".section:eq(2)"
      • A DOM element: document.getElementById("top")
      • A jQuery object: $("#top")
    • duration: (Optional) Duration of the animation. Shortcut for the duration setting.
    • settings: (Optional) Configuration object for the scroll behavior.

    Window Shorthand: You can use $.scrollTo(...) as a shorthand for $(window).scrollTo(...).

    $(element).scrollTo(target[,duration][,settings]);
  7. Configure .scrollTo() settings

    master

    You can pass a settings object as the third argument to .scrollTo() to customize the animation behavior.

    Available Settings:

    • axis: String specifying the axes to scroll. Options: 'x', 'y', or 'xy' (default: 'xy').
    • duration: Animation duration in milliseconds (default: 0).
    • limit: Boolean. If true, prevents scrolling beyond the boundaries of the element (default: true).
    • offset: A number, object { top, left }, or a function function(elem, target) to adjust the scroll position.
    • over: An object { top, left } representing a fraction of the target's dimension to scroll past (e.g., { top: 0.5 } scrolls to the middle of the target).
    • margin: Boolean. If true, accounts for the target's margin and border width.
    • queue: Boolean. If true and axis is 'xy', it performs the scroll in two steps (one axis then the other).
    • onAfter: Function called when the animation completes.
    • onAfterFirst: Function called after the first axis animation completes (used when queue: true).
    • interrupt: (Via jQuery Tween) If true, stops the animation if the user manually scrolls during the animation.
  8. Use the .scrollTo() jQuery method

    master

    The .scrollTo() method is available on both the jQuery object (for scrolling specific elements) and the global $.scrollTo function (for scrolling the window). It allows for animated scrolling to a target element, a specific coordinate, or a maximum scroll position.

    Target Types:

    • DOM Element / jQuery Object: Scrolls to the position of the element.
    • Selector String: Scrolls to the element matching the selector.
    • Number or String (e.g., '100', '50px', '10%'): Scrolls to that specific coordinate.
    • 'max': Scrolls to the maximum possible scroll position.
    • null: Does nothing.
  9. Get the maximum scroll position with $.scrollTo.max()

    master

    The $.scrollTo.max(elem, axis) method returns the maximum possible scrollable value for a given element and axis. This is useful for determining the boundaries of a scrollable container.

    • elem: The element or window to check.
    • axis: 'x' or 'y'.