首页 > 其他分享 >styled-components给svg添加动画

styled-components给svg添加动画

时间:2023-02-14 10:34:19浏览次数:47  
标签:const svg transform keyframes components styled animate


​https://stackoverflow.com/questions/68043891/inherited-svg-functional-component-cannot-add-style-styled-components​​ 如下 直接继承react组件 然后添加样式居然无效

import styled, { keyframes } from "styled-components";

const LoadIconComponent = ({ color }: { color?: string }) => (
<svg
viewBox='0 0 1024 1024'
version='1.1'
xmlns='http://www.w3.org/2000/svg'
p-id='2418'
width='1rem'
height='1rem'
>
<path
d='...'
p-id='2419'
fill={color || "#FFFFFF"}
></path>
</svg>
);

const animate = `
0%{transform: rotate(0deg)};
100%{transform: rotate(360deg)}
`;

const keyframe = keyframes`${animate}`;

const LoadIcon = styled(LoadIconComponent)`
animation: ${keyframe} 1s linear;
`;

export default LoadIcon;

这样是可以的 就是有点费事

import styled, { keyframes } from "styled-components";

const animate = `
0%{transform: rotate(0deg)};
100%{transform: rotate(360deg)}
`;

const keyframe = keyframes`${animate}`;

const LoadIcon = styled.svg.attrs({
children: (
<path
d='...'
p-id='2419'
fill='#FFFFFF'
></path>
),
viewBox: "0 0 1024 1024",
version: "1.1",
xmlns: "http://www.w3.org/2000/svg",
"p-id": "2418",
width: "1rem",
height: "1rem",
})`
animation: ${keyframe} 1s linear infinite;
`;

export default LoadIcon;


标签:const,svg,transform,keyframes,components,styled,animate
From: https://blog.51cto.com/u_15964288/6055877

相关文章

  • SVG animateMotion All In One
    SVGanimateMotionAllInOneTheSVG<animateMotion>elementprovidesawaytodefinehowanelementmovesalongamotionpath.demos<svgviewBox="002001......
  • styled-components ts频繁检查出错 keyframes
    正常的书写到了ts后频繁出错代码规范上了ts检查后如图让人烦躁的检查查出一堆不知所以的问题换一种写法通过了检测单显然这不是一个好方法ts-styled-plugin有待加......
  • ASP.NET Hang and OutOfMemory exceptions caused by STA components
    TessFerrandezPrincipalSoftwareEngineeratMicrosoft.Solvingtheproblemsoftheworldwithadebugger,asprinkleofMLandabagofchipsTwitte......
  • SVG滤镜波浪动画效果
    今天给大家分享一个用SVG实现的滤镜波浪动画效果,效果如下:以下是代码实现,欢迎大家复制粘贴和收藏。<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><m......
  • React dashboard UI components library All In One
    ReactdashboardUIcomponentslibraryAllInOne仪表板UI组件库TremorTheReactlibrarytobuilddashboardsfast#beta⚠️$npminstall@tremor/react......
  • vue3引入SvgIcon
    这里使用vite-plugin-svg-icons插件yarnaddvite-plugin-svg-icons-D#ornpmivite-plugin-svg-icons-D#orpnpminstallvite-plugin-svg-icons-D在vite.con......
  • vue3如何改变svg字体大小,以及子组件如何改变父组件背景色
    提问:1、hanzi-write生成的svg只能使用px像素大小,使用rem无效。2、子组件无法修改父组件背景色。svg问题: <template>   <divclass="header"> ......
  • 817. Linked List Components
    packageLeetCode_817/***817.LinkedListComponents*https://leetcode.com/problems/linked-list-components/*Youaregiventheheadofalinkedlistc......
  • SVG foreignObject All In One
    SVGforeignObjectAllInOneSVGforeignObjectThe<foreignObject>SVGelementincludeselementsfromadifferentXMLnamespace.Inthecontextofabrowser......
  • svg格式的loading
    <svgxmlns="http://www.w3.org/2000/svg"width="1em"height="1em"fill="currentColor"viewBox="001616"><pathd="M11.411.6A5500138.2l1.21.2.8-.8-2-......