[英]How to trigger hover event on child's object via its parent?
I have written a custom react component which has an animation on:hover.我编写了一个自定义反应组件,它在:hover 上有一个 animation。 This component is a child of a different parent component.
该组件是不同父组件的子组件。 What I would like to do is to trigger child's :hover when hovering over parent's component.
我想做的是在将鼠标悬停在父组件上时触发孩子的:hover 。 Is it possible to simulate it via css (if it isn't then how can it be achieved)?
是否可以通过 css 模拟它(如果不是,那么如何实现)?
Test Case: I have a blog post card that when hovering on lightens up.测试用例:我有一张博客明信片,当鼠标悬停在上面时会变亮。 Within it, in form of a child element I have a Link/a component which has its own css/:hover animation and also upon clicking redirects to a given href page.
在其中,以子元素的形式,我有一个 Link/a 组件,它有自己的 css/:hover animation 并且在单击时重定向到给定的 href 页面。 The effect that I would like to achieve is to animate both elements (and not only parent element) when hovering on parent as well as triggering child's click event when clicking on the parent.
我想要实现的效果是在悬停在父元素上时为两个元素(而不仅仅是父元素)设置动画,并在单击父元素时触发子元素的单击事件。
If you can share the code would be easier to explain but what you can do is in your parent component main wrapper/div you can set onMouseEnter
event and inside this event you can set a boolean state.如果您可以共享代码会更容易解释,但您可以在父组件主包装器/div 中设置
onMouseEnter
事件,并在此事件中设置 boolean state。 Then in your main wrapper/div in childs component you can get className as props and you can set conditional classNames controlled by this state (true or false) and do necessary modifications in your css.然后在子组件的主包装器/div 中,您可以获得 className 作为道具,您可以设置由此 state(真或假)控制的条件类名,并在 css 中进行必要的修改。
An example for conditional className条件类名的示例
className={`${isOnHover ? "hoverClass" : "noHoverClass"}`}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.