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

Simple & light weight (3kb minified & zipped) vanilla javascript plugin to create smooth & beautiful animations when you scrolllll! Harness the power of the most intuitive interaction and make your websites come alive!

License

NotificationsYou must be signed in to change notification settings

ericflatt/lax.js

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple & light weight (<3kb gzipped) vanilla javascript plugin to createsmooth & beautiful animations when you scrolllll! Harness the power of the most intuitive interaction and make your websites come alive!

>>> DEMO <<<

>>> MARIO DEMO <<<


lax 2.0

For those daring enough ;) there is an alpha version of lax.js 2.0 available. Have a play:https://github.com/alexfoxy/lax.js/tree/version-2


Getting Started

NPM Setup

npm install lax.js
importlaxfrom'lax.js'

Basic Browser Setup

  1. Add lax.js to your html
<scriptsrc="lib/lax.min.js"><!-- or via CDN --><scriptsrc="https://cdn.jsdelivr.net/npm/lax.js"></script>
  1. Initialize the plugin
window.onload=function(){lax.setup()// initconstupdateLax=()=>{lax.update(window.scrollY)window.requestAnimationFrame(updateLax)}window.requestAnimationFrame(updateLax)}
  1. Add class and attributes to the HTML tags you want to animate e.g.
<pclass="lax"data-lax-preset="spin fadeInOut">Look at me goooooo!</p>
  1. Scroll and enjoy!

Usage With React, Vue.js, EmberJS & DOM Changes

To increase performance lax.js indexes the list of elements to animate when the page loads. If you're using a library like React, vue.js or EmberJS, it is likely that you are adding elements after the initialwindow.onload. Because of this you will need to calllax.addElement(domElement) when you add components to the DOM that you want to animate.

See below for working examples:

You can also calllax.removeElement(domElement) when the component unmounts.

Presets

The easiest way to get started is to use the presets via thedata-lax-preset attribute. You can chain multiple presets together for e.g.data-lax-preset="blurOut fadeOut spin". Some presets also support an optional strength e.g.data-lax-preset="blurOut-50".

See the list ofSupported Presets for details.

Custom Animations

You can easily create your own effects. Just add an attribute to your HTML tag (seeSupported Attribute Keys) with an array of values. These arrays take the format ofscrollPos val, scrollPos val, ... | option=val e.g:

<pclass="lax"data-lax-opacity="0 1, 100 1, 200 0 | loop=200">I start to fade out after the window scrolls 100pxand then I'm gone by 200px!</p>

By default thescrollPos iswindow.scrollY but you can use an element distance from the top of the screen instead. You can either pass in a selectordata-lax-anchor="#bio" or set it to use itselfdata-lax-anchor="self" (this is the default for all presets) e.g.

<pclass="lax"data-lax-opacity="200 1, 100 1, 0 0"data-lax-anchor="self">I start to fade out after I'm 100px away from the top of the windowand then I'm gone by the time I reach the top!</p>

Special Values

KeyValue
vwwindow.innerWidth
vhwindow.innerHeight
elwtargetElement.clientWidth
elhtargetElement.clientHeight

You can use these instead of integer values for the scrollPos e.g.

<pclass="lax"data-lax-opacity="0 1, vh 0">I fade out as the page scrolls down andI'm gone when the page has scrolled the view port height!</p>

Calculated Values

You can also use vanilla JS within( ) for calculations and access to more variables e.g.

<pclass="lax"data-lax-opacity="0 1, (document.body.scrollHeight*0.5) 0">I fade out as the page scrolls down andI'm gone when the page has scrolled 50%down the entire page height!</p>

Options

You can pass options into your custom animations for more control e.g.

<pclass="lax"data-lax-opacity="0 1, 100 0, 200 100 | loop=200 offset=100 speed=2">I start at 0 opacity andfade in and out every 50px</p>
OptionEffect
loopmodulus the input scrollY position so the animation will loop everyloop pixels
offsetaddoffset to scrollY position so the animation will begin at this point
speedmultiplies the input scrollY position byspeed to change the speed of the animation

Responsive Design

You can set multiple presets and animations for different screen widths. When setting up lax you need to pass in your screen width breakpoints e.g.

lax.setup({breakpoints:{small:0,large:992}})

Then you can define presets or transforms per breakpoint.

<pclass="lax"data-lax-preset_small="spin">I only spin when the screen is smaller than 992px.</p><pclass="lax"data-lax-scale_small="0 1, 500 0"data-lax-scale_large="0 1, 500 2">I shrink when the screen is smaller than 992px but grow when the screen is larger 992px.</p>

Supported Presets

PresetDefault Strength
lingern/a
lazy100
eager100
lazy100
slalom50
crazyn/a
spin360
spinRev360
spinIn360
spinOut360
blurInOut40
blurIn40
blurOut40
fadeInOutn/a
fadeInn/a
fadeOutn/a
driftLeft100
driftRight100
leftToRight1
rightToLeft1
zoomInOut0.2
zoomIn0.2
zoomOut0.2
swing30
speedy30

Supported Attribute Keys

Transforms

TransformKey
opacitydata-lax-opacity
translatedata-lax-translate
translateXdata-lax-translate-x
translateYdata-lax-translate-y
scaledata-lax-scale
scaleXdata-lax-scale-x
scaleYdata-lax-scale-y
skewdata-lax-skew
skewXdata-lax-skew-x
skewYdata-lax-skew-y
rotatedata-lax-rotate
rotateXdata-lax-rotate-x
rotateYdata-lax-rotate-y

Filters (note - these may be unperformant on low powered machines)

FilterKey
brightnessdata-lax-brightness
contrastdata-lax-contrast
hue-rotatedata-lax-hue-rotate
blurdata-lax-blur
invertdata-lax-invert
saturatedata-lax-saturate
grayscaledata-lax-grayscale

Other

FilterKey
background positiondata-lax-bg-pos
background position-xdata-lax-bg-pos-x
background position-ydata-lax-bg-pos-y

Sprite Sheet Animations

You can create animations using sprite sheets. See a demohere.

Thedata-lax-sprite-data is required and formated like so[frameWidth, frameHeight, frameCount, columnCount, scrollStep]. You can either set the image using CSS or thedata-lax-sprite-image attribute. e.g.

<divclass="lax"data-lax-sprite-data="500,500,36,36,10"data-lax-sprite-image="./spritesheet.png"/>

You can turn a gif or a video into a sprite sheet with this tool:https://ezgif.com/gif-to-sprite

Note: current implimentation requires the element to be the same size as the frame width & height.

Custom Presets

To avoid duplicate code you can define your own presets with a list of attributes e.g.

lax.addPreset("myCoolPreset",function(){return{"data-lax-opacity":"(-vh*0.8) 40, (-vh*0.6) 0","data-lax-rotate":"(-vh*2) 1000, (-vh*0.5) 0"}})

You can then access this preset like this:

<pclass="lax"data-lax-preset="myCoolPreset">I'm the coolest preset in the world 😎</p>

Performance Tips

  • Avoid nesting lax enabled elements within each other, you'll get better performance using lax with smaller elements in the dom tree.
  • Avoid transforms on large elements, e.g. full screen backgrounds.
  • By default elements that have opacity 0 aren't updated. You can either manually set up adata-lax-opacity to control this yourself or usedata-lax-optimize which will set the elements opacity to 0 when it goes off -screen.
  • By default-webkit-backface-visibility: hidden; is added to your elements style to encourage the browser to render that object as a layer on the GPU and increase performance. To turn this off adddata-lax-use-gpu="false" to your element.

Notes

Screen Rotating & Resizing

As some values (vh, vw, elh, elw) are calculated on load, when the screen size changes or rotates you might want to recalculate these. E.g.

window.addEventListener("resize", function() {lax.updateElements()});

Be warned, on mobile, a resize event is fired when you scroll and the toolbar is hidden so you might want to check if the width or orientation has changed.

Scroll Wheels

Scroll wheels only increment the scroll position in steps which can cause the animations to look janky.

Merging Existing Styles

Only inline styles for transforms and filters will be merged in to the animation. Transforms and filters derived from CSS will be overwritten.

To Do / Ideas

  • Implement a tween for scroll wheels to remove reliance on smoothscroll
  • A way to add weight/momentum to moving objecs
  • More cool demos
  • More concise read me for react / vue.js
  • Support for sprite sheet animations

About

Simple & light weight (3kb minified & zipped) vanilla javascript plugin to create smooth & beautiful animations when you scrolllll! Harness the power of the most intuitive interaction and make your websites come alive!

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript82.9%
  • HTML17.1%

[8]ページ先頭

©2009-2025 Movatter.jp