gpt4 book ai didi

javascript - React中如何淡出和淡入?

转载 作者:行者123 更新时间:2023-12-03 08:28:57 26 4
gpt4 key购买 nike

所以,我完成了 FCC 的元素“随机报价机”,我已经完成了所有基本要求,例如更改报价、颜色和推文报价。但有一件事我不能做,那就是引号的淡入和淡出动画,例如像这样:https://codepen.io/freeCodeCamp/pen/qRZeGZ

在上面的链接中,动画是由 jQuery 完成的。但我已经在 ReactJS 上完成了我的元素

如果你想 fork ,这是我的代码和框:https://codesandbox.io/s/amazing-feistel-b2u6j

代码如下:

import React from "react";
import "./styles.css";

import { useState, useCallback } from "react";
import {
ChakraProvider,
Box,
Text,
Button,
Icon,
Flex,
HStack,
Link
} from "@chakra-ui/react";

import { FaTwitter, FaQuoteLeft } from "react-icons/fa";
import quoteArray from "../public/quotes";
import { defaultColor, getRandomColor } from "../public/color";

const QuoteBox = () => {
const [loading, setLoading] = useState(true);
const [quote, setQuote] = useState(null);
const [color, setColor] = useState(defaultColor);

const onQuoteChange = useCallback(async () => {
const nextColor = getRandomColor();
setLoading(true);
const randomQuote =
quoteArray[Math.floor(Math.random() * quoteArray.length)];
setTimeout(() => {
setLoading(false);
setColor(nextColor);
setQuote(randomQuote);
}, 500);
}, []);

React.useEffect(() => {
onQuoteChange();
}, [onQuoteChange]);

var randomTweet = (quote) => {
if (!quote) {
return null;
}

const link = `https://twitter.com/intent/tweet?text="${quote.quote}"%20-${quote.author}`;
return link;
};

return (
<Box
bg={color}
h="100%"
display="flex"
flexDir="column"
justifyContent="center"
>
<Box
width="60%"
border="1px"
boxShadow="md"
p={5}
rounded="md"
bg="white"
borderColor="gray.400"
mx="auto"
>
<Box>
<Flex>
<Box>
<Icon as={FaQuoteLeft} w={7} h={6} color={color} />
<Text fontSize="2xl" color={color} pl={8} mt="-20px">
{loading || !quote ? "..." : quote.quote}
</Text>
</Box>
</Flex>
</Box>
<Box>
<Text fontSize="xl" align="right" color={color}>
-{loading || !quote ? "..." : quote.author}
</Text>
</Box>

<HStack mt="2%" ml="1%" spacing="2%">
<Button color={color} size="sm" onClick={onQuoteChange}>
New Quote
</Button>

<Button
as={Link}
color={color}
size="sm"
leftIcon={<FaTwitter />}
target="_blank"
href={randomTweet(quote)}
>
Tweet now!
</Button>
</HStack>
</Box>
</Box>
);
};

function App() {
return (
<ChakraProvider>
<QuoteBox />
</ChakraProvider>
);
}

export default App;

如何制作动画?

最佳答案

<强> Working demo

为根元素(box)添加transition="0.8s Linear"属性,使颜色过渡更加平滑。

  <Box
bg={color}
h="100%"
display="flex"
flexDir="column"
justifyContent="center"
transition="0.8s linear">
</Box>

根据不透明度状态添加显示和隐藏类名 className={ opacity ? “显示”:“隐藏”}

  <Box 
className={ opacity ? "show" : "hide"}
>
<Flex>
<Box>
<Icon as={FaQuoteLeft} w={7} h={6} color={color} />
<Text fontSize="2xl" color={color} pl={8} mt="-20px">
-{quote.quote}
</Text>
</Box>
</Flex>
</Box>
<Box>
<Text className={ opacity ? "show" : "hide"} fontSize="xl" align="right" color={color}>
-{quote.author}
</Text>
</Box>

添加下面给出的样式以根据不透明度状态应用动画:

.hide {
-webkit-animation: fadeinout .3s linear forwards;
animation: fadeinout .3s linear forwards;
}

@-webkit-keyframes fadeinout {
0% { opacity: 0.6; }
50% { opacity: 0.2; }
100% { opacity: 0; }
}

@keyframes fadeinout {
0% { opacity: 0.6; }
50% { opacity: 0.2; }
100% { opacity: 0; }
}

.show {
-webkit-animation: display .5s linear forwards;
animation: display .5s linear forwards;
}


@-webkit-keyframes display {
0% { opacity: 0.2; }
50% { opacity: 0.6; }
100% { opacity: 1; }
}

@keyframes display {
0% { opacity: 0.2; }
50% { opacity: 0.6; }
100% { opacity: 1; }
}

您还可以根据您的要求调整过渡时间。

关于javascript - React中如何淡出和淡入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65731647/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com