admin管理员组文章数量:1325236
I have a div which takes the shape of a circle the css property to display a circle is taken from the circle
class. The color of the circular div is taken from the inline styling. Here a function called Status()
is used where it will return a hex color code. The circle renders with the colors according to the status we pass to the Status function. To achieve the hover effect i added a ':hover' property to the styling object but it doesn't work. Here is the code that i have tried. Any idea on how to achieve this?. i need to add a boarder/glow to the circle on mouse hover.
<div
className="circle"
style={{
backgroundColor: Status('new'),
':hover': {
boxShadow: `0px 0px 4px 2px ${Status('plience')}`,
},
}}
/>
I have a div which takes the shape of a circle the css property to display a circle is taken from the circle
class. The color of the circular div is taken from the inline styling. Here a function called Status()
is used where it will return a hex color code. The circle renders with the colors according to the status we pass to the Status function. To achieve the hover effect i added a ':hover' property to the styling object but it doesn't work. Here is the code that i have tried. Any idea on how to achieve this?. i need to add a boarder/glow to the circle on mouse hover.
<div
className="circle"
style={{
backgroundColor: Status('new'),
':hover': {
boxShadow: `0px 0px 4px 2px ${Status('plience')}`,
},
}}
/>
Share
Improve this question
asked Jun 18, 2019 at 9:59
TRomeshTRomesh
4,4818 gold badges49 silver badges78 bronze badges
1
- You can't - stackoverflow./questions/5293280/… – Paulie_D Commented Jun 18, 2019 at 10:16
1 Answer
Reset to default 6Try adding &
before :hover
This is not possible with inline styles, you may want to use onMouseEnter
and onMouseLeave
props to get the hover state and use it, for example :
class MyComponent extends React.Component {
state= {
hover: false,
}
handleMouseEnter = () => {
this.setState({ hover: true });
}
handleMouseLeave = () => {
this.setState({ hover: false });
}
render() {
const { hover } = this.state;
return(
<div
className="circle"
style={{
backgroundColor: Status('new'),
...(hover && { boxShadow: `0px 0px 4px 2px ${Status('plience')}`}),
}}
onMouseEnter={this.handleMouseEnter} // Or onMouseOver
onMouseLeave={this.handleMouseLeave}
/>
)
}
}
Alternatives :
- Use a third party styling library (e.g. Styled-ponents)
- Use classnames / css stylesheets
本文标签: javascriptAdd hover effect to react div using inline stylingStack Overflow
版权声明:本文标题:javascript - Add hover effect to react div using inline styling - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742166459a2425915.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论