【发布时间】:2020-03-19 09:53:48
【问题描述】:
我正试图在我的 ReactJS 网站上做这样的事情:
我尝试自己制作并获得了工作结果,但在我看来,实现非常复杂和繁琐。下面我放置了我的实现代码。
JS:
import React from "react"
import switchStyles from "./switch.module.scss"
class LinkSwitch extends React.Component {
constructor() {
super();
this.state = {
isHover: false,
email: false,
phone: false
};
}
handleHoverEmail = () => this.setState({ isHover: !this.state.isHover, email: !this.state.email })
handleHoverPhone = () => this.setState({ isHover: !this.state.isHover, phone: !this.state.phone })
render() {
return (
<div
className={`${switchStyles.hoverContainer} ${this.state.email ? switchStyles.activeEmail : ""}${this.state.phone ? switchStyles.activePhone : ""}`}
>
<a
className={switchStyles.ctaButton}
onMouseEnter={this.handleHoverEmail}
>
почта
</a>
<a className={switchStyles.ctaButtonEmail__hover} onMouseLeave={this.handleHoverEmail}>info@adnosov.ru</a>
<a
className={switchStyles.ctaButton}
onMouseEnter={this.handleHoverPhone}
>
телефон
</a>
<a className={switchStyles.ctaButtonPhone__hover} onMouseLeave={this.handleHoverPhone}>+7 992 020 1025</a>
</div>
)
}
}
export default LinkSwitch
SCSS:
.hover-container {
width: fit-content;
}
.hover-container > a:first-child:after {
content: '/';
background-size: contain;
background-repeat: no-repeat;
background-position: center;
display: inline-block;
margin: 0 0;
font-weight: 500;
}
.cta-button-email__hover {
display: block;
opacity: 0;
visibility: hidden;
position: absolute;
white-space: nowrap;
top: 0;
left: 0;
z-index: 1;
}
.cta-button-phone__hover {
display: block;
opacity: 0;
visibility: hidden;
position: absolute;
white-space: nowrap;
top: 0;
left: 0;
z-index: 1;
}
.active-email .cta-button-email__hover {
opacity: 1;
visibility: visible;
}
.active-phone .cta-button-phone__hover {
opacity: 1;
visibility: visible;
}
.active-email .cta-button {
opacity: 0;
}
.active-phone .cta-button {
opacity: 0;
}
我认为,使用两种状态,电话号码和电子邮件的特定类名是错误的代码。我会很高兴得到任何帮助!也许有人可以提供更简单、更优化的解决方案。
【问题讨论】:
标签: html css reactjs sass hover