Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

License

NotificationsYou must be signed in to change notification settings

flesler/jquery.scrollTo

Repository files navigation

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

GitHub version

Installation

The plugin requires jQuery 1.8 or higher.

Viabower:

bower install jquery.scrollTo

Vianpm:

npm install jquery.scrollto

Viapackagist:

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

Using a public CDN

CDN provided byjsdelivr

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

CDN provided bycdnjs

<scriptsrc="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 thereleases page.

2.0

Version 2.0 has been recently released. It is mostly backwards compatible, if you have any issue first checkthis link.If your problem is not solved then go ahead andreport 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 whereelement 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 withleft andtop 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

Theduration 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: Iftrue will cancel the animation if the user scrolls. Default isfalse
  • limit: Iftrue the plugin will not scroll beyond the container's size. Default istrue
  • margin: Iftrue, subtracts the margin and border of thetarget element. Default isfalse
  • offset: Added to the final position, can be a number or an object withleft andtop
  • over: Adds a % of thetarget dimensions:{left:0.5, top:0.5}
  • queue: Iftrue will scroll oneaxis and then the other. Default isfalse
  • onAfter(target, settings): A callback triggered when the animation ends (jQuery'scomplete())
  • 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 is0 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 viainterrupt)
  • step(): A callback triggered for every animated property on every frame
  • progress(): A callback triggered on every frame
  • And more, check jQuery'sdocumentation

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 thewindow.Remember you can pass afail() callback to be called when the animation is stopped.

onAfter and requestAnimationFrame

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

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

Demo

Check thedemo 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.

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

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 setoverflow: 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 Flesleraflesler@gmail.com

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

The above copyright notice and this permission notice shall beincluded 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 OFMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANYCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THESOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors15


[8]ページ先頭

©2009-2025 Movatter.jp