效果演示
实现了一个圆形菜单的效果,点击菜单按钮后,菜单项会从菜单按钮中心点向外展开,并且菜单项上有文字链接。可以将这段代码的效果称为“圆形菜单展开效果”。
Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>圆形菜单</title>
<link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
<link rel="stylesheet" href="./圆形菜单.css">
</head>
<body>
<nav class="menu">
<input type="checkbox" class="menu-toggler" id="menu_toggler" checked>
<label for="menu_toggler"></label>
<ul>
<li class="menu-item">
<a href="#"><i class="fa fa-qq" aria-hidden="true"></i></a>
</li>
<li class="menu-item">
<a href="#"><i class="fa fa-weixin" aria-hidden="true"></i></a>
</li>
<li class="menu-item">
<a href="#"><i class="fa fa-weibo" aria-hidden="true"></i></a>
</li>
<li class="menu-item">
<a href="#"><i class="fa fa-tencent-weibo" aria-hidden="true"></i></a>
</li>
<li class="menu-item">
<a href="#"><i class="fa fa-telegram" aria-hidden="true"></i></a>
</li>
<li class="menu-item">
<a href="#"><i class="fa fa-renren" aria-hidden="true"></i></a>
</li>
</ul>
</nav>
</body>
</html>
* {
margin: 0;
padding: 0;
}
body {
background-color: black;
height: 100vh;
}
.menu-toggler {
position: absolute;
display: block;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
width: 40px;
height: 40px;
z-index: 2;
opacity: 0;
cursor: pointer;
}
.menu-toggler:hover+label,
.menu-toggler:hover+label::before,
.menu-toggler:hover+label::after {
background: #fff;
}
.menu-toggler:checked+label {
background: transparent;
}
.menu-toggler:checked+label::before,
.menu-toggler:checked+label::after {
top: 0;
width: 40px;
transform-origin: 50% 50%;
}
.menu-toggler:checked+label::before {
transform: rotate(45deg);
}
.menu-toggler:checked+label::after {
transform: rotate(-45deg);
}
.menu-toggler:checked~ul .menu-item {
opacity: 1;
}
.menu-toggler:checked~ul .menu-item:nth-child(1) {
transform: rotate(0deg) translateX(-110px);
}
.menu-toggler:checked~ul .menu-item:nth-child(2) {
transform: rotate(60deg) translateX(-110px);
}
.menu-toggler:checked~ul .menu-item:nth-child(3) {
transform: rotate(120deg) translateX(-110px);
}
.menu-toggler:checked~ul .menu-item:nth-child(4) {
transform: rotate(180deg) translateX(-110px);
}
.menu-toggler:checked~ul .menu-item:nth-child(5) {
transform: rotate(240deg) translateX(-110px);
}
.menu-toggler:checked~ul .menu-item:nth-child(6) {
transform: rotate(300deg) translateX(-110px);
}
.menu-toggler:checked~ul .menu-item a {
pointer-events: auto;
}
.menu-toggler+label {
width: 40px;
height: 5px;
display: block;
z-index: 1;
border-radius: 2.5px;
background: rgba(255, 255, 255, 0.7);
transition: transform 0.5s, top 0.5s;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
.menu-toggler+label::before,
.menu-toggler+label::after {
width: 40px;
height: 5px;
display: block;
z-index: 1;
border-radius: 2.5px;
background: rgba(255, 255, 255, 0.7);
transition: transform 0.5s, top 0.5s;
content: "";
position: absolute;
left: 0;
}
.menu-toggler+label::before {
top: 10px;
}
.menu-toggler+label::after {
top: -10px;
}
.menu-item:nth-child(1) a {
transform: rotate(0deg);
}
.menu-item:nth-child(2) a {
transform: rotate(-60deg);
}
.menu-item:nth-child(3) a {
transform: rotate(-120deg);
}
.menu-item:nth-child(4) a {
transform: rotate(-180deg);
}
.menu-item:nth-child(5) a {
transform: rotate(-240deg);
}
.menu-item:nth-child(6) a {
transform: rotate(-30deg);
}
.menu-item {
position: absolute;
display: block;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
width: 80px;
height: 80px;
opacity: 0;
transition: 0.5s;
}
.menu-item a {
display: block;
width: inherit;
height: inherit;
line-height: 80px;
color: rgba(255, 255, 255, 0.7);
background: rgba(255, 255, 255, 0.2);
border-radius: 50%;
text-align: center;
text-decoration: none;
font-size: 40px;
pointer-events: none;
transition: 0.2s;
}
.menu-item a:hover {
box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.3);
color: #fff;
background: rgba(255, 255, 255, 0.3);
font-size: 45px;
}
实现思路拆分
*{
margin: 0;
padding: 0;
}
这段代码是将所有元素的外边距和内边距都设置为0,以便后面的布局更加精确。
body{
background: linear-gradient(to right, #ed6ea0, #ec8c69);
height: 100vh;
}
这段代码设置了页面的背景颜色为从左到右的渐变色,高度为100vh,即占据整个视口的高度。
.menu-toggler{
position: absolute;
display: block;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
width: 40px;
height: 40px;
z-index: 2;
opacity: 0;
cursor: pointer;
}
这段代码定义了菜单按钮的样式,将其定位在页面中心,设置宽高为40px,透明度为0,鼠标指针为手型。
.menu-toggler:hover + label,
.menu-toggler:hover + label::before,
.menu-toggler:hover + label::after{
background: #fff;
}
这段代码设置了当鼠标悬停在菜单按钮上时,菜单按钮和其前后的两个伪元素的背景色变为白色。
.menu-toggler:checked + label{
background: transparent;
}
这段代码设置了当菜单按钮被选中时,菜单按钮的背景色变为透明。
.menu-toggler:checked + label::before,
.menu-toggler:checked + label::after{
top: 0;
width: 40px;
transform-origin: 50% 50%;
}
.menu-toggler:checked + label::before{
transform: rotate(45deg);
}
.menu-toggler:checked + label::after{
transform: rotate(-45deg);
}
这段代码设置了当菜单按钮被选中时,菜单按钮前后的两个伪元素会旋转45度,形成一个“X”形状。
.menu-toggler:checked ~ ul .menu-item{
opacity: 1;
}
.menu-toggler:checked ~ ul .menu-item:nth-child(1){
transform: rotate(0deg) translateX(-110px);
}
.menu-toggler:checked ~ ul .menu-item:nth-child(2){
transform: rotate(60deg) translateX(-110px);
}
.menu-toggler:checked ~ ul .menu-item:nth-child(3){
transform: rotate(120deg) translateX(-110px);
}
.menu-toggler:checked ~ ul .menu-item:nth-child(4){
transform: rotate(180deg) translateX(-110px);
}
.menu-toggler:checked ~ ul .menu-item:nth-child(5){
transform: rotate(240deg) translateX(-110px);
}
.menu-toggler:checked ~ ul .menu-item:nth-child(6){
transform: rotate(300deg) translateX(-110px);
}
这段代码设置了当菜单按钮被选中时,菜单项会从菜单按钮中心点向外展开,并且每个菜单项都有一个旋转角度和一个水平位移距离。
.menu-toggler:checked ~ ul .menu-item a{
pointer-events: auto;
}
这段代码设置了当菜单按钮被选中时,菜单项上的链接可以被点击。
.menu-toggler + label{
width: 40px;
height: 5px;
display: block;
z-index: 1;
border-radius: 2.5px;
background: rgba(255,255,255,0.7);
transition: transform 0.5s, top 0.5s;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
.menu-toggler + label::before,
.menu-toggler + label::after{
width: 40px;
height: 5px;
display: block;
z-index: 1;
border-radius: 2.5px;
background: rgba(255,255,255,0.7);
transition: transform 0.5s, top 0.5s;
content: "";
position: absolute;
left: 0;
}
.menu-toggler + label::before{
top: 10px;
}
.menu-toggler + label::after{
top: -10px;
}
这段代码设置了菜单按钮前后的两个伪元素的样式,形成一个横线和两个斜线的形状。
.menu-item:nth-child(1) a{
transform: rotate(0deg);
}
.menu-item:nth-child(2) a{
transform: rotate(-60deg);
}
.menu-item:nth-child(3) a{
transform: rotate(-120deg);
}
.menu-item:nth-child(4) a{
transform: rotate(-180deg);
}
.menu-item:nth-child(5) a{
transform: rotate(-240deg);
}
.menu-item:nth-child(6) a{
transform: rotate(-30deg);
}
这段代码设置了每个菜单项上链接的旋转角度,使其在菜单展开时呈现一个圆形排列的效果。
.menu-item{
position: absolute;
display: block;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
width: 80px;
height: 80px;
opacity: 0;
transition: 0.5s;
}
.menu-item a{
display: block;
width: inherit;
height: inherit;
line-height: 80px;
color: rgba(255,255,255,0.7);
background: rgba(255,255,255,0.2);
border-radius: 50%;
text-align: center;
text-decoration: none;
font-size: 40px;
pointer-events: none;
transition: 0.2s;
}
.menu-item a:hover{
box-shadow: 0 0 0 2px rgba(255,255,255,0.3);
color: #fff;
background: rgba(255,255,255,0.3);
font-size: 45px;
}
这段代码设置了菜单项的样式,包括宽高、透明度、过渡效果等,以及菜单项上链接的样式,包括颜色、背景色、边框半径等。同时,当鼠标悬停在链接上时,链接会有一个放大和阴影的效果。