【问题标题】:CSS animation on an element after onClickonClick 后元素上的 CSS 动画
【发布时间】:2020-06-29 21:10:50
【问题描述】:

我想用 onclick 为我的按钮设置动画,但它不起作用:

css 文件(style.css):

.slide-out-top {
    -webkit-animation: slide-out-top 1s cubic-bezier(0.550, 0.085, 0.680, 0.530) both;
            animation: slide-out-top 1s cubic-bezier(0.550, 0.085, 0.680, 0.530) both;
}
@-webkit-keyframes slide-out-top {
  0% {
    -webkit-transform: translateY(0);
            transform: translateY(0);
    opacity: 1;
  }
  100% {
    -webkit-transform: translateY(-1000px);
            transform: translateY(-1000px);
    opacity: 0;
  }
}
@keyframes slide-out-top {
  0% {
    -webkit-transform: translateY(0);
            transform: translateY(0);
    opacity: 1;
  }
  100% {
    -webkit-transform: translateY(-1000px);
            transform: translateY(-1000px);
    opacity: 0;
  }
}
.slide-out-bottom {
    -webkit-animation: slide-out-bottom 1s cubic-bezier(0.550, 0.085, 0.680, 0.530) both;
            animation: slide-out-bottom 1s cubic-bezier(0.550, 0.085, 0.680, 0.530) both;
    -webkit-animation-timing-function: ease;
            animation-timing-function: ease;
}
@-webkit-keyframes slide-out-bottom {
  0% {
    -webkit-transform: translateY(0);
            transform: translateY(0);
    opacity: 1;
  }
  100% {
    -webkit-transform: translateY(1000px);
            transform: translateY(1000px);
    opacity: 0;
  }
}
@keyframes slide-out-bottom {
  0% {
    -webkit-transform: translateY(0);
            transform: translateY(0);
    opacity: 1;
  }
  100% {
    -webkit-transform: translateY(1000px);
            transform: translateY(1000px);
    opacity: 0;
  }
}

jsx 文件(preact):

import { h, Component } from 'preact';
import "../../style/index.css";
import "../../style/bootstrap.min.css";
import "./style.css";

export default class Home extends Component {

    buttonExit = () => {
        document.getElementById('buttonToExit').className = 'btn btn-outline-light slide-out-bottom';
        document.getElementById('textToExit').className = 'pt-5 pb-3 font-weight-bold text-light h1 slide-out-top';
    }

    render = () => {
        return (
            <div className="App vh-100">
                <div className="h-70 container">
                    <div className="py-5"> </div>
                    <div className="py-5" style={{ width: "100%" }}>
                        <div class="container">
                            <div class="row">
                                <div class="col-sm"> </div>
                                <div class="col text-center">
                                    <div id="textToExit" className="pt-5 pb-3 font-weight-bold text-light h1">Accès en ligne</div>
                                    <button type="button" id="buttonToExit" onClick={this.buttonExit} className="btn btn-outline-light">Se Connecter</button>
                                </div>
                                <div class="col-sm"> </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        );
    }
}

style.css 与 js 文件位于同一文件夹中。

我的函数应该修改按钮和文本元素的“类名”。 经过几次测试,该函数似乎被很好地调用了,并且动画正常运行。

有人可以帮帮我吗?

【问题讨论】:

    标签: javascript html css jsx preact


    【解决方案1】:

    className 在 DOM 中不存在,它是一个 JSX 抽象,用于代替 class,因为这是一个保留的 JavaScript 字。当您在 React 中使用虚拟 DOM(或 preact,我假设,并不完全熟悉它)时,所有文档语句(如 getElementById)也不会按预期运行。

    我会在这里考虑两个选项,使用RefsState。使用Refs,您可以跟踪DOM 节点并直接更新它,尽管这不是一个动态的或一般建议的解决方案,因为您必须事先声明所有Refs。但是,嘿,它有效。

    textRef = createRef();
    buttonRef = createRef();
    
    buttonExit = () => {
        textRef.current.classList.add('slide-out-top');
        buttonRef.current.classList.add('slide-out-bottom');
    }
    
    render = () => {
        return (
            ...your code...
            <div id="textToExit" ref={this.textRef}...>
            <button type="button" id="buttonToExit" ref={this.buttonRef}...>
            ...your code...
        )
    }
    

    使用State,您可以根据组件的当前状态有选择地激活类列表的不同部分。

    state = {
        exit: false
    }
    
    buttonExit = () => {
        this.setState({exit: true});
    }
    
    render = () => {
        const buttonExit = this.state.exit ? 'slide-out-bottom' : '';
        const textExit = this.state.exit ? 'slide-out-top' : '';
    
        return (
            ...your code...
            <div id="textToExit" className={`pt-5 pb-3 font-weight-bold text-light h1 ${textExit}`}...>
            <button type="button" id="buttonToExit" className={`btn btn-outline-light ${buttonExit}`}...>
            ...your code...
        )
    }
    

    【讨论】:

      猜你喜欢
      • 2020-08-24
      • 2011-06-18
      • 1970-01-01
      • 1970-01-01
      • 2017-02-14
      • 2017-07-04
      • 2019-11-01
      • 2020-06-26
      • 1970-01-01
      相关资源
      最近更新 更多