简体   繁体   English

如何通过其父级触发子级 object 上的 hover 事件?

[英]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.

相关问题 如何在jQuery中触发父事件而不触发子事件 - How to trigger an event on a parent but not on its child in jQuery 如何防止父 object 激活子对象的事件 - How to prevent parent object from activating child object's event jQuery触发事件首先附加在子项上,然后附加在父项的事件上 - jQuery trigger event attached on children first then its parent's event 如何使父级事件在单击子元素时不会触发,但不会阻止子元素事件 - How to get a parent's event to not trigger when clicking on child elements, but not prevent child element events 触发子元素的onclick事件,但不是父元素 - Trigger child element's onclick event, but not parent's 如何通过从父级到其子级的 id(数值)查找和显示对象 - How can I find and display a object by its id(number value) from parent a parent to it's child 使用jQuery,如何在父对象的悬停状态下定位子元素? - Using jQuery, how do I target a child element in a parent object's hover state? 如何通过父母的onmouseover事件传递孩子? - How to pass child with parent's onmouseover event? 在jQuery中拖动孩子时如何触发父拖动事件? - How to trigger parent drag event when child is dragged in jQuery? 如何仅在子 div 元素中而不在父 div 中触发 onclick 事件? - How to trigger onclick event only in the child div element but not in the parent div?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM