【问题标题】:change opacity and animated that with react js用 react js 改变不透明度和动画
【发布时间】:2021-09-11 02:05:51
【问题描述】:

我用 react 开始了一个简单的项目。在我的项目中,我有一个段落,当鼠标悬停在段落上时(鼠标进入事件),段落下方会出现一个正方形,当从段落悬停时(鼠标离开事件)该正方形消失。但这发生得太快了,所以我想顺利地改变它,我想使用不透明度并将其从 0 更改为 1,并在我的事件发生时反转。但我不知道如何通过反应动画来改变不透明度。

这是我的应用程序

import './index.css';
import React, {useState} from "react";

function App() {
    const [isShowSquare, setIsShowSquare] = useState(false);
    const showSquare = () => {
        setIsShowSquare(true);
    }
    const hideSquare = () => {
        setIsShowSquare(false);
    }
    return (
        <div>
            <p onMouseEnter={showSquare} onMouseLeave={hideSquare} style={{display:'inline-block'}}>Hover Me</p>

            {isShowSquare ?
                <div className='square'>
                </div>
                : null
            }

        </div>

    );
}

export default App;

这是我的 index.css

*{
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
    'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
    sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.square{
  width: 50px;
  height: 50px;
  background-color: #61dafb;
}

如果有人可以帮助我,我将非常感激

【问题讨论】:

  • 你检查答案了吗?

标签: javascript css reactjs typescript mouseevent


【解决方案1】:

这是一种不使用useState 的方法。 我不知道这部分是否重要,但看看我的 sandbox

首先你需要一个css类来定义方法的不透明度和需要多少时间。此外,您的第一个 square 类应该有 opacity: 0,表示不可见。

当鼠标悬停在文本上时,将额外的类添加到元素中。

  const showSquare = () => {
    div.current.classList.add("square-full");
  };

  const hideSquare = () => {
    div.current.classList.remove("square-full");
  };

.square.square-full {
  opacity: 0.5;
  transition: opacity 1s ease-out;
}

.square {
  width: 50px;
  height: 50px;
  background-color: #61dafb;
  opacity: 0;
}

更新答案:不需要ref

只需使用以下代码

export default function App() {
  const [ isShown, setShown ] = useState(false)

  return (
    <div>
      <p
        onMouseEnter={() => setShown(true)}
        onMouseLeave={() => setShown(false)}
        style={{ display: "inline-block" }}
        class="paragraph"
      >
        Hover Me
      </p>

      <div className={`square ${isShown ? 'square-full' : ''}`}></div>
    </div>
  );
}

以及我之前提到的额外课程

【讨论】:

  • 谢谢你,但你能用查询选择器写这个吗,因为我读过一些地方最好不要在反应中使用查询选择器。(出于性能原因)
  • @Poldark 将沙箱更改为使用 ref。我不知道为什么这起初不起作用。奇怪
  • 如果你一遍又一遍地看到某件事,你就会意识到它如何变得更好。没有ref 的新答案和更新答案:)
  • 非常感谢。我会检查这个解决方案,我会告诉你结果
  • 我爱你,我爱你。我还有一个关于 swiper js 包的问题,​​我可以将我的问题的链接发送给你吗?
【解决方案2】:

出现很容易,而消失我找到了这样的解决方案;


import { useState } from "react";
import "./styles.css";

export default function App() {

  const [visible, setVisible] = useState(false)
  const [disappear, setDisappear] = useState(false)



  return (
    <div className="App">
      <p onMouseLeave={()=> {
        setDisappear(true)
        setTimeout(()=>{setVisible(false)
        setDisappear(false)}
        , 1000)      
      }} onMouseEnter={()=> setVisible(true)}>Hide/Show square </p>

{visible && <div className="square"  
style={{
width: 100,
height: 100,
animation: disappear ? "disappear 1s ease" : "appear 1s ease"
}}> </div> }  

 </div>
  );
}

对于css中的动画;

//style.css

.square {
  background-color: red;
  animation: appear 1s ease;

}
@keyframes appear {
  from {
    opacity: 0;
  }

  to {
    opacity: 1;
  }
}



@keyframes disappear {
  from {
    opacity: 1;
  }

  to {
    opacity: 0;
  }
}  

所以这里发生的情况是,我们的 css 上最初有一个不透明度的关键帧。所以这很好用,棘手的部分正在消失。当我们将可见状态设置为 false 时,React 会立即删除我们的元素,因此我们有 setTimeOut 来停止 React 1 秒。在那 1 秒钟内,我们应用了我们的动画,它在我身上流畅地运行。来试试吧。

【讨论】:

    猜你喜欢
    • 2011-08-27
    • 2016-11-15
    • 1970-01-01
    • 2010-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-07
    • 1970-01-01
    相关资源
    最近更新 更多