如何使用 React 和 framer-motion 创建滚动模具?
我们可以使用带有纯 CSS 和 framer-motion 库的 react 来创建一个骰子,以便使用以下方法对其进行动画处理:
先决条件:
- JavaScript(ES6)知识
- 熟悉 HTML/CSS。
- ReactJS 的基础知识。
创建 React 应用程序并安装模块:
第 1 步:使用以下命令创建一个 React 应用程序:
npx create-react-app rolling-die
第 2 步:创建项目文件夹(即 rolling-die )后,使用以下命令移动到该文件夹:
cd rolling-die
第 3 步:创建 ReactJS 应用程序后,使用以下命令安装framer-motion模块:
$ npm install framer-motion
App.js:现在在App.js文件中写下以下代码。在这里,App 是我们编写代码的默认组件。
例子:
App.js
import React, { useState, useEffect } from "react";
import { motion } from "framer-motion";
import "./App.css";
// Animation properties for the container
// which is the face of the die
const container = {
hidden: { opacity: 1, scale: 0 },
visible: {
scale: [0, 1],
opacity: 1,
transition: {
staggerChildren: 0.5,
delayChildren: 0.5,
},
},
};
// Animation properties for the disc(s) that
// denote(s) the number player gets after rolling the die
const discsOnTheDie = {
hidden: { y: 20, opacity: 0 },
visible: {
y: 0,
opacity: [0.2, 1],
},
};
// Utility function to get the random number
// from 1 t0 6 just like a physical die
const rollTheDie = () => {
return Math.floor(Math.random() * 7 + 1);
};
const App = () => {
// Managing state of randomSize aka number on the die
// using useState hook
const [randomSize, setRandomSize] = useState(rollTheDie());
const discNumbers = new Array(randomSize);
// Assigning 0 to randomSize to the array
for (var i = 0; i < discNumbers.length; i++) {
discNumbers[i] = i;
}
useEffect(() => {
// This will fire on every change of randomSize
setRandomSize(rollTheDie());
}, [randomSize]);
return (
{/* Mapping javascript array discNumbers */}
{discNumbers.map((index) => (
))}
);
};
export default App;
App.css
body {
overflow: hidden;
width: 100vw;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background: linear-gradient(180deg, green, white);
}
.square-container {
display: grid;
width: 200px;
height: 300px;
border-radius: 50px;
padding: 30px;
gap: 20px;
list-style: none;
grid-template-rows: repeat(3, 1fr);
grid-template-columns: repeat(2, 1fr);
background: rgba(255, 255, 255, 0.2);
}
.disc {
background: white;
border-radius: 100%;
justify-content: center;
align-items: center;
}
button {
text-decoration: none;
display: inline-block;
margin-left: 80px;
border: none;
color: white;
padding: 15px 32px;
text-align: center;
font-size: 16px;
font-weight: 900;
border-radius: 50px;
background-color: #4caf50;
}
应用程序.css
body {
overflow: hidden;
width: 100vw;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background: linear-gradient(180deg, green, white);
}
.square-container {
display: grid;
width: 200px;
height: 300px;
border-radius: 50px;
padding: 30px;
gap: 20px;
list-style: none;
grid-template-rows: repeat(3, 1fr);
grid-template-columns: repeat(2, 1fr);
background: rgba(255, 255, 255, 0.2);
}
.disc {
background: white;
border-radius: 100%;
justify-content: center;
align-items: center;
}
button {
text-decoration: none;
display: inline-block;
margin-left: 80px;
border: none;
color: white;
padding: 15px 32px;
text-align: center;
font-size: 16px;
font-weight: 900;
border-radius: 50px;
background-color: #4caf50;
}
运行应用程序的步骤:使用以下命令从项目的根目录运行应用程序。
npm start
输出:现在打开浏览器并转到http://localhost:3000/ ,您将看到以下输出: