RJo*_*rns 2 css keyframe css-animations reactjs styled-components
我是 React 新手,特别尝试使用样式组件和关键帧。
我正在尝试让主页标题 h1 滑入。
我遵循了一些文档,但我觉得缺少一些东西或者我的东西不正常。
这是代码:
//Home.js
import React from "react";
import styled, { keyframes } from 'styled-components';
const Heading = keyframes`
0% { top: -3.125em; }
100% { top: 3em;
`;
const home = styled.div`
min-height: 95vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: 1.5em;
color: white;
`;
const homeHeader = styled.div`
h1 {
font-weight: lighter;
animation: Heading;
animation-duration: 3s;
animation-fill-mode: forwards;
}
// Animation
animation: ${Heading}
animation-duration: 3s;
animation-fill-mode: forwards;
`;
export const Home = () => (
<div className="home">
<header className="homeHeader">
<h1>Welcome to Freight Mule</h1>
</header>
</div>
);
export default Home;
Run Code Online (Sandbox Code Playgroud)
任何帮助理解如何让关键帧和动画工作的帮助都会非常有帮助。
有几件事是错误的:
styled-component。当你这样做时const Div = styled.div`
background-color: blue;
`
Run Code Online (Sandbox Code Playgroud)
您刚刚创建了一个新的 React 组件,可以在任何渲染方法中使用它。所以你的Home组件变成了我大写的(react期望自定义组件大写)并重命名了一些组件以避免重复的变量):
const Home = () => (
<HomeDiv>
<HomeHeader>
<h1>Welcome to Freight Mule</h1>
</HomeHeader>
</HomeDiv>
);
Run Code Online (Sandbox Code Playgroud)
top,您需要将初始top信息添加到标题中。此外,我认为你不想应用动画,h1所以我删除了它const HomeHeader = styled.div`
h1 {
font-weight: lighter;
}
position: relative;
top: 0;
animation: ${Heading};
animation-duration: 3s;
animation-fill-mode: forwards;
`;
Run Code Online (Sandbox Code Playgroud)
justify-content:center;从HomeDivcss 声明中删除 。否则,您将看到一个从 div 中心到 3em 的动画。这里是完整的代码:
const Heading = keyframes`
0% { top: -3.125em; }
100% { top: 3em;}
`;
const HomeDiv = styled.div`
min-height: 95vh;
display: flex;
flex-direction: column;
align-items: center;
font-size: 1.5em;
`;
const HomeHeader = styled.div`
h1 {
font-weight: lighter;
}
position: relative;
top: 0;
animation: ${Heading};
animation-duration: 3s;
animation-fill-mode: forwards;
`;
const Home = () => (
<HomeDiv>
<HomeHeader>
<h1>Welcome to Freight Mule</h1>
</HomeHeader>
</HomeDiv>
);
Run Code Online (Sandbox Code Playgroud)
这是一个活生生的例子
| 归档时间: |
|
| 查看次数: |
10363 次 |
| 最近记录: |