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
How to Create Typewriter Effect in ReactJS?
Learn how to enhance your React JS applications with a captivating typewriter effect using CSS and JavaScript, leveraging the typewriter-effect package for seamless and engaging UI design.ApproachTo create a typewriter effect in React, we will be using the typewriter-effect npm package. Import and u
5 min read
How to create Tabs in ReactJS ?
Tabs make it easy to explore and switch between different views. Material UI for React has this component available for us and it is very easy to integrate. We can use Tabs Component in ReactJS using the following approach.Prerequisites:NPM & Node.jsReact JSMaterial UIwe have these approaches to
4 min read
React MUI Right-to-left
Material-UI is a user interface framework that provides pre-defined and customizable React components for faster and easy web development. The Material-UI components are based on top of Material Design by Google. In this article letâs discuss the Right-to-left utility in the Material-UI library. The
3 min read
How To Create a Website in ReactJS?
ReactJS is one of the most popular JavaScript libraries for building user interfaces. It allows you to create dynamic, reusable UI components and efficiently manage state and events. In this article, we'll walk through the steps to create a basic website using ReactJS.PrerequisitesNPM & Node.jsR
5 min read
React Suite Affix Container
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 see about react suite affix top. Affix i
3 min read
How to use styles in ReactJS ?
React is a popular JavaScript library for building single-page applications (SPAs) with dynamic user interfaces. Styling in React can be done in various ways, each with its advantages. In this article, we will explore different approaches to applying styles in React, including inline styles, CSS fil
4 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
Temperature Converter in ReactJS
In this project article, we will be creating a Temperature Convert Project application using the ReactJS library. In this application, we have used the Functional components and various hooks to manage the overall state of the application. Also, we have used different icons to increase the attractiv
6 min read
React Suite Grid Offset
React Suite is a front-end library designed for the middle platform and back-end products. React Suite Grid component provides a grid layout that provides 24 grids. It helps in responsiveness. For offset, we pass the following props to the Col Component: xxlOffset: It takes a number as a value. It m
4 min read
ReactJS UI Ant Design Affix Component
Ant Design Library has this component pre-built, and it is very easy to integrate as well. Affix Component to used to wrap Affix around another component in order to make it stick to the viewport. We can use the following approach in ReactJS to use the Ant Design Affix Component. Syntax: <Affix
2 min read