使用css实现雷达效果
效果图
注意事项
- 扇形实现:将正方形移动到1/4圆处,利用overflow: hidden;将其切割成扇形
代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>雷达</title>
</head>
<body>
<div style="display: flex; justify-content: center; padding: 100px">
<div class="radar">
<div class="fan"></div>
</div>
</div>
<style>
/* 绘制雷达外圈 */
.radar {
/* 隐藏多余部分 */
overflow: hidden;
position: relative;
width: 300px;
height: 300px;
border-radius: 50%;
background: #fff;
border: 1px solid #1890ff;
/* 避免出现边框问题 */
box-sizing: border-box;
}
/* 绘制雷达中间十字线的竖线 */
.radar::before {
width: 150px;
height: 300px;
content: '';
display: block;
position: absolute;
left: 0;
top: 0;
box-sizing: border-box;
border-right: 1px solid #1890ff;
}
/* 绘制雷达中间十字线的横线 */
.radar::after {
width: 300px;
height: 150px;
content: '';
display: block;
box-sizing: border-box;
border-bottom: 1px solid #1890ff;
}
/* 绘制雷达内圈 */
.fan {
position: absolute;
top: 50%;
left: 50%;
/* 居中 */
transform: translate(-50%, -50%);
border-radius: 50%;
box-sizing: border-box;
border: 1px solid #1890ff;
width: 150px;
height: 150px;
}
/* 绘制雷达扫描的扇形 */
.fan::after {
content: '';
width: 150px;
height: 150px;
display: block;
box-sizing: border-box;
position: relative;
top: -50%;
right: -50%;
/* 移动旋转原点 */
transform-origin: 0% 100%;
border-bottom: 3px solid transparent;
border-image: linear-gradient(to right, transparent, #1890ff);
/* border-image-slice: 3; */
background: transparent;
background-image: linear-gradient(to right, transparent, #5ba9f3);
animation: rotateAnimate 2s linear infinite;
}
/* 动画 */
@keyframes rotateAnimate {
from {
transform: rotate(0deg);
/* transform: rotate(0deg) skew(-10deg); */
}
to {
transform: rotate(360deg);
/* transform: rotate(360deg) skew(-10deg); */
}
}
</style>
</body>
</html>