iis服务器助手广告广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >antd项目实现彩蛋效果的详细代码
  • 562
分享到

antd项目实现彩蛋效果的详细代码

2024-04-02 19:04:59 562人浏览 八月长安
摘要

目录1、效果图2、首先在components目录下创建TransfORM目录,包括index.CSS、index.js3、全局引入马上就要过节了,想把自己的项目搞得酷炫一些,对整个网

马上就要过节了,想把自己的项目搞得酷炫一些,对整个网站的按钮添加图标、飘花效果、首屏大图展示、顶部导航背景图,于是就写了这一遍文字,如有兴趣的小伙伴们可以一起学习进步,仅供参考。

1、效果图

效果图如下:

2、首先在components目录下创建Transform目录,包括index.css、index.js

index.css主要定义了几种漂浮时的动画轨迹


.animation1 {
  display: inline-block;
  position: fixed;
  z-index: 2000;
  opacity: 0;
  top: -40px;
  left: -40px;
  animation: animation1 8s linear infinite;
}
 
.animation2 {
  display: inline-block;
  position: fixed;
  z-index: 2000;
  opacity: 0;
  top: -40px;
  left: -40px;
  animation: animation2 9s 1s linear infinite;
}
 
.animation3 {
  display: inline-block;
  position: fixed;
  z-index: 2000;
  opacity: 0;
  top: -40px;
  left: -40px;
  animation: animation3 9s linear infinite;
}
 
.animation4 {
  display: inline-block;
  position: fixed;
  z-index: 2000;
  opacity: 0;
  top: -40px;
  left: -40px;
  animation: animation4 9s 2s linear infinite;
}
 
.animation5 {
  display: inline-block;
  position: fixed;
  z-index: 2000;
  opacity: 0;
  top: -40px;
  left: -40px;
  animation: animation5 9s 1s linear infinite;
}
 
.animation6 {
  display: inline-block;
  position: fixed;
  z-index: 2000;
  opacity: 0;
  top: -40px;
  left: -40px;
  animation: animation6 9s 3s linear infinite;
}
 
.animation7 {
  display: inline-block;
  position: fixed;
  z-index: 2000;
  opacity: 0;
  top: -40px;
  left: -40px;
  animation: animation7 8s linear infinite;
}
 
.animation8 {
  display: inline-block;
  position: fixed;
  z-index: 2000;
  opacity: 0;
  top: -40px;
  right: -200px;
  animation: animation8 10s linear infinite;
}
 
 
@keyframes animation1 {
  0% {
    top: 50%;
    left: -80px;
    opacity: 0;
  }
 
  90% {
    opacity: 1;
  }
 
  100% {
    top: 100%;
    left: 20%;
    opacity: 0;
  }
}
 
@keyframes animation2 {
  0% {
    top: 80px;
    left: -80px;
    opacity: 0;
  }
 
  90% {
    opacity: 1;
  }
 
  100% {
    top: 100%;
    left: 50%;
    opacity: 0;
  }
}
 
@keyframes animation3 {
  0% {
    top: 30%;
    left: 20%;
    opacity: 0;
  }
 
  90% {
    opacity: 1;
  }
 
  100% {
    top: 110%;
    left: 75%;
    opacity: 0;
  }
}
 
@keyframes animation4 {
  0% {
    top: -80px;
    left: -80px;
    opacity: 0;
  }
 
  90% {
    opacity: 1;
  }
 
  100% {
    top: 101%;
    left: 80%;
    opacity: 0;
  }
}
 
@keyframes animation5 {
  0% {
    top: 10%;
    left: 40%;
    opacity: 0;
  }
 
  90% {
    opacity: 1;
  }
 
  100% {
    top: 100%;
    left: 120%;
    opacity: 0;
  }
}
 
@keyframes animation6 {
  0% {
    top: -80px;
    left: 50%;
    opacity: 0;
  }
 
  90% {
    opacity: 1;
  }
 
  100% {
    top: 100%;
    left: 110%;
    opacity: 0;
  }
}
 
@keyframes animation7 {
  0% {
    top: -80px;
    left: 70%;
    opacity: 0;
  }
 
  90% {
    opacity: 1;
  }
 
  100% {
    top: 50%;
    left: 110%;
    opacity: 0;
  }
}
@keyframes animation8 {
  0% {
    top: -75px;
    right: -200px;
    opacity: 0;
    transform: rotate(0)
  }
 
  50% {
    transform: rotate(-30deg)
  }
 
  90% {
    opacity: 1;
  }
 
  100% {
    top: 100%;
    right: 100%;
    transform: rotate(0);
    opacity: 0;
  }
}
:global .switchStyle {
  z-index: 31;
}
 
.shadeWrapper {
  z-index: 1050;
  position: fixed;
  top: 0;
  left: 200px;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.3);
  display: flex;
  justify-content: center;
  align-items: center;
 
}
.shadeWrapper .shadeClose {
  cursor: pointer;
  font-size: 30px;
  position: absolute;
  right: 30px;
  top: 30px;
  color: #fff;
}

index.js是主要的逻辑代码,下面对代码进行分析,完整代码如下

  • 进入页面调用 this.getTableList()  方法,获取展示的图片列表,包括btn(按钮)、burst(首图)、float(漂浮)、header(顶部);
  • 紧接着调用 this.initStyle() 方法,首先对列表进行循环,选择出当前时间在开始时间、结束时间之间的一条数据,然后再根据showoption对展示位置进行判断,并添置flag标志;
  • 接下来调用 this.loadStyleString(sty) ,传入处理后的样式字符串,创建style标签,添加到head中。

对于漂浮的特效,由于只定义了七种轨迹,所以最多上传七张图片,initList(data, cb) 方法进行了处理,如果不够七张,则会递归重复传入数组,超过七张后截取前七张,然后回调。

对于首屏大图,一天内只出现一次,关闭时会同时设置localStorage的有效期,设置有效期一天,这样就可以判断当前日期是否和localStorage中存的一样啦。

顶部的开启、关闭特效只对float(漂浮)做了控制,有效期为七天,超时会重新显示。

// index.js
import React, { PureComponent } from 'react';
import { Switch } from 'antd';
import { CloseCircleOutlined } from '@ant-design/icons';
import './index.css';
import moment from 'moment';
Storage.prototype.setExpire = (key, value, expire) => {
  let obj = {
    data: value,
    time: Date.now(),
    expire: expire,
  };
  //localStorage 设置的值不能为对象,转为JSON字符串
  localStorage.setItem(key, JSON.stringify(obj));
};
Storage.prototype.getExpire = (key) => {
  let val = localStorage.getItem(key);
  if (!val) {
    return val;
  }
  val = JSON.parse(val);
  if (Date.now() - val.time > val.expire) {
    localStorage.removeItem(key);
    return null;
  }
  return val.data;
};
export default class Transform extends PureComponent {
  constructor(props) {
    super(props);
    let storage = localStorage.getExpire(`floatFlag`);
    if (storage == 'false' || storage == false) {
      storage = false;
    } else {
      storage = true;
    }
    this.state = {
      showBurst: false, // burst
      pathBurst: '', // burst path
      timeBurst: '', // burst time
      showFloat: false,
      showBtn: false,
      floatFlag: storage,
      floatList: [],
    };
  }
 
  componentDidMount() {
    this.getTableList();
  }
  // 获取七条数据
  initList(data, cb) {
    if (data && data.length < 7) {
      let dt = data.concat(data);
      this.initList(dt, cb);
    } else {
      cb(data.slice(0, 7));
    }
  }
  // 获取列表
  getTableList() {
    let list = [
      {
        showoption: 'btn',
        starttime: '2022-09-03',
        endtime: '2023-09-03',
        imagaddress: [
          {
            path: 'https://img-blog.csdnimg.cn/3e93fe58b6444c2c8165e85756118888.png',
          },
        ],
      },
      {
        showoption: 'burst',
        starttime: '2022-09-03',
        endtime: '2023-09-03',
        imagaddress: [
          {
            path: 'Https://img-blog.csdnimg.cn/a1c4ddc6b73b48c5a88512eba3a907fa.jpeg',
          },
        ],
      },
      {
        showoption: 'float',
        starttime: '2022-09-03',
        endtime: '2023-09-03',
        imagaddress: [
          {
            path: 'https://img-blog.csdnimg.cn/3e93fe58b6444c2c8165e85756118888.png',
          },
          {
            path: 'https://img-blog.csdnimg.cn/3e93fe58b6444c2c8165e85756118888.png',
          },
        ],
      },
      {
        showoption: 'header',
        starttime: '2022-09-03',
        endtime: '2023-09-03',
        imagaddress: [
          {
            path: 'https://img-blog.csdnimg.cn/a1c4ddc6b73b48c5a88512eba3a907fa.jpeg',
          },
        ],
      },
    ];
    this.setState(
      {
        tableList: list,
      },
      () => {
        this.initStyle();
      },
    );
  }
  add0(m) {
    return m < 10 ? '0' + m : m;
  }
  initStyle() {
    try {
      let tableList = JSON.parse(JSON.stringify(this.state.tableList));
      let resDt = [];
      let time = new Date();
      var y = time.getFullYear();
      var m = time.getMonth() + 1;
      var d = time.getDate();
      var H = time.getHours();
      var FEN = time.getMinutes();
      var Miao = time.getSeconds();
      let newDe = moment(
        `${y}-${this.add0(m)}-${this.add0(d)} ${this.add0(H)}:${this.add0(FEN)}:${this.add0(Miao)}`,
      ).format('YYYY-MM-DD HH:mm:ss');
      for (let index = 0; index < tableList.length; index++) {
        const element = tableList[index];
        let strDe = moment(`${element.starttime} 00:00:00`).format('YYYY-MM-DD HH:mm:ss');
        let endDe = moment(`${element.endtime} 23:59:59`).format('YYYY-MM-DD HH:mm:ss');
        if (moment(strDe).isBefore(moment(newDe)) && moment(newDe).isBefore(moment(endDe))) {
          resDt.push(element);
          // break;
        }
      }
      if (resDt && resDt.length > 0) {
        for (let idx = 0; idx < resDt.length; idx++) {
          const element = resDt[idx];
          if (element.showoption == 'float') {
            let str = element.imagaddress;
            let list = [];
            this.initList(str, (dt) => {
              list = dt;
            });
            this.setState({
              showFloat: true,
              showBtn: true,
              floatList: list,
            });
          } else if (element.showoption == 'btn') {
            let strBtn = element.imagaddress[0].path;
            let sty = `
              .ant-btn::before {
                  content: " ";
                  display: block;
                  background: url(${strBtn}) no-repeat!important;
                  background-size: 20px !important;
                  height: 100%;
                  width: 100%;
                  position: absolute;
                  top: -10px;
                  left: -10px;
                  opacity: 1;
              }
              `;
            let sty2 = `
              .ant-btn::before {
                  content: " ";
                  display: block;
                  background: transparent!important;
                  background-size: 20px !important;
                  height: 100%;
                  width: 100%;
                  position: absolute;
                  top: -10px;
                  left: -10px;
                  opacity: 1;
              }
              `;
            this.loadStyleString(sty);
          } else if (element.showoption == 'burst') {
            let tmZl = `${y}-${this.add0(m)}-${this.add0(d)}`;
            let flag = true;
            if (localStorage.getExpire(`timeBurstLocal`) == `${tmZl}`) {
              flag = false;
            }
            this.setState({
              showBurst: flag,
              pathBurst: element.imagaddress[0].path,
              timeBurst: tmZl,
            });
          } else if (element.showoption == 'header') {
            let strH = element.imagaddress[0].path;
            let styH = `
              .ant-pro-global-header {
                background-image: url(${strH});
                background-repeat: no-repeat;
                background-size: cover;
                // opacity: 0.8;
              }
              `;
            this.loadStyleString(styH);
          }
        }
      } else {
        this.setState({
          showFloat: false,
          showBtn: false,
          floatFlag: false,
          floatList: [],
        });
      }
    } catch (error) {
      this.setState({
        showFloat: false,
        showBtn: false,
        floatFlag: false,
        floatList: [],
      });
    }
  }
  loadStyleString(css) {
    var style = document.createElement('style');
    style.type = 'text/css';
    try {
      style.appendChild(document.createTextnode(css));
    } catch (ex) {
      style.styleSheet.cssText = css; //兼容IE
    }
    var head = document.getElementsByTagName('head')[0];
    head.appendChild(style);
  }
  // burst关闭
  CloseBurst() {
    this.setState(
      {
        showBurst: false,
        timeBurstLocal: this.state.timeBurst,
      },
      () => {
        // 有效期两天
        localStorage.setExpire(`timeBurstLocal`, `${this.state.timeBurstLocal}`, 86400000 * 2);
      },
    );
  }
  // 调用示例
  //   loadStyleString("body{background-color:red}");
  selecthtml() {
    if (this.state.showFloat && this.state.floatFlag) {
      return (
        <React.Fragment>
          {this.state.floatList
            ? this.state.floatList.map((item, index) => {
                let width = Math.round(Math.random() * 20 + 30);
                return (
                  <div key={index} className={`animation${index}`}>
                    <img width={width} src={item.path} />
                  </div>
                );
              })
            : ''}
        </React.Fragment>
      );
    } else {
      return null;
    }
  }
  // 调用示例
  selectHtmlBurst() {
    var tempHeightRight = document.documentElement.clientHeight - 100;
    if (this.state.showBurst && this.state.pathBurst) {
      return (
        <React.Fragment>
          <div
            onClick={() => {
              this.CloseBurst();
            }}
            className="shadeWrapper"
            style={{ cursor: 'pointer' }}
          >
            <div
              onClick={() => {
                this.CloseBurst();
              }}
              className="shadeClose"
            >
              <CloseCircleOutlined />
            </div>
            <div>
              <img style={{ maxHeight: tempHeightRight }} src={this.state.pathBurst} />
            </div>
          </div>
        </React.Fragment>
      );
    } else {
      return null;
    }
  }
  // 开启/关闭特效
  switchCheck(e) {
    // 有效期七天
    localStorage.setExpire(`floatFlag`, e, 86400000 * 7);
    this.setState(
      {
        floatFlag: e,
      },
      () => {
        console.log('e', e);
        // this.initStyle();
      },
    );
  }
  render() {
    return (
      <React.Fragment>
        {this.state.showBtn ? (
          <div
            style={{
              display: 'inline-block',
              position: 'fixed',
              top: '10px',
              right: '300px',
              zIndex: '31',
            }}
          >
            <Switch
              checkedChildren="关闭特效"
              unCheckedChildren="开启特效"
              checked={this.state.floatFlag}
              onChange={(e) => this.switchCheck(e)}
            />
          </div>
        ) : null}
        {this.selectHtmlBurst()}
        {this.selectHtml()}
      </React.Fragment>
    );
  }
}

3、全局引入

在layout布局中全局引入组件

到此这篇关于antd项目如何实现彩蛋效果的文章就介绍到这了,更多相关antd彩蛋内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: antd项目实现彩蛋效果的详细代码

本文链接: https://www.lsjlt.com/news/167645.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • antd项目实现彩蛋效果的详细代码
    目录1、效果图2、首先在components目录下创建Transform目录,包括index.css、index.js3、全局引入马上就要过节了,想把自己的项目搞得酷炫一些,对整个网...
    99+
    2024-04-02
  • 详解OpenGLShader彩虹条纹效果的实现
    目录前言回顾效果实现增加条纹角度变化拓展前言 在一款图片处理软件colorow中发现彩虹效果滤镜感觉蛮有意思。 在OpenGL Shader系列更新到现在尝试通过之前积累的知识点来...
    99+
    2024-04-02
  • vue项目中添加electron的详细代码
    1.在package.json中添加 "main": "electron.js", 在 “scripts”: {添加: "package": "electron-pac...
    99+
    2024-04-02
  • Sphinx 实现 PHP 项目的实时搜索效果
    一、安装 Sphinx要在 PHP 项目中实现实时搜索效果,首先需要安装 Sphinx。可以在 Sphinx 的官方网站(http://sphinxsearch.com/)上下载最新版本的 Sphinx,然后按照官方文档进行安装。二、创建 ...
    99+
    2023-10-21
    PHP Sphinx 实时搜索
  • Rust 实现 async/await的详细代码
    目录FutureWake & Context为什么需要 executor ?什么是 waker ?async/awaitExecutorWaker struct 到 ArcW...
    99+
    2024-04-02
  • idea项目代码打包为jar包的超详细步骤
    目录前言一、将所有依赖和模块代码打包为一个jar二、只将模块代码打包为一个jar总结 前言 假设我有如下简单 maven 项目 点击 File ==> Projec...
    99+
    2023-01-11
    idea将项目打包成jar包 idea 打包jar idea 打包jar
  • Python实现屏幕代码雨效果的示例代码
    直接上代码 import pygame import random def main(): # 初始化pygame pygame.init() #...
    99+
    2024-04-02
  • css实现“加号”效果的实例代码
    这篇文章给大家分享的是有关css实现“加号”效果的实例代码的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。实现下图的加号效果:若想实现这个效果, 只需一个div元素即可搞定。需要用到css的为了before和aft...
    99+
    2023-06-08
  • vue实现轮播图效果的代码
    这篇文章主要介绍“vue实现轮播图效果的代码”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“vue实现轮播图效果的代码”文章能帮助大家解决问题。1.原理:v-on:click="prev&q...
    99+
    2023-06-27
  • 怎么用jQuery代码实现选项联动轮播效果
    今天小编给大家分享一下怎么用jQuery代码实现选项联动轮播效果的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。代码如下:&l...
    99+
    2023-07-04
  • js实现拉幕效果的广告代码
    以下是一个简单的 JavaScript 实现拉幕效果的广告代码:```html.ad-container {width: 100%;...
    99+
    2023-08-09
    js
  • AndroidFlutter实现点赞效果的示例代码
    目录前言绘制小手完整源码前言 点赞这个动作不得不说在社交、短视频等App中实在是太常见了,当用户手指按下去的那一刻,给用户一个好的反馈效果也是非常重要的,这样用户点起赞来才会有一种强...
    99+
    2024-04-02
  • MyBatis实现多表联查的详细代码
    目录一、通过映射配置文件实现多表联查二、使用注解的方式一、通过映射配置文件实现多表联查 首先,使用Mysql数据库,创建两个表,分别为学生表Student表和班级表Class表,在S...
    99+
    2022-11-13
    MyBatis多表联查 MyBatis多表查询
  • 从0到1实现GCN——最详细的代码实现
    最近论文中需要使用图卷积神经网络(GNN),看了一些关于GCN的代码,还有基于PyTorch Geometric Temporal的代码实现,在这里做一下记录。 GCN原始代码 关于GCN的原理在这里不进行过多阐述,其他文章里面解释的已经很...
    99+
    2023-10-26
    pytorch 深度学习 人工智能 python
  • Sphinx 实现 PHP 项目的模糊匹配搜索效果
    首先,我们需要在 PHP 项目中安装 Sphinx。可以使用 Composer 包管理工具来安装 Sphinx 的 PHP 包。在项目根目录下的 composer.json 文件中,添加 Sphinx 包的依赖:{ "requ...
    99+
    2023-10-21
    Sphinx PHP 项目 模糊匹配搜索
  • Android直播软件搭建之实现背景颜色滑动渐变效果的详细代码
    Android直播软件搭建实现背景颜色滑动渐变效果的相关代码 一、介绍一下GradientDrawable GradientDrawable 支持渐变色的Drawable,与sha...
    99+
    2024-04-02
  • C#实现跑马灯效果的示例代码
    目录文章描述开发环境开发工具实现代码实现效果文章描述 跑马灯效果,功能效果大家应该都知道,就是当我们的文字过长,整个页面放不下的时候(一般用于公告等),可以让它自动实现来回滚动,以让...
    99+
    2022-11-13
    C#实现跑马灯效果 C# 跑马灯
  • Unity实现跑马灯效果的示例代码
    目录一、效果二、需要动画插件DOTween三、脚本1.每个格子上的脚本文件2.管理脚本文件一、效果 二、需要动画插件DOTween 下载地址 三、脚本 1.每个格子上的脚本文件 u...
    99+
    2024-04-02
  • Android RecyclerView打造悬浮效果的实现代码
    本文介绍了Android RecyclerView悬浮效果,分享给大家,具体如下:先看个效果这是一个City列表,每个City都有所属的Province,需要在滑动的时候,将对应的Province悬浮在顶部。悬浮顶部的Province需要根...
    99+
    2023-05-30
    android recyclerview 悬浮
  • JavaScript中canvas实现代码雨效果的方法
    这篇文章将为大家详细讲解有关JavaScript中canvas实现代码雨效果的方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。本文实例为大家分享了canvas实现代码雨效果的具体代码,供大家参考,具体内...
    99+
    2023-06-15
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作