Provide a nice and declarative way to create parallax effect depending on the scroll and/or the mouse
- Demo
- Install
- Get Started
- Displacements
- Layers
- Appliers
- Javascript API
- Sugar Web Components Documentation
- Browsers support
- Code linting
- Contribute
- Who are Coffeekraken?
- Licence
npm install coffeekraken-s-parallax-component --save
First, import the component into your javascript file like so:
import SParallaxComponent from 'coffeekraken-s-parallax-component'
Then simply use it inside your html like so:
<s-parallax scroll-y="300">
<!-- your coontent here... -->
</s-parallax>
This component works around a concept of displacement. The displacement is the amount of movement the component can have depending on the scroll and/or the mouse movement
A displacement is calculated based on the center of the screen and the scroll/mouse movement amount.
To specify a possible displacement amount, use the scroll
and mouse
component property. Here's how:
<!-- this mean a possible displacement based on the mouse -->
<s-parallax mouse-x="200" mouse-y="400"></s-parallax>
<!-- this mean a possible displacement based on the scroll -->
<s-parallax scroll-y="200"></s-parallax>
To simplify the use of displacements, etc... a layer system has been built-in. A layer is simply some properties attached to a layer name that you can apply simply in your html like so:
First, register a layer
import SParallaxComponent from 'coffeekraken-s-parallax-component'
SParallaxComponent.registerLayer('my-cool-layer', {
mouseX: 200,
mouseY: 400
})
Then apply your layer to any s-parallax
component in your html like so:
<s-parallax layer="my-cool-layer">
<!-- your html content here... -->
</s-parallax>
By default, this component support two ways of applying the displacement:
-
transform
: Apply the displacement as a transform css property -
background
: Apply the displacement as a background-position css property
This list can be extended by register new appliers like so:
import SParallaxComponent from 'coffeekraken-s-parallax-component'
SParallaxComponent.registerApplier('rotate', (component, displacements) => {
component.style.transform = `rotateX(${displacements.x}) rotateY(${displacements.y})`
})
You can then tell your s-parallax
component to use this applier like so:
<s-parallax applier="rotate" layer="my-cool-layer">
<!-- your html here... -->
</s-parallax>
IE / Edge | Firefox | Chrome | Safari |
---|---|---|---|
IE11+ | last 2 versions | last 2 versions | last 2 versions |
As browsers are automatically updated, we will keep as reference the last two versions of each but this component can work on older ones as well.
The webcomponent API (custom elements, shadowDOM, etc...) is not supported in some older browsers like IE10, etc... In order to make them work, you will need to integrate the corresponding polyfill.
This package uses some code linting rules. Here's the list:
- ESLint with airbnb and prettier rules for javascript files
-
Stylelint with stylelint-config-standard for
scss
files
Your commits will not been accepted if the code style is not respected!
This is an open source project and will ever be! You are more that welcomed to contribute to his development and make it more awesome every day. To do so, you have several possibilities:
We try to be some cool guys that build some cool tools to make our (and yours hopefully) every day life better.
The code is available under the MIT license. This mean that you can use, modify, or do whatever you want with it. This mean also that it is shipped to you for free, so don't be a hater and if you find some issues, etc... feel free to contribute instead of sharing your frustrations on social networks like an asshole...