【问题标题】:Two items are located next to each other when you hover over them appears mail or phone, ReactJS当您将鼠标悬停在它们上时,两个项目彼此相邻出现邮件或电话,ReactJS
【发布时间】: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}
        >
          почта&nbsp;
        </a>
        <a className={switchStyles.ctaButtonEmail__hover} onMouseLeave={this.handleHoverEmail}>info@adnosov.ru</a>
        <a 
          className={switchStyles.ctaButton}
          onMouseEnter={this.handleHoverPhone}
        >
          &nbsp;телефон
        </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


    【解决方案1】:

    @Andrewnosov 我已经制作了我认为更简单的代码来实现您的 gif 版本。

    CSS:

    h2 {
      display: flex;
      position: absolute;
      top: 0;
      left: 0;
      background-color: white;
      width: 100%;
      height: 40px;
    }
    

    JS:

    import React from 'react';
    import './App.css';
    
    class App extends React.Component {
        constructor() {
            super();
            this.state = {
                isHovered: false,
                showing: 'Email'
            };
        }
    
        setShowing = showing => {
            this.setState({ isHovered: true, showing });
        };
    
        render() {
            return (
                <div onMouseLeave={() => this.setState({ ...this.state, isHovered: false })}>
                    {this.state.showing === 'Phone' ? <h2>+7 992 020 1025</h2> : null}
                    {this.state.showing === 'Email' ? <h2>info@adnosov.ru</h2> : null}
                    {!this.state.isHovered ? (
                        <h2>
                            <span onMouseEnter={() => this.setShowing('Phone')}> Phone </span>
                            /
                            <span onMouseEnter={() => this.setShowing('Email')}> Email </span>
                        </h2>
                    ) : null}
                </div>
            );
        }
    }
    
    export default App;
    

    希望它会有所帮助:)

    【讨论】:

    • 谢谢!是的,这是更简单的方法,但是这个方法不能完成任务。它只是在将鼠标悬停在块上时交替发生邮件和电话,例如,如果我将鼠标悬停在电话上 - 我拿到了电话,第二次我将鼠标悬停在电话上,但我会收到电子邮件等。:(((
    • 非常感谢您指出这一点。我真的很抱歉我之前没有注意到,但我编辑了答案的 JavaScript 来实现它。仍然希望它对您有足够的帮助,并感谢您的帮助。
    • 效果很好!而且您的代码非常简单!非常感谢,我的朋友!
    • 很高兴它有帮助,请随时接受我的回答并投票。也谢谢你。我真的很感谢你的问题,继续努力。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-15
    • 2020-02-15
    • 1970-01-01
    相关资源
    最近更新 更多