You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将加密货币价格文本放入彩色圆角盒子并添加静态£符号

你需要分别修改style.css和app.js两个文件即可实现全部需求,index.html无需调整:

1. 编写style.css样式代码

你已经在index.html中引入了该样式文件,直接写入以下内容:

/* 持仓价值盒子样式 */
#shib-worth {
  /* 盒子背景色符合要求 */
  background-color: #23272a;
  /* 圆角效果 */
  border-radius: 12px;
  /* 内边距控制盒子尺寸 */
  padding: 18px 40px;
  /* 盒子宽度自适应内容 */
  width: fit-content;
  /* 水平居中展示 */
  margin: 30px auto 0;
  /* 内部元素对齐 */
  display: flex;
  align-items: center;
  gap: 8px;
  /* 全局使用Sora字体 */
  font-family: 'Sora', sans-serif;
  font-size: 2.4rem;
  font-weight: 800;
}

/* 静态£符号用伪元素实现,无需修改html结构 */
#shib-worth::before {
  content: "£";
  /* 白色符合要求 */
  color: #fff;
}

2. 调整app.js代码

移除冗余的样式设置,保留核心数值更新和动态变色逻辑:

let stockPriceElement = document.getElementById('shib-worth');
let lastPrice = null;

ws.onmessage = (event) => {
    let stockObject = JSON.parse(event.data);
    let price = parseFloat(stockObject.p).toFixed(8);
    let shib_start_price = 0.00003442;
    let shib_balance = 7263219;
    let shib_start_worth = shib_start_price * shib_balance;
    let shib_worth_now = price * shib_balance;
    let convert_shib_worth_to_gbp = shib_worth_now / 100 * 73;
    stockPriceElement.innerText = parseFloat(convert_shib_worth_to_gbp).toFixed(2);
    // 动态设置数值颜色
    stockPriceElement.style.color = convert_shib_worth_to_gbp === shib_start_worth ? '#9e9e9e' : convert_shib_worth_to_gbp > shib_start_worth ? '#4caf50' : '#f44336';
    lastPrice = convert_shib_worth_to_gbp
};

效果说明

  • 自动生成白色Sora字体的£符号,和右侧数值垂直居中对齐
  • 盒子背景为要求的#23272a,圆角效果符合预期
  • 数值保留原有的动态变色逻辑,盒子自适应内容宽度且水平居中
  • 可根据需要调整样式中的border-radius、padding、font-size参数修改盒子大小、圆角弧度和字号

内容的提问来源于stack exchange,提问作者JC Smyth

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 13:45:11