React+ animate 添加动画

import React, { useState, useEffect, Fragment } from 'react'
import styles from './style.less'
import 'animate.css';
// 视频
const dataS = [
    { id: 1 },
    { id: 2 },
    { id: 3 },
]
// 圆点
const currentPageIndex = [
    { id: 1 },
    { id: 2 },
    { id: 3 },
]
const Index = () =>
{
    const [SwitchingCircles, setSwitchingCircles] = useState(1)

    useEffect(() =>
    {  // 发送数据请求 设置订阅/启动定时器 手动更改 DOM 等 ~
        document.documentElement.style.setProperty('--animate-duration', '1s');  // 使动画持续时间为 3 s
        return () =>
        {  // 组件卸载之前 做一些收尾工作 比如清楚定时器/取消订阅 等 ~
        }
    }, [])  // 检测数组内变量 如果为空 则监控全局
    // 添加动画类并自动删除它们
    const animateCSS = (element, animation, prefix = 'animate__') =>
        // 我们创建一个 Promise 并返回它
        new Promise((resolve, reject) =>
        {
            const animationName = `${prefix}${animation}`;
            const node = document.querySelector(element);

            node.classList.add(`${prefix}animated`, animationName);

            // 当动画结束时,我们清理类并解析 Promise
            function handleAnimationEnd (event)
            {
                event.stopPropagation();
                node.classList.remove(`${prefix}animated`, animationName);
                resolve('Animation ended');
            }

            node.addEventListener('animationend', handleAnimationEnd, { once: true });
        });

    const circleClick = (data) =>
    {
        const { id } = data
        if (id > SwitchingCircles)
        {
            animateCSS('.AddAnimation', 'fadeInRight')
        } else if (id < SwitchingCircles)
        {
            animateCSS('.AddAnimation', 'fadeInLeftBig')
        } else
        {
            animateCSS('.AddAnimation', 'bounce')
        }

        setSwitchingCircles(id)
    }


    return (
        <div className={styles.container}>
            <div style={{ display: 'flex', justifyContent: 'center' }} className={styles.overall} className='AddAnimation' >
                {SwitchingCircles === 1 ? (
                    <div>
                        {dataS.map((data, index) => (
                            <div key={index} className={styles.core}>
                                <div className={styles.video}>312</div>
                            </div>
                        ))}
                    </div>
                ) : null}

                {SwitchingCircles === 2 ? (
                    <div>
                        {dataS.map((data, index) => (
                            <div key={index} className={styles.core}>
                                <div className={styles.video}>12222</div>
                            </div>
                        ))}
                    </div>
                ) : null}
                {SwitchingCircles === 3 ? (
                    <div>
                        {dataS.map((data, index) => (
                            <div key={index} className={styles.core}>
                                <div className={styles.video}>33333</div>
                            </div>
                        ))}
                    </div>
                ) : null}
            </div>
            <div className={styles.round}>
                {currentPageIndex.map((data, index) => (
                    <h1 key={index} onClick={() => circleClick(data)} style={data.id === SwitchingCircles ? { background: 'rgba(42, 164, 252, 1)' } : null} className={styles.SmallCircle}></h1>
                ))}
            </div>
        </div >
    )
}

export default Index

.container {
  width: 100%;
  height: 100%;
  position: relative;

  .overall {
    margin: 0 auto;
    position: relative;
    width: 1000px;
    height: 237px;
    box-sizing: border-box;
    // background-color: pink;
    padding: 24px 0 0 30px;
    background-color: pink;
  }
}


.core {
  position: relative;
  display: inline-block;
  padding: 13px;
  width: 304px;
  height: 171px;
  border: 1px solid #4776BC;
  margin-right: 14px;

  .video {
    height: 100%;
    background-color: pink;
  }

  .video::before {
    position: absolute;
    top: -1px;
    left: -1px;
    content: "";
    width: 0;
    height: 0;
    border-left: 10px solid #1DA2FF;
    border-right: 5px solid transparent;
    border-bottom: 10px solid transparent;
    //   border-top: 5px solid #1DA2FF;
  }

  .video::after {
    position: absolute;
    right: -1px;
    bottom: -1px;
    content: "";
    border-bottom: 10px solid #1DA2FF;
    border-left: 10px solid transparent;
  }



}

.round {
  display: flex;
  justify-content: center;

  .SmallCircle {
    cursor: pointer;
    margin: 18px;
    width: 14px;
    height: 14px;
    background: #0B3858;
    border-radius: 50%;
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

臧小川

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值