X
返回列表 发新帖

一个仪表盘代码,自动获得数值后指针变化

[复制链接]
掌门 | 2026-4-3
咨询·自助·工单
创客秀 发表于 前天 15:00  | 显示全部楼层 | 阅读模式

马上注册,获取更多创业资源,认识更多朋友!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x

仿这个图片用html写的代码
yibiaopan.jpg
实际效果:
ybp.jpg
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>带外围数字的随机仪表盘</title>
  7.     <style>
  8.         /* 仪表盘整体容器(包含数值显示) */
  9.         .dashboard-wrapper {
  10.             position: relative;
  11.             width: 220px; /* 加宽适配外围数字 */
  12.             height: 120px;
  13.             margin: 50px auto;
  14.             text-align: center;
  15.         }

  16.         /* 仪表盘容器 */
  17.         .dashboard {
  18.             position: relative;
  19.             width: 200px;
  20.             height: 100px;
  21.             margin: 0 auto;
  22.             border-radius: 100px 100px 0 0;
  23.             background: linear-gradient(to right, #ff9500 0%, #ff5e00 100%);
  24.             overflow: hidden;
  25.         }

  26.         /* 刻度背景纹理 */
  27.         .dashboard::before {
  28.             content: '';
  29.             position: absolute;
  30.             top: 0;
  31.             left: 0;
  32.             width: 100%;
  33.             height: 100%;
  34.             background:
  35.                 repeating-linear-gradient(
  36.                     to bottom right,
  37.                     transparent 0px,
  38.                     transparent 2px,
  39.                     rgba(255, 255, 255, 0.15) 2px,
  40.                     rgba(255, 255, 255, 0.15) 4px
  41.                 );
  42.             border-radius: 100px 100px 0 0;
  43.             pointer-events: none;
  44.         }

  45.         /* 刻度线容器 */
  46.         .scale-container {
  47.             position: absolute;
  48.             bottom: 0;
  49.             left: 0;
  50.             width: 100%;
  51.             height: 100%;
  52.             border-radius: 100px 100px 0 0;
  53.             pointer-events: none;
  54.         }

  55.         /* 主刻度线(长刻度) */
  56.         .major-scale {
  57.             position: absolute;
  58.             bottom: 0;
  59.             left: 50%;
  60.             width: 2px;
  61.             height: 20px;
  62.             background: #fff;
  63.             transform-origin: bottom center;
  64.         }

  65.         /* 副刻度线(短刻度) */
  66.         .minor-scale {
  67.             position: absolute;
  68.             bottom: 0;
  69.             left: 50%;
  70.             width: 1px;
  71.             height: 10px;
  72.             background: rgba(255, 255, 255, 0.8);
  73.             transform-origin: bottom center;
  74.         }

  75.         /* 数字标记(调整到外围) */
  76.         .scale-number {
  77.             position: absolute;
  78.             color: #333; /* 改深色更清晰 */
  79.             font-size: 12px;
  80.             font-weight: bold;
  81.             transform: translateX(-50%);
  82.             /* 禁止文字选中 */
  83.             user-select: none;
  84.         }

  85.         /* 指针样式 */
  86.         .needle {
  87.             position: absolute;
  88.             bottom: 0;
  89.             left: 50%;
  90.             width: 4px;
  91.             height: 90px;
  92.             background: #333;
  93.             transform-origin: bottom center;
  94.             transform: translateX(-50%) rotate(-90deg);
  95.             transition: transform 0.8s ease; /* 放慢动画更自然 */
  96.             z-index: 10;
  97.         }

  98.         /* 指针头部小圆点 */
  99.         .needle::after {
  100.             content: '';
  101.             position: absolute;
  102.             bottom: -5px;
  103.             left: 50%;
  104.             width: 12px;
  105.             height: 12px;
  106.             background: #333;
  107.             border-radius: 50%;
  108.             transform: translateX(-50%);
  109.         }

  110.         /* 当前数值显示 */
  111.         .current-value {
  112.             position: absolute;
  113.             bottom: 10px;
  114.             left: 50%;
  115.             transform: translateX(-50%);
  116.             font-size: 18px;
  117.             font-weight: bold;
  118.             color: #333;
  119.             z-index: 15;
  120.         }
  121.     </style>
  122. </head>
  123. <body>
  124.     <!-- 仪表盘整体容器 -->
  125.     <div class="dashboard-wrapper">
  126.         <!-- 仪表盘主体 -->
  127.         <div class="dashboard">
  128.             <!-- 刻度线容器 -->
  129.             <div class="scale-container" id="scaleContainer"></div>
  130.             <!-- 指针 -->
  131.             <div class="needle" id="needle"></div>
  132.             <!-- 当前数值显示 -->
  133.             <div class="current-value" id="currentValue">0</div>
  134.         </div>
  135.     </div>

  136.     <script>
  137.         // 获取核心元素
  138.         const needle = document.getElementById('needle');
  139.         const scaleContainer = document.getElementById('scaleContainer');
  140.         const currentValue = document.getElementById('currentValue');
  141.         
  142.         // 初始化刻度和外围数字
  143.         function initScale() {
  144.             // 生成刻度:0-100,每10个单位一个主刻度(带数字),每2个单位一个副刻度
  145.             for (let value = 0; value <= 100; value += 2) {
  146.                 // 计算刻度旋转角度(和指针角度逻辑一致)
  147.                 const angle = (value / 100) * 180 - 90;
  148.                
  149.                 // 主刻度(每10个单位)
  150.                 if (value % 10 === 0) {
  151.                     // 创建主刻度线
  152.                     const majorScale = document.createElement('div');
  153.                     majorScale.className = 'major-scale';
  154.                     majorScale.style.transform = `translateX(-50%) rotate(${angle}deg)`;
  155.                     scaleContainer.appendChild(majorScale);

  156.                     // 计算外围数字位置(半径加大,移到仪表盘外)
  157.                     const radius = 110; // 大于仪表盘半径(100),显示在外部
  158.                     const rad = angle * Math.PI / 180;
  159.                     // 基于仪表盘中心(100,100)计算外围坐标
  160.                     const x = 100 + radius * Math.sin(rad);
  161.                     const y = 100 - radius * Math.cos(rad);

  162.                     // 创建外围数字标记
  163.                     const scaleNumber = document.createElement('div');
  164.                     scaleNumber.className = 'scale-number';
  165.                     scaleNumber.textContent = value;
  166.                     // 挂载到外层容器,避免被仪表盘裁剪
  167.                     scaleNumber.style.position = 'absolute';
  168.                     scaleNumber.style.left = `${x}px`;
  169.                     scaleNumber.style.top = `${y}px`;
  170.                     document.querySelector('.dashboard-wrapper').appendChild(scaleNumber);
  171.                 } else {
  172.                     // 创建副刻度线
  173.                     const minorScale = document.createElement('div');
  174.                     minorScale.className = 'minor-scale';
  175.                     minorScale.style.transform = `translateX(-50%) rotate(${angle}deg)`;
  176.                     scaleContainer.appendChild(minorScale);
  177.                 }
  178.             }
  179.         }

  180.         // 设置指针角度和数值显示
  181.         function setNeedle(randomValue) {
  182.             // 校验数值范围
  183.             const value = Math.max(0, Math.min(100, randomValue));
  184.             
  185.             // 计算旋转角度
  186.             const angle = (value / 100) * 180 - 90;
  187.             
  188.             // 更新指针样式
  189.             needle.style.transform = `translateX(-50%) rotate(${angle}deg)`;
  190.             // 更新当前数值显示
  191.             currentValue.textContent = value;
  192.         }

  193.         // 生成随机数值并更新仪表盘
  194.         function generateRandomValue() {
  195.             // 生成0-100之间的随机整数
  196.             const randomNum = Math.floor(Math.random() * 101);
  197.             setNeedle(randomNum);
  198.         }

  199.         // 初始化刻度 + 指针
  200.         initScale();
  201.         // 页面加载后先随机一次
  202.         generateRandomValue();
  203.         // 每隔3秒自动更新随机数值(可根据需要调整时间)
  204.         setInterval(generateRandomValue, 3000);
  205.     </script>
  206. </body>
  207. </html>
复制代码


关于我们
关于我们
友情链接
联系我们
帮助中心
网友中心
购买须知
支付方式
服务支持
解决方案
售后服务
定制流程
关注我们
官方微博
官方空间
官方微信
快速回复 返回顶部 返回列表