Toggle navigation
在线编辑器
在线代码
文本比较
jQuery下载
前端库
在线手册
登录/注册
下载代码
html
css
js
分享到微信朋友圈
X
html
css
*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; } :root { font-size: 62.5%; } html, body { height: 100%; } body { background: #f1f5fe; display: flex; justify-content: center; align-items: center; } .switch { background: transparent; outline: none; border: none; cursor: pointer; position: relative; background: #3B3364; border-radius: 100rem; -webkit-tap-highlight-color: transparent; } .switch__background { position: absolute; top: 0; left: 0; background: #3B3364; border-radius: 100rem; width: 20rem; height: 100%; z-index: -1; } .egg { height: 14.4rem; width: 32.4rem; } .egg__mainGroup { transform: translate(2.2rem, calc(2.2rem + 1.2px)); } .egg__white.-hidden { visibility: hidden; }
JavaScript
const switchButton = document.querySelector('.switch'); const switchBackground = switchButton.querySelector('.switch__background'); const egg = switchButton.querySelector('.egg'); const eggMainGroup = egg.querySelector('.egg__mainGroup'); const eggWhite = egg.querySelector('.egg__white.-fried'); const eggWhiteMiddle = egg.querySelector('.egg__white.-middle'); const eggWhiteRaw = egg.querySelector('.egg__white.-raw'); const yolk = egg.querySelector('.egg__yolk'); const yolkShadows = Array.from(egg.querySelectorAll('.egg__yolkShadow')); const EGG_RAW_PATH = eggWhiteRaw.getAttribute('d'); const EGG_MIDDLE_PATH = eggWhiteMiddle.getAttribute('d'); const EGG_FRIED_PATH = eggWhite.getAttribute('d'); const SWITCH_PADDING = Number( getComputedStyle(eggMainGroup).transform.split(',')[4] ); const { switchBackgroundCoords, eggCoords, eggWhiteCoords, eggWhiteRawCoords, eggWhiteMiddleCoords, yolkCoords, } = (() => { const elements = { switchBackground, egg, eggWhite, eggWhiteRaw, eggWhiteMiddle, yolk }; return Object.keys(elements).reduce((previous, elementName) => { return { ...previous, [`${elementName}Coords`]: elements[elementName].getBoundingClientRect() }; }, {}); })(); let isAnimating = false; const toggleAnimationController = () => { isAnimating = !isAnimating; }; const toggleEggWhite = isOff => { const scaleMiddle = 0.7; const xMiddle = eggCoords.width / 2 - (eggWhiteCoords.width * scaleMiddle) / 2; new TimelineLite() .to(eggWhite, 0.25, { x: xMiddle, y: eggWhiteMiddleCoords.height / 4, scale: scaleMiddle, ease: Circ.easeIn, attr: { d: EGG_MIDDLE_PATH } }) .to(eggWhite, 0.25, { x: isOff ? '0%' : eggCoords.width - eggWhiteRawCoords.width - SWITCH_PADDING * 2, y: '0%', scale: 1, ease: Back.easeOut.config(1.7), attr: { d: isOff ? EGG_FRIED_PATH : EGG_RAW_PATH } }); }; const toggleYolk = isOff => { const xMiddle = eggCoords.width / 2 - yolkCoords.width; const xRaw = eggCoords.width - eggWhiteRawCoords.width - yolkCoords.width - SWITCH_PADDING / 2; new TimelineLite() .to(yolk, 0.25, { x: xMiddle, y: 10, ease: Circ.easeIn, opacity: 0 }) .to(yolk, 0.25, { x: isOff ? '0%' : xRaw, fill: isOff ? '#facf37' : '#FDDD71', y: isOff ? '0%' : 15, opacity: 1, ease: Back.easeOut.config(1.7) }); }; const toggleYolkShadows = isOff => { const toggleShadow = item => { new TweenLite(item, 0.1, { ...isOff ? { delay: 0.35 } : {}, opacity: isOff ? 1 : 0, }) } yolkShadows.forEach(toggleShadow); }; const toggleSwitchMovement = isOff => { new TweenLite(switchButton, 0.4, { x: isOff ? '0%' : '3vw', ease: Power1.easeInOut }); }; const toggleSwitchBackground = isOff => { const x = isOff ? '0%' : eggCoords.width - switchBackgroundCoords.width; new TweenMax(switchBackground, 0.5, { x, ease: Back.easeOut.config(1.7), onComplete: toggleAnimationController, }); }; const toggle = () => { if (isAnimating) return; toggleAnimationController(); const isOff = switchButton.classList.contains('-off'); switchButton.classList.toggle('-off'); toggleSwitchBackground(isOff); toggleEggWhite(isOff); toggleYolk(isOff); toggleYolkShadows(isOff); toggleSwitchMovement(isOff); }; switchButton.addEventListener('click', toggle);
粒子
时间
文字
hover
canvas
3d
游戏
音乐
火焰
水波
轮播图
鼠标跟随
动画
css
加载动画
导航
菜单
按钮
滑块
tab
弹出层
统计图
svg
×
Close
在线代码下载提示
开通在线代码永久免费下载,需支付20jQ币
开通后,在线代码模块中所有代码可终身免费下!
您已开通在线代码永久免费下载,关闭提示框后,点下载代码可直接下载!
您已经开通过在线代码永久免费下载
对不起,您的jQ币不足!可通过发布资源 或
直接充值获取jQ币
取消
开通下载
<!doctype html> <html> <head> <meta charset="utf-8"> <title>荷包蛋开关切换效果-jq22.com</title> <script src="https://www.jq22.com/jquery/jquery-1.10.2.js"></script> <style>
</style> </head> <body>
<script>
</script>
</body> </html>
2012-2021 jQuery插件库版权所有
jquery插件
|
jq22工具库
|
网页技术
|
广告合作
|
在线反馈
|
版权声明
沪ICP备13043785号-1
浙公网安备 33041102000314号