简体   繁体   English

我应该如何获得效果,当我单击按钮时,页面在React中变空白一秒钟

[英]how should I achieve the effect where when I click on a button, the page go blank for a second in React

I am new to React and css. 我是React和CSS的新手。 I wanted to make a button and when the button is clicked on, the page will go blank for, like 1 sec, then gradually returns to the norm. 我想创建一个按钮,当单击该按钮时,该页面将保持空白,例如1秒钟,然后逐渐恢复正常。

Suppose I have a button 假设我有一个按钮

<Button
 onClick={() => {
   goBlank();
   }}>
  button
</Button>

I figured that I need to use opacity and transition property. 我认为我需要使用opacitytransition属性。 But I couldn't seem to make that happen. 但我似乎无法实现这一目标。

Use this to click a button and have the page fade out and back in: 使用此按钮单击一个按钮,使页面淡出并返回:

Fadeback.js Fadeback.js

import React from 'react';
import './Fadeback.css';

export default class Fadeback extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      visible: true
    };
  }

  goBlank(){
    this.setState({ visible: false });
    setTimeout(() => this.setState({visible: true}), 1000);
  }

  render() {
    const classes = this.state.visible ? 'page-content' : 'page-content hide';
    return(
      <div className={classes}>
        <button onClick={() => this.goBlank()}>
          Go Blank
        </button>
        <div>Well this is the content</div>
        <div>Well this is the content</div>
        <div>Well this is the content</div>
        <div>Well this is the content</div>
        <div>Well this is the content</div>
      </div>
    );
  }

}

Fadeback.css Fadeback.css

.page-content {
    transition: opacity 0.5s;
    opacity: 1;
}

.page-content.hide {
    opacity: 0;
    pointer-events:none;
}

this does changes the opacity to 0 and sets it back to 1 a second later, for the gradual effect, you might want to use actual css transitions and achieve this effect by applying classes 这确实将不透明度更改为0,然后将其重新设置为1秒,为达到渐进效果,您可能希望使用实际的CSS过渡并通过应用类来实现此效果

blank = () => {
    document.body.style.setProperty("opacity", "0")
    setTimeout(document.body.style.setProperty("opacity", "1"), 1000)
}


<Button onClick={blank}>
  button
</Button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我单击按钮打开侧边栏时,它会显示一个空白页 - when I click the button to open the sidebar it brings a blank white page 当我单击 gliderjs 中的下一个/上一个按钮时,页面 go 到顶部 - Page go to top when i click on next/prev button in gliderjs 如何在按下第二个按钮时隐藏按钮,以及当我点击页面的空白区域时如何使按钮显示? - How to hide button when second is pressed and how to make it appear when i click on empty area of page? 当我单击打开按钮时,页面中应出现一个文本框 - when i click the open button a text box should appear in the page 当我点击按钮时,没有声音效果 - When I click the button, there is no sound effect 渲染react组件时如何摆脱3秒的延迟? - How should I rid of a 3 second delay when rendering react component? react native:当我从另一个页面单击按钮时,如何更改视图? - react native: How can I change a view when i click a button from another page? 如何在第二次单击按钮时重复@keyframes? - How to repeat @keyframes when I click a button in the second time? 当我单击一个按钮时,页面不会在 React Javascript 中刷新 - When I click a button the page does not refresh in React Javascript 单击按钮时使用 React 导航到新页面 - Navigate to a new page with React when I click on a button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM