使用一个div实现5个圆环

本文介绍了如何仅使用CSS通过背景颜色、border属性、伪元素before和after,以及box-shadow,来实现一个div创建出五个圆环的效果。尽管在面试中未通过,但作者发现CSS的潜力,并鼓励大家探讨更多解决方案。

使用一个div实现5个圆环


面试的时候遇到了一个考察css的题,觉得挺有意思的,来记录一波:
题目:

怎么实现以下这张图片的效果?不许用图片,不许用svg。

在这里插入图片描述

我:(⊙o⊙)… 我当时的回答是可以div嵌套,emmmm…好像有点low啊,,,
面试官提示:能不能用一个div实现呢?
我:(⊙o⊙)…我只知道背景色和border可以设置颜色,那也只有两个环,,,
面试官:你可以想想盒模型中,哪些属性可以设置背景色的
我:(⊙o⊙)…border和margin
面试官:这俩可以设置颜色吗???你确定??
我:(⊙o⊙)…不能设置颜色,我再想想,,
面试官又提示:伪元素了解吗?
我:(⊙o⊙)…before和after吗?
面试官:对哦,他们是块级的还是什么?
我:块儿级的
面试官:那他们可以设置宽高吗?可以设置颜色吗?
我:(⊙o⊙)…好像可以
面试官:那你现在可以有几个环了
我:四个??
面试官:好吧,这个不纠结了,你可以再去研究一下。
我:凉。。。。。

行吧,,,今天来完成一下这个,废话不多说,直接上代码:

	div {
        width: 100px;
        height: 100px;
        background-color: brown;
        border: 10px solid gold;
        border-radius: 50%;
        box-shadow: 0px 0px 0px 10px;
        margin: 10px;
      }
      div::before {
        background-color: green;
        height: 50%;
        width: 50%;
        border-radius: 50%;
        display: block;
        content: '';
        position: relative;
        top: 25%;
        left: 25%;
      }
      div::after {
        background-color: sandybrown;
        height: 80%;
        width: 80%;
        border-radius: 50%;
        display: block;
        content: '';
        margin-top: -40%;
        margin-left: 10%;
      }

总结一下:

1. 背景颜色
2. border属性
3. after伪元素
4. before伪元素
5. box-shadow属性
6. 结合定位将他们的位置摆一摆就ok啦!

写完还是蛮有意思的,css也是一门大学问,虽然面试没有通过,有点小失落,但是还得接着学鸭!接着学!!!
以上内容只是自己琢磨出来的一种解决方法,欢迎大家讨论!

<think>我们使用jq(jQuery)来实现一个圆环进度效果。通常,圆环进度效果可以通过两个重叠的圆形来实现一个作为背景,另一个作为前景(表示进度)。前景圆环的弧度会随着进度值的变化而变化。实现思路:1.使用SVG或者CSS来绘制圆环。这里我们选择使用SVG,因为SVG在绘制图形方面更灵活和精确。2.我们将使用SVG的circle元素,并利用stroke-dasharray和stroke-dashoffset属性来实现圆环的进度效果。3.通过jQuery动态改变stroke-dashoffset的值来更新进度。步骤:1.创建两个circle,一个作为背景(灰色),另一个作为前景(彩色,表示进度)。2.计算圆环的周长(2*π*r),这个值将用于stroke-dasharray。3.根据进度百分比(0-100)计算stroke-dashoffset的值。公式为:offset=周长*(1-百分比/100)。注意:我们将使用viewBox来定义SVG的视口,使其自适应。代码结构:-一个容器div,包含一个SVG元素,SVG中有两个circle。-使用jQuery控制进度。我们将创建一个函数,用于更新进度,例如:updateProgress(percent)。下面是一个完整的示例:</think># 使用jQuery实现圆环进度效果 下面我将使用jQuery实现一个美观的圆环进度条效果。这个效果通过SVG绘制圆环,并使用jQuery动态控制进度变化。 ## 实现思路 1. 使用SVG绘制两个圆形:一个作为背景环,另一个作为进度环 2. 利用`stroke-dasharray`和`stroke-dashoffset`属性实现进度效果 3. 使用jQuery动态控制进度值并更新显示 4. 添加动画效果使进度变化更平滑 ## 完整代码实现 ```html <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>jQuery圆环进度条</title> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <style> body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; display: flex; flex-direction: column; align-items: center; justify-content: center; min-height: 100vh; background: linear-gradient(135deg, #1a2a6c, #b21f1f, #fdbb2d); margin: 0; padding: 20px; color: white; } .container { text-align: center; background: rgba(0, 0, 0, 0.4); padding: 30px; border-radius: 20px; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3); backdrop-filter: blur(10px); max-width: 500px; width: 100%; } h1 { margin-top: 0; font-size: 2.5rem; text-shadow: 0 2px 4px rgba(0,0,0,0.3); } .progress-container { position: relative; width: 250px; height: 250px; margin: 30px auto; } .progress-ring { transform: rotate(-90deg); transform-origin: 50% 50%; } .progress-ring__circle { transition: stroke-dashoffset 0.5s ease; fill: transparent; } .progress-background { stroke: rgba(255, 255, 255, 0.2); stroke-width: 12; } .progress-bar { stroke: url(#gradient); stroke-width: 12; stroke-linecap: round; } .progress-text { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-size: 3rem; font-weight: bold; color: white; text-shadow: 0 2px 10px rgba(0,0,0,0.5); } .progress-label { font-size: 1.2rem; margin-top: -10px; } .controls { display: flex; justify-content: center; gap: 15px; margin-top: 20px; flex-wrap: wrap; } button { background: rgba(255, 255, 255, 0.2); border: none; color: white; padding: 12px 25px; border-radius: 30px; font-size: 1rem; cursor: pointer; transition: all 0.3s ease; backdrop-filter: blur(5px); box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2); } button:hover { background: rgba(255, 255, 255, 0.3); transform: translateY(-3px); box-shadow: 0 6px 15px rgba(0, 0, 0, 0.3); } input[type="range"] { width: 100%; margin: 20px 0; background: transparent; -webkit-appearance: none; } input[type="range"]::-webkit-slider-runnable-track { height: 10px; background: rgba(255, 255, 255, 0.2); border-radius: 5px; } input[type="range"]::-webkit-slider-thumb { -webkit-appearance: none; width: 24px; height: 24px; border-radius: 50%; background: white; cursor: pointer; margin-top: -7px; box-shadow: 0 4px 8px rgba(0,0,0,0.3); } .explanation { margin-top: 30px; background: rgba(0, 0, 0, 0.3); padding: 20px; border-radius: 10px; text-align: left; } .explanation h3 { margin-top: 0; border-bottom: 1px solid rgba(255,255,255,0.2); padding-bottom: 10px; } .code-block { background: rgba(0, 0, 0, 0.5); padding: 15px; border-radius: 8px; font-family: monospace; overflow-x: auto; margin: 15px 0; font-size: 0.9rem; } .key-point { color: #fdbb2d; font-weight: bold; } </style> </head> <body> <div class="container"> <h1>jQuery圆环进度条</h1> <div class="progress-container"> <svg class="progress-ring" width="250" height="250" viewBox="0 0 250 250"> <defs> <linearGradient id="gradient" x1="0%" y1="0%" x2="100%" y2="0%"> <stop offset="0%" stop-color="#00c6ff" /> <stop offset="100%" stop-color="#0072ff" /> </linearGradient> </defs> <circle class="progress-ring__circle progress-background" cx="125" cy="125" r="100" /> <circle class="progress-ring__circle progress-bar" cx="125" cy="125" r="100" stroke-dasharray="628.3" stroke-dashoffset="628.3" /> </svg> <div class="progress-text">0%</div> <div class="progress-label">当前进度</div> </div> <div class="controls"> <button id="btn25">25%</button> <button id="btn50">50%</button> <button id="btn75">75%</button> <button id="btn100">100%</button> </div> <input type="range" id="progressSlider" min="0" max="100" value="0"> <div class="explanation"> <h3>实现原理</h3> <p>这个圆环进度条使用SVG和jQuery实现:</p> <div class="code-block"> // 计算圆周长 const circumference = 2 * Math.PI * 100; // 半径100 // 设置进度 function setProgress(percent) { const offset = circumference - (percent / 100) * circumference; $(".progress-bar").css("stroke-dashoffset", offset); $(".progress-text").text(percent + "%"); } </div> <p><span class="key-point">关键点:</span></p> <ul> <li>使用SVG的<code><circle></code>元素绘制圆环</li> <li>通过<code>stroke-dasharray</code>设置虚线模式(等于圆周长)</li> <li>通过<code>stroke-dashoffset</code>控制虚线的偏移量实现进度效果</li> <li>使用CSS过渡效果实现平滑动画</li> <li>jQuery用于处理用户交互和更新进度</li> </ul> </div> </div> <script> $(document).ready(function() { // 计算圆周长:2 * π * 半径 (半径=100) const circumference = 2 * Math.PI * 100; // 设置进度函数 function setProgress(percent) { // 计算偏移量:周长 - (百分比 * 周长) const offset = circumference - (percent / 100) * circumference; // 应用偏移量到进度环 $(".progress-bar").css("stroke-dashoffset", offset); // 更新文本显示 $(".progress-text").text(percent + "%"); } // 初始化进度为0% setProgress(0); // 按钮事件绑定 $("#btn25").click(function() { setProgress(25); }); $("#btn50").click(function() { setProgress(50); }); $("#btn75").click(function() { setProgress(75); }); $("#btn100").click(function() { setProgress(100); }); // 滑块事件绑定 $("#progressSlider").on("input", function() { const percent = $(this).val(); setProgress(percent); }); }); </script> </body> </html> ``` ## 代码解释 ### 核心原理 圆环进度条的核心原理是利用SVG的`stroke-dasharray`和`stroke-dashoffset`属性: 1. `stroke-dasharray`定义虚线模式(这里设置为圆周长,即628.3) 2. `stroke-dashoffset`控制虚线的起始偏移量 3. 通过动态改变`stroke-dashoffset`的值,实现进度效果 ### 关键代码段 ```javascript // 计算圆周长:2 * π * 半径 (半径=100) const circumference = 2 * Math.PI * 100; function setProgress(percent) { // 计算偏移量:周长 - (百分比 * 周长) const offset = circumference - (percent / 100) * circumference; // 应用偏移量到进度环 $(".progress-bar").css("stroke-dashoffset", offset); // 更新文本显示 $(".progress-text").text(percent + "%"); } ``` ### 实现细节 1. **SVG结构**:使用两个圆形元素,一个作为背景,另一个作为进度条 2. **渐变效果**:使用线性渐变使进度条颜色更丰富 3. **动画过渡**:通过CSS的`transition`属性实现平滑动画 4. **用户交互**:提供按钮和滑块控制进度 5. **响应式设计**:使用相对单位确保在不同设备上正常显示 ### 视觉设计 - 使用半透明背景和模糊效果创建现代玻璃态设计 - 添加阴影增强层次感 - 使用渐变色背景提升整体视觉效果 - 进度文本居中显示,带有阴影增强可读性 这个圆环进度条效果可以轻松集成到任何需要展示进度的网页应用中,如文件上传、任务完成度、加载状态等场景。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值