如何让浮动社交分享按钮默认处于收起状态?
实现默认收起的浮动社交分享按钮
要让分享按钮默认处于收起状态,只需简单修改一处代码,再确认原有CSS逻辑即可:
修改步骤
- 移除HTML中menu标签的默认
open类
原代码里的<menu class="open">会触发图标默认展开,把class="open"去掉,改成<menu>就能让图标默认隐藏。 - CSS逻辑无需改动
原CSS已设置:只有当menu拥有open类时,.action类的社交图标才会显示(设置--factor: 1和opacity: 1);默认状态下图标会保持opacity:0和收起的位置,这部分逻辑完全符合需求。
修改后的完整代码
HTML代码
<head> <meta charset="UTF-8"> <link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css'> <link rel="stylesheet" href="style.css"> </head> <body> <menu> <a href="#" class="action"><i class="fab fa-dribbble"></i></a> <a href="#" class="action"><i class="fab fa-instagram"></i></a> <a href="#" class="action"><i class="fab fa-twitter"></i></a> <a href="#" class="action"><i class="fab fa-facebook-f"></i></a> <a href="#" class="trigger"><i class="fas fa-share"></i></a> </menu> <script src="script.js"></script> </body>
CSS代码(无需修改)
menu { --size: 2.1rem; --radius: 6rem; --padding: .5rem; --bg-color: rgba(255, 255, 255, 0.9); --fg-color: rgba(0, 0, 0, 0.7); --hi-color: #12192c; font-size: 29px; position: fixed; bottom: var(--padding); right: var(--padding); } menu > * { position: absolute; display: grid; place-content: center; border-radius: 50%; font-size: 29px; background: var(--bg-color); color: var(--fg-color); text-decoration: none; box-shadow: 0px 0px 9px 0px rgba(0, 0, 0, 0.6); } menu > .action { --factor: 0; width: 2.5rem; height: 2.5rem; right: calc(1.35 * var(--size)); bottom: calc(1.35 * var(--size)); opacity: 0; transform: rotate(calc(-1 * var(--angle))) translateY(calc(-1 * var(--radius) * var(--factor))) rotate(var(--angle)); transition: transform 250ms ease-in-out, opacity 250ms ease-in-out, box-shadow 250ms ease-in-out, color 250ms ease-in-out; } menu > .action:hover, menu > .trigger:hover { color: var(--hi-color); box-shadow: 0px 0px 0px 0.35rem rgba(0, 0, 0, 0.2); } menu.open > .action { --factor: 1; font-size: 20px; opacity: 1; } menu > .action:nth-child(1) { --angle: 0deg; transition-delay: 0ms; } menu > .action:nth-child(2) { --angle: 30deg; transition-delay: 50ms; } menu > .action:nth-child(3) { --angle: 60deg; transition-delay: 100ms; } menu > .action:nth-child(4) { --angle: 90deg; transition-delay: 150ms; } menu > .trigger { width: calc(1.3 * var(--size)); height: calc(1.3 * var(--size)); bottom: 0; right: 0; font-size: 1.5rem; transition: box-shadow 250ms ease-in-out, color 250ms ease-in-out; } menu > .trigger > i { transition: transform 250ms ease-in-out; } menu.open > .trigger > i { transform: rotate(-360deg); }
JavaScript代码(无需修改)
const trigger = document.querySelector("menu > .trigger"); trigger.addEventListener('click', (e) => { e.currentTarget.parentElement.classList.toggle("open"); });
内容的提问来源于stack exchange,提问作者Ridwan
相关产品推荐
相关产品推荐

