antd項(xiàng)目實(shí)現(xiàn)彩蛋效果的詳細(xì)代碼
馬上就要過節(jié)了,想把自己的項(xiàng)目搞得酷炫一些,對(duì)整個(gè)網(wǎng)站的按鈕添加圖標(biāo)、飄花效果、首屏大圖展示、頂部導(dǎo)航背景圖,于是就寫了這一遍文字,如有興趣的小伙伴們可以一起學(xué)習(xí)進(jìn)步,僅供參考。
1、效果圖
效果圖如下:
2、首先在components目錄下創(chuàng)建Transform目錄,包括index.css、index.js
index.css主要定義了幾種漂浮時(shí)的動(dòng)畫軌跡
/* 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是主要的邏輯代碼,下面對(duì)代碼進(jìn)行分析,完整代碼如下
- 進(jìn)入頁面調(diào)用 this.getTableList() 方法,獲取展示的圖片列表,包括btn(按鈕)、burst(首圖)、float(漂?。?、header(頂部);
- 緊接著調(diào)用 this.initStyle() 方法,首先對(duì)列表進(jìn)行循環(huán),選擇出當(dāng)前時(shí)間在開始時(shí)間、結(jié)束時(shí)間之間的一條數(shù)據(jù),然后再根據(jù)showoption對(duì)展示位置進(jìn)行判斷,并添置flag標(biāo)志;
- 接下來調(diào)用 this.loadStyleString(sty) ,傳入處理后的樣式字符串,創(chuàng)建style標(biāo)簽,添加到head中。
對(duì)于漂浮的特效,由于只定義了七種軌跡,所以最多上傳七張圖片,initList(data, cb) 方法進(jìn)行了處理,如果不夠七張,則會(huì)遞歸重復(fù)傳入數(shù)組,超過七張后截取前七張,然后回調(diào)。
對(duì)于首屏大圖,一天內(nèi)只出現(xiàn)一次,關(guān)閉時(shí)會(huì)同時(shí)設(shè)置localStorage的有效期,設(shè)置有效期一天,這樣就可以判斷當(dāng)前日期是否和localStorage中存的一樣啦。
頂部的開啟、關(guān)閉特效只對(duì)float(漂?。┳隽丝刂?,有效期為七天,超時(shí)會(huì)重新顯示。
// 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 設(shè)置的值不能為對(duì)象,轉(zhuǎn)為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(); } // 獲取七條數(shù)據(jù) 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關(guān)閉 CloseBurst() { this.setState( { showBurst: false, timeBurstLocal: this.state.timeBurst, }, () => { // 有效期兩天 localStorage.setExpire(`timeBurstLocal`, `${this.state.timeBurstLocal}`, 86400000 * 2); }, ); } // 調(diào)用示例 // 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; } } // 調(diào)用示例 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; } } // 開啟/關(guān)閉特效 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="關(guān)閉特效" unCheckedChildren="開啟特效" checked={this.state.floatFlag} onChange={(e) => this.switchCheck(e)} /> </div> ) : null} {this.selectHtmlBurst()} {this.selectHtml()} </React.Fragment> ); } }
3、全局引入
在layout布局中全局引入組件
到此這篇關(guān)于antd項(xiàng)目如何實(shí)現(xiàn)彩蛋效果的文章就介紹到這了,更多相關(guān)antd彩蛋內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Javascript實(shí)現(xiàn)鼠標(biāo)移入方向感知
這篇文章主要為大家詳細(xì)介紹了Javascript實(shí)現(xiàn)鼠標(biāo)移入方向感知,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06CommonJS與ES6?Module的使用區(qū)別分析
這篇文章主要為大家介紹了CommonJS與ES6?Module的使用區(qū)別分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04JavaScript實(shí)現(xiàn)簡(jiǎn)單購物小表格
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)簡(jiǎn)單購物小表格,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09D3.js實(shí)現(xiàn)雷達(dá)圖的方法詳解
大家應(yīng)該都知道基本圖表一共有六種,分別是柱狀圖、折線圖、散點(diǎn)圖、氣泡圖、餅圖和雷達(dá)圖。前面五種圖形都已經(jīng)介紹過如何實(shí)現(xiàn)了,今天我們一起來看看最后的雷達(dá)圖。有需要的朋友們可以參考學(xué)習(xí)下。2016-09-09JS實(shí)現(xiàn)鼠標(biāo)拖拽盒子移動(dòng)及右鍵點(diǎn)擊盒子消失效果示例
這篇文章主要介紹了JS實(shí)現(xiàn)鼠標(biāo)拖拽盒子移動(dòng)及右鍵點(diǎn)擊盒子消失效果,涉及javascript事件響應(yīng)及頁面元素屬性動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-01-01禁用backspace網(wǎng)頁回退功能的實(shí)現(xiàn)代碼
下面小編就為大家?guī)硪黄胋ackspace網(wǎng)頁回退功能的實(shí)現(xiàn)代碼。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11