使用HTML5制作一个刻度温度计

要使用HTML5制作一个刻度温度计,你可以结合HTML、CSS和JavaScript来实现。下面是一个简单的刻度温度计示例:

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>刻度温度计</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="thermometer">
        <div class="bulb"></div>
        <div class="tube">
            <div class="mercury" style="height: 50%;"></div> <!-- 初始高度为50%,可以根据需要调整 -->
            <div class="scale">
                <div class="tick" style="bottom: 0;">0°C</div> <!-- 可以根据需要添加更多刻度 -->
                <div class="tick" style="bottom: 20%;">20°C</div>
                <div class="tick" style="bottom: 40%;">40°C</div>
                <div class="tick" style="bottom: 60%;">60°C</div>
                <div class="tick" style="bottom: 80%;">80°C</div>
                <div class="tick" style="bottom: 100%;">100°C</div>
            </div>
        </div>
    </div>
    <script src="script.js"></script>
</body>
</html>

CSS (styles.css)

body {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background-color: #f4f4f4;
}

.thermometer {
    position: relative;
    width: 40px;
    height: 200px;
    background-color: transparent;
}

.bulb {
    position: absolute;
    bottom: 0;
    width: 60px;
    height: 60px;
    background-color: #ff6347; /* Tomato 红色 */
    border-radius: 50%;
    left: -10px; /* 使灯泡居中 */
}

.tube {
    position: absolute;
    width: 20px;
    height: 140px;
    background-color: #ddd;
    top: 20px; /* 与灯泡顶部对齐 */
    left: 10px; /* 使管子居中 */
    border-radius: 10px;
}

.mercury {
    position: absolute;
    bottom: 0;
    width: 100%;
    background-color: #ff6347; /* 与灯泡相同的颜色 */
    border-radius: 10px 10px 0 0; /* 只在顶部添加圆角 */
}

.scale {
    position: absolute;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
}

.tick {
    position: relative;
    text-align: center;
    transform: translateX(-50%); /* 使刻度标签居中 */
    left: 50%;
}

JavaScript (script.js) - 可选,用于动态控制水银高度

如果你想要动态地改变温度计中的水银高度,你可以添加一些JavaScript代码。例如:

const mercury = document.querySelector('.mercury');
const maxHeight = mercury.parentElement.offsetHeight; // 管子的高度

// 示例:设置水银高度为70%
setMercuryHeight(0.7); // 0.7 表示 70%

function setMercuryHeight(percentage) {
    mercury.style.height = `${Math.round(percentage * maxHeight)}px`;
}

在这个示例中,我创建了一个简单的HTML结构来表示温度计,包括一个灯泡(.bulb)、一个管子(.tube)和管子内的水银(.mercury)。CSS用于样式化这些元素,而JavaScript则用于动态控制水银的高度。你可以根据需要调整HTML、CSS和JavaScript代码来满足你的具体需求。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王铁柱666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值