Creating a tilt effect in React JS typically involves applying styles dynamically based on user interaction, such as mouse movements. This tilt effect can enhance the UI of our web application so that it seems more realistic and better. We will do this using the Tilt Component from the "react-parallax-tilt" package/module.
Approach
We will need to use the Tilt Component from the 'react-parallax-tilt' package to show tilt effect for mouse hover on the card element. We can also modify the tilt behavior of our Tilt Component using its props. Below is a complete reference of the most generally used props to modify the default behavior of the Tilt Component as per our needs:
- tiltEnable: boolean (Default value: true). To enable/disable the tilt effect.
- tiltReverse: boolean (Default value: false). To reverse the tilt direction.
- tiltMaxAngleX: number (Default value: 20), Range: 0 - 90. To specify the max tilt rotation (degrees) on the x-axis.
- tiltMaxAngleY: number (Default value: 20), Range: 0 - 90. To specify the max tilt rotation (degrees) on the y-axis.
- glareEnable: boolean (Default value: false). To enable/disable the glare effect.
- glareMaxOpacity: number (Default value: 0.7), Range: 0 - 1. To determine the maximum glare opacity (0.5 = 50%, 1 = 100%, etc.).
- glareColor: string (Default value: #ffffff). To set the color of the glare effect.
- glareReverse: boolean (Default value: false). To reverse the glare direction.
- scale: number (Default value: 1). To determine the scale of the component (1.5 = 150%, 2 = 200%, etc.).
- perspective: number (Default value: 1000). It defines how far the object (wrapped/child component) is away from the user. The lower the more extreme the tilt gets.
- gyroscope: boolean (Default value: false). To enable/disable device orientation detection.
Creating React Application
Step 1: First of all, let's create a simple react application using 'create-react-app'. Open your command prompt or you can use a code editor like VSCode and use its command prompt and type the following command:
npx create-react-app foldername
Step 2: Move to the project directory
cd foldername
Step 3: Now install the 'react-parallax-tilt' package using npm.
npm i react-parallax-tilt
Project Structure
Your project should look like this
Folder StructureDependencies list after installing packages
{
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-parallax-tilt": "^1.7.171",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
}
Example: Shows a card with tilt effect using react-parallax-tilt library in react js.
CSS
/* Filename - App.css */
#background {
width: 100%;
height: 100%;
background-color: rgb(19, 19, 59);
position: absolute;
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
}
CSS
/* TiltComponent.css */
.tiltComponent {
height: 400px;
width: 700px;
background-color: blue;
}
JavaScript
// Filename - App.js
import "./App.css";
import TiltComponent from "./TiltComponent";
function App() {
return (
<div id="background">
<TiltComponent></TiltComponent>
</div>
);
}
export default App;
JavaScript
// Filename - TiltComponent.js
import React from 'react'
import Tilt from "react-parallax-tilt";
import "./TiltComponent.css";
const TiltComponent = () => {
return (
<Tilt glareEnable={true} tiltMaxAngleX={10}
tiltMaxAngleY={10} perspective={1000}
glareColor={"rgb(255,0,0)"}>
<div className='tiltComponent'>
{/* Your Code Here */}
</div>
</Tilt>
)
}
export default TiltComponent;
Steps to run the application: Write the below command in the terminal to run the application:
npm start
Output: Head towards your browser to see the output. If it's not shown, go to 'http://localhost:3000/' to see your output.
For video reference : Gif file
Similar Reads
Glassmorphism Card Hover Effect Glassmorphism is a modern way of styling web-elements of any web-page and providing a 3D as well as a glass effect. This animation effect can be easily generated by using HTML, CSS, and Vanilla-tilt JS. We can implement Glassmorphism using various CSS properties. It is used to add a glass effect to
3 min read
Animated modal using react, framer-motion & styled-components In this article, we are going to learn how to create an animated model using react, framer-motion & styled-components.PrerequisitesJavaScript (ES6).HTMLCSSReactJS. React useStateSteps to Create React Application And Installing Module:Step 1: Now, you will start a new project using create-react-a
4 min read
How to rotate a particular string onScroll in ReactJS ? In, this article, we will see how we can rotate a text while Scrolling to give a nice user experience by using an npm package react-scroll-rotate.Prerequisites:NodeJS or NPMReact JSSyntax:<ScrollRotate> </ScrollRotate>We wrap the text within these tags which we want to show effects.Steps
2 min read
Design a Flip Card Effect using ReactJS Design a Flip Card Effect using ReactJS refers to adding flip animations to the react component. The flip card effect is where a card flips to reveal its backside when clicked or hovered over.PrerequisitesReact JSNode.js & NPMTable of ContentApproach 1: Using CSS stylingApproach 2: Using react-c
4 min read
React Suite Icon Flip and rotate A React suite is a library of React components, sensible UI design, and a friendly development experience. It is supported in all major browsers. It provides pre-built components of React which can be used easily in any web application. In this article, we'll learn about React suite Icon Flip and Ro
3 min read
Animating With react-motion In web applications to make the website or the web page more attractive and to enhance the user experience animation is used. These animations can be performed with the help of react-motion, which is the popular library of react applications.What is React-Motion?React-Motion is a popular animation l
4 min read