React-基于react-transition-group 实现从下到上进入的入场动画
本功能是实现两个下拉选择框依次在页面滚动到指定位置时由下往上入场。
利用了CSSTransition实现。
react-transition-group官网:http://reactcommunity.org/react-transition-group/css-transition
页面代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | import React, { useState, useEffect } from 'react' ; import { CSSTransition } from 'react-transition-group' ; import Location from '@/assets/svg/location.svg' ; import ArrowUp from '@/assets/svg/arrow-down.svg' ; import Globe from '@/assets/images/globe.png' ; import './index.scss' ; const JoinUs = () => { const [options, setOptions] = useState( false ); const showOption = ()=>{ setOptions(!options); } const [inProp, setInProp] = useState( false ); const [inProp2, setInProp2] = useState( false ); const handleScroll = () => { const obj = document.querySelector( '.section-joinus' ); const clientHeight = document.documentElement.clientHeight; //浏览器可见区域高度。 const diff = clientHeight - obj.getBoundingClientRect().top; if (diff > 150){ setInProp( true ); } else { setInProp( false ); } if (diff > 300){ setTimeout(()=>{ setInProp2( true ); },1500) } else { setInProp2( false ); } }; const [items, setItems] = useState([ { id:0, country: 'Singapore' , work1: 'Product Manager / Product Owner' , work2: 'Legal Manager' , btntxt: 'View All Jobs' }, { id:1, country: 'China / Xiamen' , work1: 'Coporate Planning Manager' , work2: 'UI / UX Designer' , btntxt: 'View All Jobs' } ]); useEffect(() => { window.addEventListener( 'scroll' , handleScroll); return () => { window.removeEventListener( 'scroll' , handleScroll); }; }, []); return ( <div className= "section section-joinus" > <div className= 'padding37' > <div className= "page-wrapper-inner" > <h1>Join Us</h1> <div className= 'section-joinus__resc' > An opportunity to work with the best talents within the tech industry, with future hubs throughout Asia, there will be countless opportunities to travel, develop your skills and expand your knowledge while enjoying a new culture! </div> {items.map((item, index) => ( <CSSTransition key={index} in ={index === 0 ?inProp : inProp2} timeout={500} classNames= 'fade' > <div className= 'section-joinus__select' key={item.country}> <div className= 'country' onClick={() => showOption()}><i className= 'i-pos' ><Location/></i><span>{item.country}</span><i className= 'i-up' ><ArrowUp/></i></div> <div className= 'option' style={{height: index===0 ? options ? '0' : '200px' : options? '200px' : '0' }}> <ul> <li>{item.work1}</li> <li>{item.work2}</li> </ul> <button>{item.btntxt}</button> </div> </div> </CSSTransition> ))} </div> </div> <p className= 'section-joinus__china' >CHINA / XIAMEN</p> <p className= 'section-joinus__singa' >SINGAPORE</p> <i className= 'section-joinus__circleChina' ></i> <i className= 'section-joinus__circleSinga' ></i> <div className= 'section-joinus__bg' ><img src={Globe} /></div> </div> ); }; export default JoinUs; |
scss代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | //enter是入场前的刹那(点击按钮),appear指页面第一次加载前的一刹那(自动) //enter-active指入场后到入场结束的过程,appear-active则是页面第一次加载自动执行 .fade-enter { opacity: 0; transform: translate(0,200px); } .fade-enter-active { opacity: 1; transition: all .6s; transform: translate(0,0); } .fade-exit { opacity: 1; transition: all .6s; transform: translate(0,0); } .fade-exit-active { opacity: 0; transform: translate(0,200px); } |
分类:
react从入门到放弃
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具