jQuery构建现代可定制数字时钟

  • 源码大小:7.02KB
  • 所需积分:1积分
  • 源码编号:19JP-3759
  • 浏览次数:1173次
  • 最后更新:2023年07月19日
  • 所属栏目:时间和时钟
本站默认解压密码:19jp.com 或 19jp_com

简介

您的网站或应用程序需要数字时钟吗?在这篇博客文章中,我们将向您展示如何使用JavaScript创建自定义数字时钟,您可以根据自己的喜好进行配置。

我们将引导您完成代码,让您了解这一切是如何工作的,然后就如何使用新时钟给出一些想法。因此,无论您是在寻找一种快速简便的方法,将数字时钟纳入您的下一个项目,还是只想了解更多关于JavaScript编程的信息,请继续阅读!

如何使用它:

1.为数字时钟创建HTML。

  1. <section id="clockContainer" class="clockContainer-theme-day">
  2. <div id="clockTime" class="clockTime-theme-day">N/a</div>
  3. </section>

2.为时钟控制面板创建HTML,您可以在其中设置时间格式(12小时或24小时)、主题(亮、暗或自动)和面部方向(垂直或水平)。

  1. <button id="btnAdjustOpen" type="button" class="btn btn-link">
  2. <span class="glyphicon glyphicon-cog" aria-hidden="true"></span>
  3. Adjustments
  4. </button>
  5. <section id="adjustmentContainer" class="adjustment-theme-day">
  6. <button id="btnAdjustClose" type="button" class="btn btn-danger" style="float:right;">Close</button>
  7. <br><br>
  8. <h3>Time Format</h3>
  9. <div id="btnGroupTimeFormat" class="btn-group btn-group-toggle" data-toggle="buttons">
  10. <label class="btn btn-secondary active">
  11. <input type="radio" name="options" id="option1" autocomplete="off"> 12 Hour
  12. </label>
  13. <label class="btn btn-secondary">
  14. <input type="radio" name="options" id="option2" autocomplete="off"> 24 Hour
  15. </label>
  16. </div>
  17. <br><br>
  18. <h3>Theme</h3>
  19. <div id="btnGroupTheme" class="btn-group btn-group-toggle" data-toggle="buttons">
  20. <label class="btn btn-secondary active">
  21. <input type="radio" name="options" id="option1" autocomplete="off"> Day
  22. </label>
  23. <label class="btn btn-secondary">
  24. <input type="radio" name="options" id="option2" autocomplete="off"> Night
  25. </label>
  26. <label class="btn btn-secondary">
  27. <input type="radio" name="options" id="option3" autocomplete="off"> Auto
  28. </label>
  29. </div>
  30. <br><br>
  31. <h3>Face Orientation</h3>
  32. <div id="btnGroupOrientation" class="btn-group btn-group-toggle" data-toggle="buttons">
  33. <label class="btn btn-secondary active">
  34. <input type="radio" name="options" id="option1" autocomplete="off"> Horizontal
  35. </label>
  36. <label class="btn btn-secondary">
  37. <input type="radio" name="options" id="option2" autocomplete="off"> Verticle
  38. </label>
  39. </div>
  40. </section>

3.数字时钟的主要CSS样式。

  1. #clockContainer {
  2. width: fit-content;
  3. margin: 0 auto;
  4. margin-top: 8.5rem;
  5. padding: 3rem;
  6. border-radius: 80px;
  7. }
  8.  
  9. #clockTime {
  10. font-size: 12rem;
  11. font-weight: 900;
  12. font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
  13. }
  14.  
  15. /* The button to open the adjustment container */
  16. #btnAdjustOpen{
  17. position: fixed;
  18. top: 0;
  19. right: 0;
  20. margin: 16px;
  21. }
  22.  
  23. /* The adjustment container */
  24. #adjustmentContainer {
  25. display: inline-block;
  26. position: fixed;
  27. width: 18rem;
  28. height: 100vh;
  29. margin-right: -18rem;
  30. padding: 16px;
  31. right: 0;
  32. top: 0;
  33. color: #fff;
  34. }

4.白天/晚上主题的CSS样式。

  1. /* Day theme colors */
  2. .body-theme-day {
  3. background-color: rgb(240, 240, 240);
  4. }
  5.  
  6. .clockContainer-theme-day {
  7. box-shadow: 25px 25px 25px #FFFFFF,
  8. -25px -25px 25px #dbdbdb;
  9. }
  10.  
  11. .clockTime-theme-day {
  12. color: #000;
  13. }
  14.  
  15. .adjustment-theme-day {
  16. background-color: rgb(29, 29, 29);
  17. box-shadow: 0px -1rem 15px rgb(29, 29, 29);
  18. }
  19.  
  20. /* Night theme colors */
  21. .body-theme-night {
  22. background-color: rgb(29, 29, 29);
  23. }
  24.  
  25. .clockContainer-theme-night {
  26. box-shadow: 25px 25px 25px #0a0a0a,
  27. -25px -25px 25px #242424;
  28. }
  29.  
  30. .clockTime-theme-night {
  31. color: rgb(175, 175, 175);
  32. }
  33.  
  34. .adjustment-theme-night {
  35. background-color: rgb(240, 240, 240);
  36. }
  37.  
  38. .adjustment-theme-night > h3 {
  39. color: #000;
  40. }

5.通过将主题CSS类添加到body标记来设置初始主题。

  1. <body class="body-theme-day">
  2. ...
  3. </body>
  4.  
  5. <!-- OR -->
  6. <body class="body-theme-night">
  7. ...
  8. </body>

6.在文档中加载必要的jQuery库。

  1. <script src="/path/to/cdn/jquery.min.js"></script>

7.数字时钟的主要功能。

  1. // Provides a simple clock with built-in DOM manipulation
  2. // Set the time of the clock
  3. function updateClockTick() {
  4. var $clockComponent = $("#clockTime");
  5.  
  6. var mDate = new Date();
  7.  
  8. var hour = mDate.getHours();
  9. var minute = mDate.getMinutes();
  10. var second = mDate.getSeconds();
  11.  
  12. // Adjustments made by script.adjustment.js
  13. // By default it is military time
  14. //
  15. if (currentClockSetting == ADJUSTMENT_CLOCK_USE_12_HOUR) {
  16. if (hour > 12) {
  17. hour -= 12;
  18. }
  19. }
  20.  
  21. // Adjustments made by script.adjustment.js
  22. // apply a <br> to create a horitonztal or verticle clock face
  23. if (currentOrientationSetting == ADJUSTMENT_ORIENTATION_HORIZONTAL) {
  24. // Remove padding by clock when horizontal
  25. $clockComponent.parent().css("marginTop", "");
  26.  
  27. $clockComponent.text(zeroPad(hour) + ":" + zeroPad(minute));
  28. } else if (currentOrientationSetting == ADJUSTMENT_ORIENTATION_VERTICAL) {
  29. // Add padding by clock when vertical
  30. $clockComponent.parent().css("marginTop", "1.5rem");
  31.  
  32. $clockComponent.html(zeroPad(hour) + "<br>" + zeroPad(minute));
  33. }
  34. }
  35.  
  36. // Thanks for the slice idea https://stackoverflow.com/questions/18889548/javascript-change-gethours-to-2-digit
  37. // Add leading zeros to numbers,
  38. // returns a string
  39. function zeroPad(num) {
  40. return ("0" + num).slice(-2);
  41. }

8.以下脚本负责根据用户的喜好动态调整页面内容

  1. // This script is responsible for adjusting the page content dynamically
  2. // based on what the user prefered
  3. const ADJUSTMENT_CLOCK_USE_12_HOUR = 0;
  4. const ADJUSTMENT_CLOCK_USE_24_HOUR = 1;
  5. var currentClockSetting = 0;
  6.  
  7. const THEME_DAY = 0;
  8. const THEME_NIGHT = 1;
  9. const THEME_AUTO = 2;
  10.  
  11. var currentThemeSetting = 0;
  12. var watchTheme = window.matchMedia("(prefers-color-scheme: dark)");
  13.  
  14. const ADJUSTMENT_ORIENTATION_HORIZONTAL = 0;
  15. const ADJUSTMENT_ORIENTATION_VERTICAL = 1;
  16. var currentOrientationSetting = ADJUSTMENT_ORIENTATION_HORIZONTAL;
  17.  
  18. // Initiator of this script
  19. function pageIsReady() {
  20. // Buttons
  21. var $btnAdjust = $("#btnAdjustOpen");
  22. var $btnAdjustClose = $("#btnAdjustClose");
  23. var $adjustContainer = $("#adjustmentContainer");
  24.  
  25. $btnAdjust.click(function() {
  26. // Show the adjustment pane & hide this button
  27. $btnAdjust.hide();
  28. $adjustContainer.animate({
  29. right: "18rem"
  30. }, 200, "swing");
  31. });
  32.  
  33. $btnAdjustClose.click(function() {
  34. // Hide the adjustment pane & show this button
  35. $btnAdjust.show();
  36.  
  37. $adjustContainer.animate({
  38. right: 0
  39. }, 200, "swing");
  40.  
  41. });
  42. // Add click listeners to each radio input. And when clicked,
  43. // update the corresponding option with an int value based
  44. // on the button index. (Taking a look at the UI will help)
  45. var $inputGroupTime = $("#btnGroupTimeFormat > label > input");
  46. // For each option in the radio group
  47. $inputGroupTime.each(function(index, e) {
  48. // Add a click listener. Set the
  49. // adjustment value equal to
  50. // the index
  51. // TODO: Kind of makes the CONST variable useless
  52. // in a sort of way
  53. var thisElement = $(e);
  54.  
  55. $(e).click(function() {
  56. currentClockSetting = index;
  57.  
  58. // Remove the css highlight on any of the elements
  59. $inputGroupTime.parent().removeClass("active");
  60.  
  61. // Highlight this element
  62. thisElement.parent().addClass("active");
  63. });
  64. });
  65.  
  66. // Find the radio group
  67. var $inputGroupTheme = $("#btnGroupTheme > label > input");
  68. // For each option in the radio group
  69. $inputGroupTheme.each(function(index, e) {
  70. // Add a click listener. Set the
  71. // adjustment value equal to
  72. // the index
  73. // TODO: Kind of makes the CONST variable useless
  74. // in a sort of way
  75. var thisElement = $(e);
  76. $(e).click(function() {
  77.  
  78. if (currentThemeSetting != index) {
  79. currentThemeSetting = index;
  80.  
  81. // Remove the css highlight on any of the elements
  82. $inputGroupTheme.parent().removeClass("active");
  83. // Highlight this element
  84. thisElement.parent().addClass("active");
  85. // Updates the UI to a dark or light theme
  86. themeOptions(index);
  87. }
  88.  
  89. });
  90. });
  91.  
  92. // Find the radio group
  93. var $inputGroupOrientation = $("#btnGroupOrientation > label > input");
  94. // For each option in the radio group
  95. $inputGroupOrientation.each(function(index, e) {
  96. // Add a click listener. Set the
  97. // adjustment value equal to
  98. // the index
  99. // TODO: Kind of makes the CONST variable useless
  100. // in a sort of way
  101. var thisElement = $(e);
  102. $(e).click(function() {
  103. currentOrientationSetting = index;
  104. // Remove the css highlight on any of the elements
  105. $inputGroupOrientation.parent().removeClass("active");
  106. // Highlight this element
  107. thisElement.parent().addClass("active");
  108.  
  109. });
  110. });
  111.  
  112. }
  113.  
  114. function themeOptions(selectedIndex){
  115.  
  116. if (selectedIndex == THEME_DAY) {
  117. watchTheme.removeEventListener("change", windowThemeChanged);
  118. applyTheme("day");
  119. } else if (selectedIndex == THEME_NIGHT) {
  120. watchTheme.removeEventListener("change", windowThemeChanged);
  121. applyTheme("night");
  122. } else if (selectedIndex == THEME_AUTO) {
  123. // Register an event listener.
  124. // Call an event when the host system's theme changes
  125. windowThemeChanged(watchTheme);
  126. watchTheme.addEventListener("change", windowThemeChanged);
  127. }
  128.  
  129. }
  130.  
  131. // Called when the host system theme is changed
  132. function windowThemeChanged(e){
  133. const newTheme = e.matches ? "dark" : "light";
  134. if (newTheme == "dark") {
  135. applyTheme("night");
  136. } else if (newTheme == "light") {
  137. applyTheme("day");
  138. }
  139.  
  140. }
  141.  
  142. // Applies a "day" or "night" theme to the website
  143. // based on the input string
  144. function applyTheme(themeString){
  145. // Grab all the elements that require changing
  146. // remove the classes and then add the classes with the specified
  147. // "day" or "night" string
  148. $("body").removeClass().addClass("body-theme-" + themeString);
  149. $("#clockContainer").removeClass().addClass("clockContainer-theme-" + themeString);
  150. $("#clockTime").removeClass().addClass("clockTime-theme-" + themeString);
  151. $("#adjustmentContainer").removeClass().addClass("adjustment-theme-" + themeString);
  152. }

9.启用数字时钟。

  1. $(document).ready(function() {
  2.  
  3. // Once this script is loaded, call the other clock
  4. // script that the DOM can be manipulated
  5. pageIsReady();
  6.  
  7. // Create a repeatable ticker object which calls upon
  8. // other objects
  9. var interval = setInterval(function() {
  10. // Call tick function
  11. updateClockTick();
  12. }, 1000);
  13.  
  14. });

预览截图