Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

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

Let your page react to scroll changes.

License

NotificationsYou must be signed in to change notification settings

terwanerik/ScrollTrigger

Repository files navigation

BuildDeployLicenseIssuesGitHub releasenpm (scoped)

Let your page react to scroll changes.

The most basic usage of ScrollTrigger is to trigger classes based on the current scroll position. E.g. when an element enters the viewport, fade it in. You can add custom offsets per element, or set offsets on the viewport (e.g. always trigger after the element reaches 20% of the viewport)

When using the callbacks ScrollTrigger becomes really powerfull. You can run custom code when an element enters / becomes visible, and even return Promises to halt the trigger if the callback fails. This makes lazy loading images very easy.

Installation

npm install @terwanerik/scrolltrigger or just add thedist/ScrollTrigger.min.js file to your project and import that.

How to use?

The easiest way to start is to create a new instance and add some triggers to it, with all default values. This will toggle the 'visible' class when the element comes into the viewport, and toggles the 'invisible' class when it scrolls out of the viewport.

// when using ES6 import / npmimportScrollTriggerfrom'@terwanerik/scrolltrigger'// Create a new ScrollTrigger instance with default optionsconsttrigger=newScrollTrigger()// When not using npm, create a new instance with 'new ScrollTrigger.default()'// Add all html elements with attribute data-triggertrigger.add('[data-trigger]')

Now in your CSS add the following classes, this fades the[data-trigger] elements in and out.

.visible, .invisible {opacity:0.0;transition: opacity0.5s ease;}.visible {opacity:1.0;}

⚠️AttentionAre you migrating from 0.x to 1.x?Checkout the migration guide!

Some more demo's

A more detailed example

Adding callbacks / different classes can be done globally, this becomes the default for all triggers you add, or you can specify custom configuration when adding a trigger.

// Create a new ScrollTrigger instance with some custom optionsconsttrigger=newScrollTrigger({trigger:{once:true}})// Add all html elements with attribute data-trigger, these elements will only be triggered oncetrigger.add('[data-trigger]')// Add all html elements with attribute data-triggerAlways, these elements will always be triggeredtrigger.add('[data-triggerAlways]',{once:false})

Options

The full set of options is taken from thedemo directory, for more info, check that out.

consttrigger=newScrollTrigger({// Set custom (default) options for the triggers, these can be overwritten// when adding new triggers to the ScrollTrigger instance. If you pass// options when adding new triggers, you'll only need to pass the object// `trigger`, e.g. { once: false }trigger:{// If the trigger should just work one timeonce:false,offset:{// Set an offset based on the elements position, returning an// integer = offset in px, float = offset in percentage of either// width (when setting the x offset) or height (when setting y)//// So setting an yOffset of 0.2 means 20% of the elements height,// the callback / class will be toggled when the element is 20%// in the viewport.element:{x:0,y:(trigger,rect,direction)=>{// You can add custom offsets according to callbacks, you// get passed the trigger, rect (DOMRect) and the scroll// direction, a string of either top, left, right or// bottom.return0.2}},// Setting an offset of 0.2 on the viewport means the trigger// will be called when the element is 20% in the viewport. So if// your screen is 1200x600px, the trigger will be called when the// user has scrolled for 120px.viewport:{x:0,y:(trigger,frame,direction)=>{// We check if the trigger is visible, if so, the offset// on the viewport is 0, otherwise it's 20% of the height// of the viewport. This causes the triggers to animate// 'on screen' when the element is in the viewport, but// don't trigger the 'out' class until the element is out// of the viewport.// This is the same as returning Math.ceil(0.2 * frame.h)returntrigger.visible ?0 :0.2}}},toggle:{// The class(es) that should be toggledclass:{in:'visible',// Either a string, or an array of stringsout:['invisible','extraClassToToggleWhenHidden']},callback:{// A callback when the element is going in the viewport, you can// return a Promise here, the trigger will not be called until// the promise resolves.in:null,// A callback when the element is visible on screen, keeps// on triggering for as long as 'sustain' is setvisible:null,// A callback when the element is going out of the viewport.// You can also return a promise here, like in the 'in' callback.//// Here an example where all triggers take 10ms to trigger// the 'out' class.out:(trigger)=>{// `trigger` contains the Trigger object that goes out// of the viewportreturnnewPromise((resolve,reject)=>{setTimeout(resolve,10)})}}},},// Set custom options and callbacks for the ScrollAnimationLoopscroll:{// The amount of ms the scroll loop should keep triggering after the// scrolling has stopped. This is sometimes nice for canvas// animations.sustain:200,// Window|HTMLDocument|HTMLElement to check for scroll eventselement:window,// Add a callback when the user has scrolled, keeps on triggering for// as long as the sustain is set to docallback:didScroll,// Callback when the user started scrollingstart:()=>{},// Callback when the user stopped scrollingstop:()=>{},// Callback when the user changes direction in scrollingdirectionChange:()=>{}}})/*** ** Methods on the ScrollTrigger instance ***//** * Creates a Trigger object from a given element and optional option set *@param {HTMLElement} element *@param {DefaultOptions.trigger} [options=DefaultOptions.trigger] options *@returns Trigger */trigger.createTrigger(element,options)/** * Creates an array of triggers *@param {HTMLElement[]|NodeList} elements *@param {Object} [options=null] options *@returns {Trigger[]} Array of triggers */trigger.createTriggers(elements,options)/** * Adds triggers *@param {string|HTMLElement|NodeList|Trigger|Trigger[]} objects A list of objects or a query *@param {Object} [options=null] options *@returns {ScrollTrigger} */trigger.add(objects,options)/** * Removes triggers *@param {string|HTMLElement|NodeList|Trigger|Trigger[]} objects A list of objects or a query *@returns {ScrollTrigger} */trigger.remove(objects)/** * Lookup one or multiple triggers by a query string *@param {string} selector *@returns {Trigger[]} */trigger.query(selector)/** * Lookup one or multiple triggers by a certain HTMLElement or NodeList *@param {HTMLElement|HTMLElement[]|NodeList} element *@returns {Trigger|Trigger[]|null} */trigger.search(element)/** * Reattaches the scroll listener */trigger.listen()/** * Kills the scroll listener */trigger.kill()/*** ** Methods on a Trigger instance, e.g. when receiving from a callback or from a query ***/constreceivedTrigger=newTrigger()/** * The HTML element */receivedTrigger.element/** * The offset settings */receivedTrigger.offset/** * The toggle settings */receivedTrigger.toggle/** * If the trigger should fire once, boolean */receivedTrigger.once/** * If the trigger is visible, boolean */receivedTrigger.visible

Migrating from 0.x to 1.x

The main differences between0.x and1.x are the way you add and configure yourtriggers.0.x added all HTMLElement's with the data-scroll attribute by default,1.x doesn't do that, this requires you to add the triggers yourself. This improvesthe configuration of the triggers.

Also, please note that whennot using a package manager / webpack, and you'rejust importing the minified version, you'll have to always usenew ScrollTrigger.default().

<scriptsrc="dist/ScrollTrigger.min.js"></script><script>vartrigger=newScrollTrigger.default()</script>

Take for example the following element in ScrollTrigger0.x:

<divdata-scroll="once addHeight"data-scroll-showCallback="alert('Visible')"data-scroll-hideCallback="alert('Invisible')"></div>

In ScrollTrigger1.x you would write this mostly in #"auto" data-snippet-clipboard-copy-content="// Say you have some divs with class 'animateMe'const scrollTrigger = new ScrollTrigger()scrollTrigger.add('.animateMe', { once: true, // same functionality as the `once` flag in v0.x offset: { element: { y: 1.0 // note that we pass a float instead of an integer, when the // offset is a float, it takes it as a percentage, in this // case, add 100% of the height of the element, the same // functionality as the `addHeight` flag in v0.x } }, toggle: { callback: { in: () => { // same as the data-scroll-showCallback, no need to set a // custom callScope when calling custom functions and // the possibility to return a Promise alert('Visible') }, out: () => { // same as the data-scroll-hideCallback alert('Invisible') } } }})">

// Say you have some divs with class 'animateMe'constscrollTrigger=newScrollTrigger()scrollTrigger.add('.animateMe',{once:true,// same functionality as the `once` flag in v0.xoffset:{element:{y:1.0// note that we pass a float instead of an integer, when the// offset is a float, it takes it as a percentage, in this// case, add 100% of the height of the element, the same// functionality as the `addHeight` flag in v0.x}},toggle:{callback:{in:()=>{// same as the data-scroll-showCallback, no need to set a// custom callScope when calling custom functions and// the possibility to return a Promisealert('Visible')},out:()=>{// same as the data-scroll-hideCallbackalert('Invisible')}}}})

The advantage of writing all this in javascript is the configuration possible, sayi want to change the offset of the element after the first time it's been visible(e.g. remove theaddHeight flag after it's been shown):

scrollTrigger.add('.animateMe',{offset:{element:{y:1.0}},toggle:{callback:{in:(trigger)=>{// remove the element offsettrigger.offset.element.y=0}}}})

Another example for setting custom classes per toggle;

<divdata-scroll="toggle(animateIn, animateOut)"></div>

Becomes

constscrollTrigger=newScrollTrigger()scrollTrigger.add('[data-scroll]',{toggle:{class:{in:'animateIn',out:'animateOut'}}})

If you have any questions on migrating tov1.x feel free tocreate a new issue.

Contributing

Fork, have a look in thesrc/ directory and enjoy! If you have improvements, start a new branch & create a pull request when you're all done :)

Troubleshooting

You can see really quickly if the Trigger is working by hitting 'inspect element'. Here you can see if the visible/invisble class is toggled when you scroll past the element.

If the classes don't toggle, check the JavaScript console. There might be some handy info in there.

Found an issue?

Ooh snap, well, bugs happen. Please create a new issue and mention the OS and browser (including version) that the issue is occurring on. If you are really kind, make aminimal, complete and verifiable example and upload that tocodepen.

Legacy

Looking for the old ScrollTrigger? Check out thelegacy branch!


[8]ページ先頭

©2009-2025 Movatter.jp