我在Javascript中向下滚动Codepen时修改了这个下划线动画,效果很好.您可以在这里的Codepen中看到它与交叉点观察者一起工作,对于下划线,它使用生成的svg.
Explanation how it works:
这种效果是通过相对于要强调的文本定位SVG,并在其中设置动画来实现的.
单词被包装成在span
个元素中加下划线,以便直接定位单词.
为了使用CSS访问path元素,SVG代码直接位于HTML中.将其添加到要在其中添加下划线的文本所包装的span元素中.
SVG元素有一个类名,因此可以使用CSS将其作为目标->;class="squiggle"
squiggle position
以及width
和height
直接在线设置在SVG上,以使行显示在我想要的位置.
现在下划线显示在正确的位置.通过使用stroke-dasharray
和stroke-dashoffset
属性的组合,可以通过SVG路径的笔划进行动画.
对于开始样式,stroke-dasharray
和stroke-dashoffset
应该足够大,使笔划不可见.
.squiggle path {
stroke-dasharray: 1600;
stroke-dashoffset: 1600;
}
当容器元素在视图中时,我们可以设置动画名称、持续时间、迭代计数和填充模式:
.text-effect.in-view .squiggle path {
animation-name: underline;
animation-duration: 1s;
animation-iteration-count: 1;
animation-fill-mode: forwards;
}
要在元素从视图中滚出时反向显示动画,@keyframes underline-out
会执行相反的动画.
My attempt to rebuild it in React Typescript:
现在,首先,我只try 实现动画React Typescript作为样式化组件,然后在下一步中try 从代码笔添加交点观察者.
我的App.tsx
import Typography from '@mui/material/Typography';
import { Box, keyframes, styled } from '@mui/material';
import React from 'react';
const Underline = keyframes`
{
"from": {
strokeDashoffset: "1600",
},
"to": {
strokeDashoffset: "200",
}
}
`;
const styles = {
textDecoration: {
textDecoration: 'none',
cursor: 'pointer',
color: 'inherit',
},
textEffect: {
strokeDasharray: '1600',
strokeDashoffset: '200',
animationName: 'none',
},
squiggle: {
position: 'absolute',
top: '90%',
left: '-9px',
},
'squiggle path': {
stroke: '#FB9F18',
strokeWidth: '14px',
strokeLinecap: 'round',
strokeDasharray: '1600',
strokeDashoffset: '1600',
animationName: 'underline-out',
animationDuration: '1s',
animationIterationCount: '1',
},
'textEffect.in-view .squiggle path': {
animationName: 'underline',
animationDuration: '1s',
animationIterationCount: '1',
animationFillMode: 'forwards',
},
'@keyframes underline': {
from: {
strokeDashoffset: '1600',
},
to: {
strokeDashoffset: '200',
},
},
'@keyframes underline-out': {
from: {
strokeDashoffset: '200',
},
to: {
strokeDashoffset: '1600',
},
},
};
function UnderlineEffect() {
return (
<Box style={styles.textEffect}>
<Typography>
Double your efficiency,{' '}
<span class="underline">
<a style={styles.textDecoration} href="www.somewebsite.com">
guaranteed.
</a>
<svg
width="215px"
height="25px"
style={styles.squiggle}
viewBox="0 0 466 29"
version="1.1"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xml:space="preserve"
xmlns:serif="http://www.serif.com/"
style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:2;">
<g transform="matrix(1,0,0,1,-4055,-7503)">
<g transform="matrix(1,0,0,1,2942.74,0)">
<g
id="squiggle-underline"
transform="matrix(0.59639,0.0106812,-0.0121867,0.680453,651.002,2140.64)">
<path
d="M976.992,7882.9C1019.57,7882.9 1065.7,7874.34 1108.59,7871.65C1246.32,7863.04 1383.5,7857.74 1521.52,7857.74C1541.4,7857.74 1709,7857.97 1714.47,7868.91C1715.77,7871.51 1710.77,7870.69 1710.29,7870.73C1695.62,7871.88 1681.88,7871.57 1667.06,7871.96C1646.09,7872.51 1625.12,7872.92 1604.15,7873.36C1564.57,7874.19 1525.13,7876.05 1485.69,7879.23C1411.5,7885.22 1337.41,7889.35 1263.06,7892.45C1196.87,7895.22 1130.52,7894 1064.4,7897.12C1021.44,7899.14 978.37,7907.57 935.437,7907.57"
style="fill:none;"
/>
</g>
</g>
</g>
</svg>
</span>
</Typography>
</Box>
);
}
export { UnderlineEffect };