Convert Figma logo to code with AI

flesler logojquery.scrollTo

Lightweight, cross-browser and highly customizable animated scrolling with jQuery

3,672
1,026
3,672
0

Top Related Projects

The javascript library for magical scroll interactions.

A lightweight script to animate scrolling to anchor links.

🛤 Detection of elements in viewport & smooth scrolling with parallax.

26,486

Animate on scroll library

fullPage plugin by Alvaro Trigo. Create full screen pages fast and simple

28,370

the last carousel you'll ever need

Quick Overview

The jquery.scrollTo library is a lightweight and flexible jQuery plugin that allows you to smoothly scroll the browser window to a specific element or position on the page. It provides a simple and consistent API for controlling the scrolling behavior across different browsers and devices.

Pros

  • Lightweight and Efficient: The library is small in size, making it suitable for use in performance-sensitive web applications.
  • Cross-Browser Compatibility: The plugin works consistently across a wide range of modern browsers, ensuring a seamless user experience.
  • Customizable Behavior: Users can easily configure the scrolling duration, easing function, and other parameters to match their specific requirements.
  • Extensive Documentation and Examples: The project's documentation provides clear and comprehensive guidance, making it easy for developers to get started and integrate the library into their projects.

Cons

  • Dependency on jQuery: The library requires the use of the jQuery library, which may not be necessary for all projects.
  • Limited Functionality: While the library provides a solid set of features for basic scrolling needs, it may lack advanced functionality compared to some other scrolling libraries.
  • Potential Performance Issues: Depending on the complexity of the page and the frequency of scrolling, the library's performance may degrade in certain scenarios.
  • Potential Maintenance Concerns: The project's last commit was in 2016, which may raise concerns about ongoing maintenance and support.

Code Examples

Here are a few examples of how to use the jquery.scrollTo library:

  1. Scroll to a specific element:
// Scroll to the element with the ID "target"
$.scrollTo('#target', 800, {
  offset: -50, // Adjust the vertical offset (in pixels)
  easing: 'swing' // Set the easing function
});
  1. Scroll to a specific position on the page:
// Scroll to a specific position (x, y)
$.scrollTo(500, 300, {
  duration: 1000, // Set the scroll duration (in milliseconds)
  axis: 'xy' // Scroll on both the x and y axes
});
  1. Scroll relative to the current position:
// Scroll down by 200 pixels
$.scrollTo('+=200', {
  duration: 400, // Set the scroll duration (in milliseconds)
  interrupt: true // Allow interrupting the current scroll
});
  1. Scroll to an element within a container:
// Scroll to the element with the ID "target" within the container with the ID "container"
$.scrollTo('#container', 600, {
  target: '#target',
  offset: { top: -20, left: 0 } // Adjust the offset (in pixels)
});

Getting Started

To use the jquery.scrollTo library in your project, follow these steps:

  1. Include the jQuery library in your HTML file:
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  1. Include the jquery.scrollTo.min.js file in your HTML file:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-scrollTo/2.1.3/jquery.scrollTo.min.js"></script>
  1. Use the $.scrollTo() function to scroll to a specific element or position on the page:
// Scroll to the element with the ID "target"
$.scrollTo('#target', 800);
  1. Customize the scrolling behavior by passing an options object as the third argument to the $.scrollTo() function:
$.scrollTo('#target', 800, {
  offset: -50, // Adjust the vertical offset (in pixels)
  easing: 'swing' // Set the easing function
});

For more advanced usage and configuration options, please refer to the project's documentation.

Competitor Comparisons

The javascript library for magical scroll interactions.

Pros of ScrollMagic

  • ScrollMagic provides a more comprehensive set of features and functionality compared to jQuery.scrollTo, including the ability to create complex scrolling animations and interactions.
  • ScrollMagic has a more robust and flexible API, allowing for greater customization and control over the scrolling behavior.
  • ScrollMagic integrates well with other popular libraries and frameworks, such as GSAP, making it a versatile choice for building complex web applications.

Cons of ScrollMagic

  • ScrollMagic has a steeper learning curve compared to jQuery.scrollTo, as it requires a deeper understanding of the library's API and features.
  • ScrollMagic may be overkill for simple scrolling use cases, where jQuery.scrollTo may be a more lightweight and straightforward solution.
  • ScrollMagic has a larger file size and dependency footprint compared to jQuery.scrollTo, which may be a consideration for performance-sensitive projects.

Code Comparison

jQuery.scrollTo:

$('html, body').scrollTo('#target', 800, {
  offset: -100,
  easing: 'swing'
});

ScrollMagic:

var controller = new ScrollMagic.Controller();
var scene = new ScrollMagic.Scene({
  triggerElement: '#trigger',
  duration: 500
})
.setPin('#target')
.addTo(controller);

A lightweight script to animate scrolling to anchor links.

Pros of Smooth Scroll

  • Lightweight: Smooth Scroll is a lightweight library, weighing in at around 3KB minified and gzipped, making it a great choice for performance-conscious projects.
  • Customizable: The library offers a wide range of options for customizing the scrolling behavior, such as speed, easing, and offset.
  • Accessibility: Smooth Scroll includes support for keyboard navigation, making it more accessible for users who prefer not to use a mouse.

Cons of Smooth Scroll

  • Limited Functionality: Compared to jQuery.scrollTo, Smooth Scroll has a more limited set of features, such as the lack of support for scrolling to specific elements or coordinates.
  • Dependency on jQuery: While Smooth Scroll can be used without jQuery, it is primarily designed to work with the jQuery library, which may not be suitable for all projects.

Code Comparison

jQuery.scrollTo:

$('html, body').scrollTo('#target', 800, {
  offset: -50,
  easing: 'swing'
});

Smooth Scroll:

smoothScroll.animateScroll('#target', null, {
  offset: 50,
  speed: 800,
  easing: 'easeInOutCubic'
});

In the above examples, both libraries are used to scroll the page to an element with the ID #target. The key differences are:

  • jQuery.scrollTo uses the $('html, body').scrollTo() syntax, while Smooth Scroll uses the smoothScroll.animateScroll() method.
  • jQuery.scrollTo accepts the target, duration, and options as separate arguments, while Smooth Scroll uses a single options object.
  • The option names also differ slightly, with jQuery.scrollTo using offset and easing, while Smooth Scroll uses offset and speed (with easing as a separate option).

🛤 Detection of elements in viewport & smooth scrolling with parallax.

Pros of Locomotive Scroll

  • Smooth Scrolling: Locomotive Scroll provides a highly customizable and smooth scrolling experience, with support for various easing functions and scroll behaviors.
  • Parallax Effects: The library offers built-in support for parallax effects, allowing for more engaging and visually appealing scrolling experiences.
  • Responsive Design: Locomotive Scroll is designed to work well across different screen sizes and devices, making it a suitable choice for responsive web applications.

Cons of Locomotive Scroll

  • Larger File Size: Locomotive Scroll has a larger file size compared to jQuery.scrollTo, which may impact the initial load time of your web application.
  • Dependency on Third-Party Libraries: Locomotive Scroll relies on the GSAP (GreenSock Animation Platform) library, which adds an additional dependency to your project.
  • Learning Curve: Locomotive Scroll has a more complex API and configuration options compared to the simpler jQuery.scrollTo, which may require more time to learn and integrate into your project.

Code Comparison

jQuery.scrollTo

$('html, body').scrollTo(target, {
  duration: 800,
  offset: -50,
  easing: 'swing'
});

Locomotive Scroll

const scroll = new LocomotiveScroll({
  el: document.querySelector('[data-scroll-container]'),
  smooth: true,
  smartphone: {
    smooth: true
  },
  tablet: {
    smooth: true
  }
});

scroll.scrollTo(target, {
  offset: -50,
  duration: 800,
  easing: 'easeInOutCubic'
});
26,486

Animate on scroll library

Pros of AOS

  • AOS provides a wide range of animation effects, including fade, zoom, flip, and more, allowing for more creative and visually appealing page interactions.
  • AOS is designed to be easy to use, with a simple API and straightforward configuration options.
  • AOS is actively maintained and has a large community of contributors, ensuring ongoing support and improvements.

Cons of AOS

  • AOS may have a larger footprint than jquery.scrollTo, as it includes additional animation features beyond just scrolling.
  • AOS may have a steeper learning curve for developers who are already familiar with jquery.scrollTo and its simpler API.
  • AOS may not be as performant as jquery.scrollTo for simple scrolling tasks, as it includes additional overhead for the animation features.

Code Comparison

jquery.scrollTo:

$('html, body').scrollTo(target, 800, {
  offset: -100,
  easing: 'swing'
});

AOS:

AOS.init({
  duration: 800,
  easing: 'ease-in-out-sine',
  once: true
});

fullPage plugin by Alvaro Trigo. Create full screen pages fast and simple

Pros of fullPage.js

  • Provides a full-page scrolling experience with smooth transitions and customizable options.
  • Supports vertical and horizontal scrolling, as well as callbacks and events for advanced functionality.
  • Includes a variety of pre-built section designs and themes, making it easy to create visually appealing layouts.

Cons of fullPage.js

  • Can be more complex to set up and configure compared to a simpler scrolling library like jQuery.scrollTo.
  • May have a larger file size and dependencies, which could impact page load times.
  • Might not be as flexible or customizable as a more lightweight scrolling solution.

Code Comparison

jQuery.scrollTo:

$('html, body').animate({
    scrollTop: $(target).offset().top
}, 1000);

fullPage.js:

new fullpage('#fullpage', {
    anchors: ['firstPage', 'secondPage', 'thirdPage', 'fourthPage'],
    navigation: true
});
28,370

the last carousel you'll ever need

Pros of Slick

  • Slick provides a wide range of customization options, allowing developers to tailor the carousel to their specific needs.
  • The library is well-documented, with a comprehensive set of examples and a detailed API reference.
  • Slick has a large and active community, with regular updates and bug fixes.

Cons of Slick

  • Slick may have a steeper learning curve compared to jQuery.scrollTo, as it has a more complex API and configuration options.
  • The library can be heavier and more resource-intensive than jQuery.scrollTo, especially for simpler carousel use cases.

Code Comparison

jQuery.scrollTo:

$('html, body').animate({
  scrollTop: $('#target').offset().top
}, 800);

Slick:

$('.your-class').slick({
  slidesToShow: 3,
  slidesToScroll: 1,
  autoplay: true,
  autoplaySpeed: 2000,
});

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

jQuery.scrollTo

Lightweight, cross-browser and highly customizable animated scrolling with jQuery

GitHub version

Installation

The plugin requires jQuery 1.8 or higher.

Via bower:

bower install jquery.scrollTo

Via npm:

npm install jquery.scrollto

Via packagist:

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

Using a public CDN

CDN provided by jsdelivr

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

CDN provided by cdnjs

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

Downloading Manually

If you want the latest stable version, get the latest release from the releases page.

2.0

Version 2.0 has been recently released. It is mostly backwards compatible, if you have any issue first check this link. If your problem is not solved then go ahead and report the issue.

Usage

jQuery.scrollTo's signature is designed to resemble $().animate().

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

element

This must be a scrollable element, to scroll the whole window use $(window).

target

This defines the position to where element must be scrolled. The plugin supports all these formats:

  • A number with a fixed position: 250
  • A string with a fixed position with px: "250px"
  • A string with a percentage (of container's size): "50%"
  • A string with a relative step: "+=50px"
  • An object with left and top containining any of the aforementioned: {left:250, top:"50px"}
  • The string "max" to scroll to the end.
  • A string selector that will be relative to the element to scroll: ".section:eq(2)"
  • A DOM element, probably a child of the element to scroll: document.getElementById("top")
  • A jQuery object with a DOM element: $("#top")

settings

The duration parameter is a shortcut to the setting with the same name. These are the supported settings:

  • axis: The axes to animate: xy (default), x, y, yx
  • interrupt: If true will cancel 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, can be a number or an object with left and top
  • over: Adds a % of the target dimensions: {left:0.5, top:0.5}
  • queue: If true will scroll one axis and then the other. Default is false
  • onAfter(target, settings): A callback triggered when the animation ends (jQuery's complete())
  • onAfterFirst(target, settings): A callback triggered after the first axis scrolls when queueing

You can add any setting supported by $().animate() as well:

  • duration: Duration of the animation, default is 0 which makes it instantaneous
  • easing: Name of an easing equation, you must register the easing function: swing
  • fail(): A callback triggered when the animation is stopped (f.e via interrupt)
  • step(): A callback triggered for every animated property on every frame
  • progress(): A callback triggered on every frame
  • And more, check jQuery's documentation

window shorthand

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

Changing the default settings

As with most plugins, the default settings are exposed so they can be changed.

$.extend($.scrollTo.defaults, {
  axis: 'y',
  duration: 800
});

Stopping the animation

jQuery.scrollTo ends up creating ordinary animations which can be stopped by calling $().stop() or $().finish() on the same element you called $().scrollTo(), including the window. Remember you can pass a fail() callback to be called when the animation is stopped.

onAfter and requestAnimationFrame

jQuery.scrollTo has a onAfter callback for work that runs after the animation finishes. It will be called before the scroll event fires. To combat this you can use requestAnimationFrame to do work on the next tick. It is available in many browsers, but you may want to polyfill for the few it does not support.

$.scrollTo(100, {
  onAfter: function() {
    requestAnimationFrame(function() {
        $(".result").addClass("selected");
    });
  }
});

Demo

Check the demo to see every option in action.

Complementary plugins

There are two plugins, also created by me that depend on jQuery.scrollTo and aim to simplify certain use cases.

jQuery.localScroll

This plugin makes it very easy to implement anchor navigation. If you don't want to include another plugin, you can try using something like this minimalistic gist.

jQuery.serialScroll

This plugin simplifies the creation of scrolling slideshows.

Troubleshooting

  • Cannot read property 'propHooks' of undefined: You are most likely using the slim version jQuery, which doesn't include the effects module. The plugin cannot function without it and you'll need the full version.

  • The element doesn't scroll: A good approach to debug is to set overflow: auto temporarily to the scrollable element, to make sure it would be manually scrollable. If you don't see any scrollbar, the problem is likely in the HTML/CSS.

License

(The MIT License)

Copyright (c) 2007 Ariel Flesler aflesler@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

NPM DownloadsLast 30 Days