Snippets: Aspect Radio Calculator
CSS Before Head
HTML Head
:root { --primary-color: #4CAF50; --secondary-color: #f8f9fa; --error-color: #dc3545; --text-color: #333; --border-radius: 10px; --box-shadow: 0 2px 15px rgba(0,0,0,0.1); } * { box-sizing: border-box; margin: 0; padding: 0; } body { font-family: 'Segoe UI', sans-serif; background-color: #f5f5f5; color: var(--text-color); min-height: 100vh; display: flex; justify-content: center; align-items: flex-start; padding: 20px; } .container { background: white; padding: 25px; border-radius: var(--border-radius); box-shadow: var(--box-shadow); width: 100%; max-width: 800px; transition: all 0.3s ease; } .input-group { margin-bottom: 25px; padding: 20px; border-radius: var(--border-radius); background-color: rgba(248, 249, 250, 0.5); transition: all 0.3s ease; } h2 { margin-bottom: 20px; text-align: center; color: var(--primary-color); } h3 { margin-bottom: 15px; color: var(--primary-color); border-bottom: 1px solid #eee; padding-bottom: 10px; } label { display: block; margin-bottom: 8px; font-weight: 500; } input { width: 100%; padding: 12px; border: 1px solid #ddd; border-radius: 6px; margin-bottom: 15px; font-size: 16px; transition: border-color 0.3s ease; } input:focus { outline: none; border-color: var(--primary-color); box-shadow: 0 0 0 2px rgba(76, 175, 80, 0.2); } button { background-color: var(--primary-color); color: white; padding: 12px 25px; border: none; border-radius: 6px; cursor: pointer; font-size: 16px; font-weight: 500; margin: 10px 0; transition: all 0.3s ease; width: 100%; } button:hover { background-color: #45a049; transform: translateY(-2px); box-shadow: 0 4px 10px rgba(0,0,0,0.1); } button:active { transform: translateY(0); } .result { margin-top: 15px; padding: 15px; background-color: var(--secondary-color); border-radius: 6px; min-height: 60px; line-height: 1.6; font-size: 16px; } .error { color: var(--error-color); font-size: 14px; margin-top: 8px; min-height: 20px; } /* 响应式设计 */ @media (min-width: 600px) { button { width: auto; } } @media (prefers-color-scheme: dark) { body { background-color: #202124; color: #e8eaed; } .container { background-color: #2d2e30; box-shadow: 0 2px 15px rgba(0,0,0,0.3); } .input-group { background-color: rgba(45, 46, 48, 0.7); } input { background-color: #3c4043; color: #e8eaed; border-color: #5f6368; } input:focus { border-color: var(--primary-color); box-shadow: 0 0 0 2px rgba(76, 175, 80, 0.3); } .result { background-color: #3c4043; } }
CSS After Head
JS Before HTML Body
<div class="container"> <h2>长宽比计算器</h2> <div class="input-group"> <h3>1. 计算初始长宽比</h3> <label>长度:</label> <input type="number" step="any" id="length" placeholder="请输入长度"> <label>宽度:</label> <input type="number" step="any" id="width" placeholder="请输入宽度"> <button onclick="calculateAspectRatio()">计算长宽比</button> <div class="result" id="aspectResult"></div> <div class="error" id="initialError"></div> </div> <div class="input-group"> <h3>2. 根据长宽比计算新尺寸</h3> <label>新长度(留空则计算宽度):</label> <input type="number" step="any" id="newLength" placeholder="可选输入新长度"> <label>新宽度(留空则计算长度):</label> <input type="number" step="any" id="newWidth" placeholder="可选输入新宽度"> <button onclick="calculateNewDimension()">计算新尺寸</button> <div class="result" id="newDimensionResult"></div> <div class="error" id="newError"></div> </div> </div>
HTML Body
HTML Foot
let aspectRatio = null; // 存储计算得到的长宽比([x, y]) // 计算最大公约数 function gcd(a, b) { while (b !== 0) { let temp = b; b = a % b; a = temp; } return a; } // 计算长宽比 function calculateAspectRatio() { const length = parseFloat(document.getElementById('length').value); const width = parseFloat(document.getElementById('width').value); const errorEl = document.getElementById('initialError'); const resultEl = document.getElementById('aspectResult'); // 输入验证 if (isNaN(length) || isNaN(width)) { errorEl.textContent = "请输入有效的数字"; resultEl.textContent = ""; aspectRatio = null; return; } if (length <= 0 || width <= 0) { errorEl.textContent = "长度和宽度必须大于0"; resultEl.textContent = ""; aspectRatio = null; return; } errorEl.textContent = ""; // 计算最简比值 const divisor = gcd(length, width); const ratioX = (length / divisor).toFixed(2); const ratioY = (width / divisor).toFixed(2); // 处理浮点精度问题(例如16:9可能计算为16.00:9.00) const cleanX = ratioX.endsWith('.00') ? ratioX.split('.')[0] : ratioX; const cleanY = ratioY.endsWith('.00') ? ratioY.split('.')[0] : ratioY; aspectRatio = [length / width, cleanX, cleanY]; // 存储原始比例和简化后的比值 resultEl.innerHTML = `长宽比为:${cleanX} : ${cleanY}<br>(原始比例:${length.toFixed(2)} : ${width.toFixed(2)})`; } // 计算新尺寸 function calculateNewDimension() { const newLength = parseFloat(document.getElementById('newLength').value); const newWidth = parseFloat(document.getElementById('newWidth').value); const errorEl = document.getElementById('newError'); const resultEl = document.getElementById('newDimensionResult'); // 验证基础条件 if (!aspectRatio) { errorEl.textContent = "请先计算初始长宽比"; resultEl.textContent = ""; return; } if (isNaN(newLength) && isNaN(newWidth)) { errorEl.textContent = "请至少输入一个新长度或新宽度"; resultEl.textContent = ""; return; } if (!isNaN(newLength) && newLength <= 0 || !isNaN(newWidth) && newWidth <= 0) { errorEl.textContent = "输入的尺寸必须大于0"; resultEl.textContent = ""; return; } errorEl.textContent = ""; const [ratio] = aspectRatio; // 原始比例(长/宽) let calculatedValue; if (!isNaN(newLength)) { // 已知新长度,计算宽度 = 长度 / 比例 calculatedValue = newLength / ratio; resultEl.innerHTML = `当长度为 ${newLength.toFixed(2)} 时,<br>对应的宽度应为:${calculatedValue.toFixed(2)}<br>(长宽比保持 ${aspectRatio[1]} : ${aspectRatio[2]})`; } else { // 已知新宽度,计算长度 = 宽度 * 比例 calculatedValue = newWidth * ratio; resultEl.innerHTML = `当宽度为 ${newWidth.toFixed(2)} 时,<br>对应的长度应为:${calculatedValue.toFixed(2)}<br>(长宽比保持 ${aspectRatio[1]} : ${aspectRatio[2]})`; } }
JS After HTML Body
Full HTML Code