Ever find yourself counting people or objects just for fun? I certainly did as a child, whether it was the number of cars passing by or how many people were in a room. This simple habit sparked the idea behind my project: The People Counter.
The primary goal of creating The People Counter was to dive into JavaScript, understand its syntax, and get comfortable with its flow. While I named it “The People Counter,” the concept is versatile and can be adapted to any type of counter—be it a car counter, house counter, toffee counter, or even a star counter. It’s fundamentally a counter app that helps in grasping the basics of JavaScript programming.
This project was built using resources from the Scrimba learning platform, which provided valuable insights and guidance throughout the development process.
Click to view the app in action!
The People Counter is designed to provide an easy, effective way to track and manage counts, all while showcasing the power of HTML, CSS, and JavaScript.
Real-Time Counting
Keep track of your count with a simple click of the "Increment" button. No more manual tallying!
This feature updates the displayed count instantly each time you click the button.
Save and View Entries
Log your counts and view a history of previous entries. Perfect for keeping track of multiple counts over time.
优雅且响应灵敏的设计
该应用程序可无缝适应各种屏幕尺寸,无论您是在台式机还是移动设备上,都能确保干净、现代的界面。
应用程序的设计在所有设备上看起来都很棒,增强了用户体验。
HTML:应用程序的主干,提供基本结构。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="index.css"> <link href="https://fonts.googleapis.com/css2?family=Playfair+Display:wght@400;700&family=Lato:wght@300;400;700&display=swap" rel="stylesheet"> <title>The People Counter</title> </head> <body> <div class="app-container"> <header> <h1>The People Counter</h1> </header> <main class="counter-container"> <div class="counter-display"> <div class="counter-frame"> <div id="count-el">0</div> </div> </div> <div class="controls"> <button id="increment-btn" onclick="increment()"> <span>Increment</span> </button> <button id="save-btn" onclick="save()"> <span>Save</span> </button> </div> <div class="entries"> <h2>Previous Entries</h2> <div id="save-el" class="entry-list"></div> </div> </main> </div> <script src="index.js"></script> </body> </html>
CSS
对于应用程序的样式,您可以使用 CSS 使其具有视觉吸引力和响应能力。 (由于本节更多地关注 JavaScript,因此我将在这里跳过详细的 CSS。)
JavaScript
通过动态功能为应用程序带来交互性。
let count = 0 let countEl = document.getElementById("count-el") let saveEl = document.getElementById ("save-el") function increment() { count += 1 countEl.textContent = count } function save() { let countStr = count + " - " saveEl.textContent += countStr countEl.textContent = 0 count = 0 }
说明:
变量声明:
自增函数:
保存函数:
增加计数:
点击“递增”按钮,计数加1。显示的数字会实时更新。
保存计数:
单击“保存”按钮记录当前计数。计数将被添加到先前条目的列表中,并且显示将重置为 0。
查看以前的条目:
保存的计数将显示在“以前的条目”部分下方,您可以在其中查看您的计数历史记录。
构建人数统计器是一次富有洞察力的体验,尤其是在学习了 Scrimba 教程之后。它强化了 HTML、CSS 和 JavaScript 中的关键概念,并演示了如何创建功能齐全、响应式的 Web 应用程序。
人数统计器证明了简单的想法如何可以通过一些编码知识演变成实用的工具。无论您是跟踪人、物体,还是只是享受数字带来的乐趣,这个应用程序都为古老的习惯提供了现代解决方案。
以上是构建'人流量统计器”:从童年计数到现代网站的旅程的详细内容。更多信息请关注PHP中文网其他相关文章!