我如何仅使用NextJS激活样式属性以悬停组件?
我希望能够悬停一些组件并具有特殊的CSS特征。因此,例如,如果我徘徊在项目A上,我只希望将项目A与其他项目区分开。
现在,我有这种方法更改元素背景图像,这是我的代码:
const StatsIndicadores = () => {
const somearray = ['10%', '50%', '60%', '60%']
const [isHovering, setIsHovering] = useState(false)
const [hoverStyle, setHoverStyle] = useState({})
const [index, setIndex] = useState(0)
const mouseEntered = (index) => {
setIsHovering(true);
setIndex(index + 1);
}
const mouseLeft = () => setIsHovering(false)
useEffect(() => {
if (isHovering) {
setHoverStyle({
background: `center url("/images/stats/rounded-images/${index}.png") no-repeat`,
border: '1px solid #f5f5f5',
boxShadow: '0px 0px 10px #f5f5f5',
transition: 'all 0.3s ease-in-out'
});
} else {
setHoverStyle({});
}
}, [isHovering]);
return (
<>
{<Grid container className={style.test}>
{
somearray.map((item, index) => (
<Grid item xs={12} md={3} key={index}>
<div className={style.statsContainer}>
<div className={style.circle} style={hoverStyle} onMouseEnter={(e) => { mouseEntered(index) }} onMouseLeave={mouseLeft}>
<p className={style.circleStats}>Some value</p>
<p className={`${style.circleStats} ${style.circleStatsDescription}`}>Ver más</p>
</div>
<p className={style.indicador}>Some name</p>
</div>
</Grid>
))
}
</Grid>}
</>
)
}
我不想在其他圆圈上显示背景图像,但我不知道如何仅在悬停的一个圆圈上显示它。 我有想法比较盘旋项目的索引,如果该项目活跃,请显示背景...但是我不知道这样做是最好的。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
截至目前,当其中一个盘旋时,您将这些样式应用于所有圆圈。您需要根据活动盘旋圆圈有条件地应用样式(您可以使用任何独特的属性 - 在下面的示例中,我使用了ID)。
演示代码:
As of now, you're applying the styles to all circles when one of them is hovered. You need to conditionally apply the styles based upon the active hovered circle (you can use any property that's unique -- in the example below, I used an id).
Demo Code:
Working Demo
Code:
styles: