中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當前位置: 首頁 > news >正文

外國黃色網站今日預測足球比分預測

外國黃色網站,今日預測足球比分預測,網站開發(fā)怎么做,裝修設計平臺有哪些風尚云網前端學習:制作一款簡易的在線計算器 簡介 在前端開發(fā)的學習過程中,實現一個簡單的在線計算器是一個常見的練習項目。它不僅能夠幫助我們熟悉HTML、CSS和JavaScript的基本用法,還能夠加深我們對事件處理和DOM操作的理解。今天&#…

風尚云網前端學習:制作一款簡易的在線計算器

簡介

在前端開發(fā)的學習過程中,實現一個簡單的在線計算器是一個常見的練習項目。它不僅能夠幫助我們熟悉HTML、CSS和JavaScript的基本用法,還能夠加深我們對事件處理和DOM操作的理解。今天,我們將一起學習如何創(chuàng)建一款簡易的在線計算器,并探索其背后的代碼實現。在這里插入圖片描述

項目結構

我們的計算器項目包含三個主要部分:HTML結構、CSS樣式和JavaScript邏輯。

HTML結構

HTML部分負責構建計算器的界面。我們使用<div>元素來創(chuàng)建計算器的容器,<input>元素作為顯示屏,以及一系列<button>元素作為數字和運算符的按鈕。

<div class="calculator"><p>風尚云網前端學習:<br>一款簡易的在線計算器</p><input type="text" id="display" class="display" readonly><div class="button-grid"><!-- 按鈕 --></div>
</div>

CSS樣式

CSS部分負責計算器的樣式設計。我們使用了Flexbox布局來居中顯示計算器,并為按鈕和顯示屏添加了一些基本的樣式,使其看起來更加美觀。

body {font-family: 'Arial', sans-serif;background-color: #ececec;display: flex;justify-content: center;align-items: center;height: 100vh;margin: 0;
}.calculator {background-color: #ffffff;border-radius: 10px;box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);overflow: hidden;width: 240px;max-width: 100%;padding: 20px;
}.display {background-color: #f7f7f7;color: #333;font-size: 1.2em;padding: 15px;text-align: right;border: none;outline: none;width: 100%;box-sizing: border-box;margin-bottom: 15px;
}.button-grid {display: grid;grid-template-columns: repeat(4, 1fr);gap: 10px;
}.button {background-color: #e0e0e0;border: none;border-radius: 5px;color: #333;cursor: pointer;font-size: 1em;padding: 15px;text-align: center;transition: background-color 0.3s ease;
}/* 更多樣式... */

JavaScript邏輯

JavaScript部分負責計算器的邏輯處理。我們?yōu)槊總€按鈕添加了事件監(jiān)聽器,以便在點擊時執(zhí)行相應的操作。這包括數字輸入、運算符選擇、計算結果和清除操作。

const display = document.getElementById('display');
let currentInput = '';
let previousInput = '';
let operation = null;const buttons = document.querySelectorAll('.button');
buttons.forEach(button => {button.addEventListener('click', () => {const value = button.textContent;if (value === 'C') {currentInput = '';previousInput = '';operation = null;display.value = '';} else if (value === '=') {if (previousInput !== '' && operation !== null && currentInput !== '') {calculateResult();}} else if (['+', '-', '×', '÷'].includes(value)) {if (currentInput !== '') {if (operation) {calculateResult();}previousInput = parseFloat(currentInput);currentInput = '';operation = value;}} else if (value === '.') {if (!currentInput.includes('.')) {currentInput += value;}} else {currentInput += value;}display.value = currentInput || previousInput || '';});
});function calculateResult() {let result;const num1 = parseFloat(previousInput);const num2 = parseFloat(currentInput);switch (operation) {case '+':result = num1 + num2;break;case '-':result = num1 - num2;break;case '×':result = num1 * num2;break;case '÷':if (num2 === 0) {alert('除數不能為0');return;}result = num1 / num2;break;}display.value = result;currentInput = result.toString();previousInput = '';operation = null;
}

完整代碼

<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"> <!-- 設置字符編碼為UTF-8 --><meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- 使頁面在移動設備上正常顯示 --><title>基本計算器</title> <!-- 頁面標題 --><style>/* CSS樣式開始 */body {font-family: 'Arial', sans-serif; /* 設置字體 */background-color: #ececec; /* 設置背景顏色 */display: flex; /* 使用Flexbox布局 */justify-content: center; /* 水平居中 */align-items: center; /* 垂直居中 */height: 100vh; /* 高度設置為視口高度 */margin: 0; /* 移除默認邊距 */}.calculator {background-color: #ffffff; /* 計算器背景顏色 */border-radius: 10px; /* 邊框圓角 */box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); /* 陰影效果 */overflow: hidden; /* 隱藏溢出內容 */width: 240px; /* 計算器寬度 */max-width: 100%; /* 最大寬度為100% */padding: 20px; /* 內邊距 */}.display {background-color: #f7f7f7; /* 顯示屏背景顏色 */color: #333; /* 文字顏色 */font-size: 1.2em; /* 字體大小 */padding: 15px; /* 內邊距 */text-align: right; /* 文本右對齊 */border: none; /* 無邊框 */outline: none; /* 無輪廓 */width: 100%; /* 寬度100% */box-sizing: border-box; /* 盒模型 */margin-bottom: 15px; /* 外邊距 */}.button-grid {display: grid; /* 使用網格布局 */grid-template-columns: repeat(4, 1fr); /* 四列 */gap: 10px; /* 間隔 */}.button {background-color: #e0e0e0; /* 按鈕背景顏色 */border: none; /* 無邊框 */border-radius: 5px; /* 邊框圓角 */color: #333; /* 文字顏色 */cursor: pointer; /* 鼠標指針 */font-size: 1em; /* 字體大小 */padding: 15px; /* 內邊距 */text-align: center; /* 文本居中 */transition: background-color 0.3s ease; /* 背景色漸變 */}.button:hover {background-color: #d5d5d5; /* 鼠標懸停背景顏色 */}.button:active {background-color: #cccccc; /* 鼠標按下背景顏色 */}.operator {background-color: #000; /* 操作符按鈕背景顏色 */color: #fff; /* 文字顏色 */}.operator:hover {background-color: #f57c00; /* 鼠標懸停背景顏色 */}.operator:active {background-color: #ef6c00; /* 鼠標按下背景顏色 */}.equals {background-color: #000; /* 等號按鈕背景顏色 */color: #fff; /* 文字顏色 */}.equals:hover {background-color: #45a049; /* 鼠標懸停背景顏色 */}.equals:active {background-color: #3e8e41; /* 鼠標按下背景顏色 */}/* CSS樣式結束 */</style>
</head>
<body>
<div class="calculator"><!-- 標題和副標題 --><p>風尚云網前端學習:<br>一款簡易的在線計算器</p><!-- 顯示屏 --><input type="text" id="display" class="display" readonly><!-- 按鈕網格 --><div class="button-grid"><!-- 清除按鈕 --><button class="button clear">C</button><!-- 數字按鈕 --><button class="button number">1</button><button class="button number">2</button><button class="button number">3</button><button class="button number">4</button><button class="button number">5</button><button class="button number">6</button><button class="button number">7</button><button class="button number">8</button><button class="button number">9</button><button class="button number">0</button><!-- 小數點按鈕 --><button class="button decimal">.</button><!-- 操作符按鈕 --><button class="button operator">+</button><button class="button operator">-</button><button class="button operator">×</button><button class="button operator">÷</button><!-- 等號按鈕 --><button class="button equals">=</button></div>
</div><script>// 獲取顯示屏元素const display = document.getElementById('display');// 用于存儲當前輸入的數字let currentInput = '';// 用于存儲之前的數字let previousInput = '';// 用于存儲操作符let operation = null;// 獲取所有按鈕const buttons = document.querySelectorAll('.button');// 為每個按鈕添加點擊事件buttons.forEach(button => {button.addEventListener('click', () => {const value = button.textContent;// 處理清除按鈕if (value === 'C') {currentInput = '';previousInput = '';operation = null;display.value = '';} else if (value === '=') {// 處理等號按鈕,計算結果if (previousInput !== '' && operation !== null && currentInput !== '') {calculateResult();}} else if (['+', '-', '×', '÷'].includes(value)) {// 處理操作符按鈕if (currentInput !== '') {if (operation) {calculateResult();}previousInput = parseFloat(currentInput);currentInput = '';operation = value;}} else if (value === '.') {// 處理小數點按鈕if (!currentInput.includes('.')) {currentInput += value;}} else {// 處理數字按鈕currentInput += value;}// 更新顯示屏display.value = currentInput || previousInput || '';});});// 計算結果的函數function calculateResult() {let result;const num1 = parseFloat(previousInput);const num2 = parseFloat(currentInput);// 根據操作符計算結果switch (operation) {case '+':result = num1 + num2;break;case '-':result = num1 - num2;break;case '×':result = num1 * num2;break;case '÷':if (num2 === 0) {alert('除數不能為0');return;}result = num1 / num2;break;}// 顯示結果display.value = result;// 重置輸入currentInput = result.toString();previousInput = '';operation = null;}
</script></body>
</html>

結論

通過本教程,我們學習了如何創(chuàng)建一個簡易的在線計算器。這個項目不僅鍛煉了我們的前端技能,還幫助我們理解了事件驅動編程的基本概念。希望這個教程能夠幫助你更好地理解HTML、CSS和JavaScript的實際應用,并激發(fā)你創(chuàng)建自己的網頁項目。


注意:在實際部署時,請確保所有的資源文件(如CSS和JavaScript)都已正確鏈接,以便計算器能夠正常工作。如果你在實現過程中遇到任何問題,歡迎在評論區(qū)提問或尋求幫助。

我是風尚,夢想是帶十萬人創(chuàng)建一個風尚云網全能圈子!

http://www.risenshineclean.com/news/54478.html

相關文章:

  • 做爰全過程網站seo實戰(zhàn)技術培訓
  • 專做網站app拉新推廣賺傭金
  • 網頁游戲排行榜3d商丘網站優(yōu)化公司
  • 做動圖素材網站百度知道首頁登錄入口
  • 新服務器做網站高端婚戀網站排名
  • wordpress小程序調用seo關鍵詞排名優(yōu)化銷售
  • 靜態(tài)網站制作模板代寫文章
  • 用什么軟件做介紹視頻網站怎么樣推廣自己的網址
  • 自己怎么做獨立網站整站排名服務
  • 羅湖做網站多少錢域名搜索
  • 學做海報的網站朋友圈網絡營銷
  • 學歷網站怎么做seo搜索引擎優(yōu)化營銷案例
  • 同里做網站網絡推廣員好做嗎
  • 哪個網站可以做印章圖案鄭州網絡營銷排名
  • 中國建設教育網站職業(yè)技術培訓
  • 外貿網站建設公司如何網上營銷
  • 網站后臺管理系統(tǒng)開發(fā)快手作品推廣網站
  • 真題真做報名網站寧波seo推廣方式排名
  • 外貿手機網站模板全網關鍵詞云查詢
  • 東阿縣城市建設局網站seo優(yōu)化網
  • 德州做網站建設的公司哪家好網站是怎么做出來的
  • 基本網頁設計seo分析師招聘
  • 微網站微信數據庫設計創(chuàng)建網站
  • 天貓轉讓濟南seo培訓
  • 企業(yè)網站建設 論文百度智能建站系統(tǒng)
  • 云南網站設計公司關鍵詞歌詞打印
  • 中國營銷新聞網合肥百度快照優(yōu)化排名
  • 如何用手機做網站官網優(yōu)化 報價
  • MAC怎么做網站網站推廣優(yōu)化方法
  • 做網站用舊域名好不好最新全國疫情消息