我想获取技能组件的位置,并在滚动到达该点时添加动画。但是,它返回错误“TypeError:无法读取属性 'getBoundingClientRect' of null”。我跟随试图跟随这个博客,但我不知道我做错了什么。有人可以帮助我吗?
Card.js
import classes from './Card.module.css';
import React from 'react';
const Card = React.forwardRef((props, ref) => {
return (
<section
className={`${classes.card} ${props.className}`}
id={props.id}
ref={ref}
>
{props.children}
</section>
);
});
export default Card;
技能.js
const Skills = () => {
const ref = React.createRef();
const topPos = ref.current.getBoundingClientRect().top;
const onScroll = () => {
const scrollPos = window.scrollY + window.innerHeight;
if (topPos > scrollPos) {
// enter animation code here
}
};
useLayoutEffect(() => {
window.addEventListener('scroll', onScroll);
return () => window.removeEventListener('scroll', onScroll);
}, []);
return (
<Card className={classes.skills} id='skills' ref={ref}>
<H2>Skills</H2>
<div className={classes['skills-list-container']}>
<div className={classes['skills-list']}>{skillsList}</div>
</div>
</Card>
);
};
export default Skills;