react-tilty

A React port of the vanilla-tilt.js version of Tilt.js
"A tiny requestAnimationFrame powered 60+fps lightweight parallax hover tilt effect for React"
Installation
This package is hosted on npm
npm i react-tilty
How to Use
This component is imported and used like any standard React component
import React, { Component } from "react";
import Tilty from "react-tilty";
class App extends Component {
render() {
return (
<div class="App">
<Tilty></Tilty>
</div>
);
}
}
export default App;
Options
Tilty has a variety of options which can be passed in either as a settings object prop or as individual properties using data-tilt-{propertyname}
Here is a list of available options with their defaults:
reverse: false
max: 35
perspective: 1000
scale: 1
speed: 300
transition: true
axis: null
reset: true
easing: "cubic-bezier(.03,.98,.52,.99)",
glare: false
"max-glare": 1
"glare-prerender": false
gyroscope: true
gyroscopeMinAngleX: -45
gyroscopeMaxAngleX: 45
gyroscopeMinAngleY: -45
gyroscopeMaxAngleY: 45
Example:
<Tilty
data-tilt-reverse="true"
data-tilt-axis="x"
settings = {{
scale: 1.2,
perspective: 900,
reset: false
}}>
</Tilty>
Creating a Parallax Effect
In order to add a parallax effect to the element and it's child, you must add some css properties to them:
- Add
transform-style: preserve-3d
to your tilt element - Add
transform: translateZ(20px)
to your child element (this pixel value can be increased to cause the child element to feel more separated)
<Tilty style={{transformStyle: "preserve-3d"}}>
<div style={{transform: "translateZ(30px)"}}></div>
</Tilty>
Tilt Change Event
You can add an event listener to the component's tiltChange
event in order to access it's x and y tilts, percentages, and overall angle
componentDidMount() {
const tilt = document.querySelector('#tilty');
tilt.addEventListener("tiltChange", e => {
console.log(e.detail);
});
}
License
MIT License