演示效果
实现思路
采用单选按钮的状态来控制导航的展开和收缩,通过transform来实现变换,并添加过渡效果来美化展开和收缩。
单选按钮要去除默认效果,设置z-index使其在最上层盖住其他部分。
代码
html结构
<!--
* @Descripttion:
* @version:
* @Author: siebe
* @Date: 2022-07-16 00:04:01
* @LastEditors: siebe
* @LastEditTime: 2022-07-16 00:04:17
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>展开收缩的图标导航</title>
<script src="https://kit.fontawesome.com/d0ccd97f28.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="menu">
<input type="checkbox">
<button class="fa-solid fa-pen"></button>
<button class="fa-solid fa-arrows-rotate"></button>
<button class="fa-solid fa-trash-can"></button>
<button class="fa-solid fa-bars"></button>
</div>
</body>
</html>
css样式
body {
margin: 0;
padding: 0;
height: 100vh;
background-color: #c4c2c2;
display: flex;
justify-content: center;
align-items: center;
}
.menu {
position: relative;
}
input {
margin: 0;
padding: 0;
position: absolute;
top: 0px;
right: 5px;
z-index: 5;
-moz-appearance: none;
-webkit-appearance: none;
appearance: none;
display: block;
width: 40px;
height: 40px;
border-radius: 50%;
outline: 0;
cursor: pointer;
}
button {
height: 40px;
width: 40px;
margin-right: 5px;
padding: 0;
display: inline-block;
background-color: #E08E79;
border: 0;
outline: 0;
border-radius: 50%;
color: white;
text-align: center;
cursor: pointer;
will-change: transform;
transition: background-color 250ms cubic-bezier(0.41, 0.29, 0.38, 0.94), transform 250ms cubic-bezier(0.41, 0.29, 0.38, 0.94);
}
button:hover {
background-color: #d66b50;
}
button:active {
background-color: #c54d2e;
}
input:hover~.fa-bars {
background-color: #d66b50;
}
input~button:nth-child(4) {
transform: translateX(50px) rotate(60deg);
}
input~button:nth-child(3) {
transform: translateX(100px) rotate(90deg);
}
input~button:nth-child(2) {
transform: translateX(150px) rotate(180deg);
}
input[type="checkbox"]:checked~button {
transform: translateX(0);
}
gitee地址:siebe/html-css-demo (https://gitee.com/siebe/html-css-demo)
图标来源:图标库Font Awesome中的免费图标。
每天一个网页小实例,生活多姿又多彩。