GithubHelp home page GithubHelp logo

chouzejkl / laxxx Goto Github PK

View Code? Open in Web Editor NEW

This project forked from alexfoxy/lax.js

1.0 1.0 0.0 123 KB

Simple & light weight (2kb 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: MIT License

JavaScript 100.00%

laxxx's Introduction

lax.js

Simple & light weight (2kb 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!

>>> DEMO <<<

Getting started

NPM setup

npm install lax.js
import lax from 'lax.js'

Basic browser setup

  1. Add lax.js to your html
<script src="lib/lax.min.js" >
  1. Initialize the plugin
window.onload = function() {
	lax.setup({ /* opts */ }) // init
	  
	document.addEventListener('scroll', function(e) {
	  lax.update(window.scrollY) // update every scroll
	}, false)

	lax.update(window.scrollY) // set initial positions
}
  1. Add attributes to the HTML tags you want to animate e.g.
<p data-lax-preset="spin fadeInOut">Look at me goooooo!</p>
  1. Scroll and enjoy!

Dealing with DOM changes

lax builds a list of all elements it needs to control when the page loads so if they are added to the DOM subsequently they won't be updated on page scroll. If you're using a library like React or vue.js, it is likely that not all elements are in the dom on page load. Because of this you will need to call lax.populateElements() when you add elements to the DOM that you want to animate.

For example componentDidMount() // React or created() // vue.js.

Presets

The easiest way to get started is to use the presets via the data-lax-preset attribute. You can chain multiple presetes 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 of Supported Presets for details.

Custom Animations

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

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

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

<p 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 window
	and then I'm gone by the time I reach the top!
</p>

There are also some shortcuts for useful values:

Key Value
vw window.innerWidth
vh window.innerHeight
elw targetElement.clientHeight
elh targetElement.clientWidth

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

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

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

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

Supported Presets

Preset Default Strength
linger n/a
lazy 100
eager 100
lazy 100
slalom 50
crazy n/a
spin 360
spinRev 360
spinIn 360
spinOut 360
blurInOut 40
blurIn 40
blurOut 40
fadeInOut n/a
fadeIn n/a
fadeOut n/a
driftLeft 100
driftRight 100
leftToRight 1
rightToLeft 1
zoomInOut 0.2
zoomIn 0.2
zoomOut 0.2

Supported Attribute Keys

Transforms

Transform Key
opacity data-lax-opacity
translate data-lax-translate
translateX data-lax-translate-x
translateY data-lax-translate-y
scale data-lax-scale
scaleX data-lax-scale-x
scaleY data-lax-scale-y
skew data-lax-skew
skewX data-lax-skew-x
skewY data-lax-skew-y
rotate data-lax-rotate

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

Filter Key
brightness data-lax-brightness
contrast data-lax-contrast
hue-rotate data-lax-hue-rotate
blur data-lax-blur
invert data-lax-invert
saturate data-lax-saturate
grayscale data-lax-grayscale

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:

<p data-lax-preset="myCoolPreset">
	I'm the coolest preset in the world ๐Ÿ˜Ž
</p>

Notes

Performance

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 add lax-optimize="false" to your element.

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.populateElements()
});

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 icrement the scroll position in steps which can cause the animations to look janky. You can use the SmoothScroll (http://www.smoothscroll.net/) plugin to smooth this out, however there maybe performance implications that need investigating.

To Do / Ideas

  • Re-calculate values on rotate / change window size
  • Add debug mode
  • Elastic bouncing values
  • Optimise elements that go off screen
  • Implement a tween for scroll wheels to remove dependency on smoothscroll
  • Better error reporting
  • Add "momentum" as option for anchor & presets
  • Move presets to lax-presets.js to reduce base library size

laxxx's People

Contributors

0xflotus avatar alexfoxy avatar antonreshetov avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.