限时优惠活动
亲爱的苦力吧用户,我们为了回馈新老用户一直以来的鼎力支持,即日起(2025-02-06至2025-03-06)凡是购买苦力吧VIP/充值K币的新老用户,都直接可获得买一送一的优惠馈赠。例如:购买一个月的VIP会员可直接获得两个月的VIP会员;充值100K币可直接获得200K币,以此类推!有任何疑问可联系在线客服,感谢各位用户对苦力吧素材的信任与厚爱,我们将一如既往的给大家上新更多优质的素材源码,祝大家开工大吉、工作顺利、心想事成。

原生js实现的秒针平滑扫动的模拟时钟特效

所属分类: 网页特效-日期时间    2024-12-26 03:12:30

原生js实现的秒针平滑扫动的模拟时钟特效 ie兼容6
 查看演示  登录后下载 温馨提示
登录会员即可享受免费下载
 我要建站

原生js实现的秒针平滑扫动的模拟时钟特效(共3个文件)

    • index.html

使用方法

  • code
  • source
  1. // Setters
  2. const setTimeString = timeUnit => {
  3. // String representations of time numbers
  4. const timeStrings = {
  5. 0: "",
  6. 1: "1",
  7. 2: "2",
  8. 3: "3",
  9. 4: "4",
  10. 5: "5",
  11. 6: "6",
  12. 7: "7",
  13. 8: "8",
  14. 9: "9",
  15. 10: "10",
  16. 11: "11",
  17. 12: "12",
  18. 13: "13",
  19. 14: "14",
  20. 15: "15",
  21. 16: "16",
  22. 17: "17",
  23. 18: "18",
  24. 19: "19",
  25. 20: "20",
  26. 30: "21",
  27. 40: "22",
  28. 50: "23",
  29. 60: "24" };
  30. if (timeUnit < 20) {
  31. return timeStrings[timeUnit];
  32. } else {
  33. const digitOne = timeStrings[Math.floor(timeUnit / 10) * 10];
  34. const digitTwo = timeStrings[timeUnit % 10] ?
  35. timeStrings[timeUnit % 10] :
  36. 0;
  37. if (digitTwo !== 0) {
  38. return `${digitOne} ${digitTwo}`;
  39. } else {
  40. return digitOne;
  41. }
  42. }
  43. };
  44. (function () {
  45. // Getters
  46. const getDomElements = () => {
  47. const hourDisplay = document.getElementById("hour-display");
  48. const minuteDisplay = document.getElementById("minute-display");
  49. const dayDisplay = document.getElementById("day-display");
  50. const monthDisplay = document.getElementById("month-display");
  51. const dateDisplay = document.getElementById("date-display");
  52. const hourHand = document.getElementById("hour-hand");
  53. const minuteHand = document.getElementById("minute-hand");
  54. const secondHand = document.getElementById("second-hand");
  55. return {
  56. hourDisplay,
  57. minuteDisplay,
  58. dayDisplay,
  59. monthDisplay,
  60. dateDisplay,
  61. hourHand,
  62. minuteHand,
  63. secondHand };
  64. };
  65. const getCurrentTime = () => {
  66. const date = new Date();
  67. const time = date.getTime();
  68. const hours = date.getHours();
  69. const minutes = date.getMinutes();
  70. const seconds = date.getSeconds();
  71. const milliseconds = date.getMilliseconds();
  72. return {
  73. date,
  74. time,
  75. // Adjust hours to 12 hour clock
  76. hours: hours > 12 ? hours - 12 : hours,
  77. minutes,
  78. seconds,
  79. milliseconds };
  80. };
  81. const setDateDisplay = currentTime => {
  82. const nowDateString = currentTime.toDateString();
  83. const dayString = nowDateString.slice(0, 3);
  84. const monthString = nowDateString.slice(4, 7);
  85. const dateString = nowDateString.slice(8, 10);
  86. return {
  87. dayString,
  88. monthString,
  89. dateString };
  90. };
  91. // Rendering
  92. const rotateHand = (timeUnit, factor, hand) => {
  93. // -90 degress accomodates for initial css layout position
  94. const position = timeUnit * factor - 90;
  95. hand.style.transform = `rotate(${position}deg)`;
  96. };
  97. const renderClock = () => {
  98. const domElements = getDomElements();
  99. const currentTime = getCurrentTime();
  100. // Hand values for animation
  101. // Seconds, minutes, hours reflect 100ms setInterval() iteration
  102. const seconds =
  103. (currentTime.seconds * 1000 + currentTime.milliseconds) / 1000;
  104. const minutes = (currentTime.minutes * 60 + seconds) / 60;
  105. const hours = (currentTime.hours * 60 + minutes) / 60;
  106. // Display strings for long-form readout
  107. const hourString = setTimeString(currentTime.hours);
  108. const minuteString = setTimeString(currentTime.minutes);
  109. let { dayString, monthString, dateString } = setDateDisplay(
  110. currentTime.date);
  111. // Populate DOM Elements
  112. domElements.hourDisplay.innerHTML = hourString;
  113. domElements.minuteDisplay.innerHTML = minuteString;
  114. domElements.dayDisplay.innerHTML = `${dayString} | `;
  115. domElements.monthDisplay.innerHTML = `${monthString} | `;
  116. domElements.dateDisplay.innerHTML = dateString;
  117. // Rotate Hands
  118. rotateHand(seconds, 6, domElements.secondHand);
  119. rotateHand(minutes, 6, domElements.minuteHand);
  120. rotateHand(hours, 30, domElements.hourHand);
  121. };
  122. const run = () => {
  123. setInterval(() => {
  124. renderClock();
  125. }, 100);
  126. };
  127. run();
  128. })();
// Setters
const setTimeString = timeUnit => {
  // String representations of time numbers
  const timeStrings = {
    0: "",
    1: "1",
    2: "2",
    3: "3",
    4: "4",
    5: "5",
    6: "6",
    7: "7",
    8: "8",
    9: "9",
    10: "10",
    11: "11",
    12: "12",
    13: "13",
    14: "14",
    15: "15",
    16: "16",
    17: "17",
    18: "18",
    19: "19",
    20: "20",
    30: "21",
    40: "22",
    50: "23",
    60: "24" };


  if (timeUnit < 20) {
    return timeStrings[timeUnit];
  } else {
    const digitOne = timeStrings[Math.floor(timeUnit / 10) * 10];
    const digitTwo = timeStrings[timeUnit % 10] ?
    timeStrings[timeUnit % 10] :
    0;
    if (digitTwo !== 0) {
      return `${digitOne} ${digitTwo}`;
    } else {
      return digitOne;
    }
  }
};

(function () {
  // Getters
  const getDomElements = () => {
    const hourDisplay = document.getElementById("hour-display");
    const minuteDisplay = document.getElementById("minute-display");
    const dayDisplay = document.getElementById("day-display");
    const monthDisplay = document.getElementById("month-display");
    const dateDisplay = document.getElementById("date-display");
    const hourHand = document.getElementById("hour-hand");
    const minuteHand = document.getElementById("minute-hand");
    const secondHand = document.getElementById("second-hand");

    return {
      hourDisplay,
      minuteDisplay,
      dayDisplay,
      monthDisplay,
      dateDisplay,
      hourHand,
      minuteHand,
      secondHand };

  };

  const getCurrentTime = () => {
    const date = new Date();
    const time = date.getTime();
    const hours = date.getHours();
    const minutes = date.getMinutes();
    const seconds = date.getSeconds();
    const milliseconds = date.getMilliseconds();

    return {
      date,
      time,
      // Adjust hours to 12 hour clock
      hours: hours > 12 ? hours - 12 : hours,
      minutes,
      seconds,
      milliseconds };

  };

  const setDateDisplay = currentTime => {
    const nowDateString = currentTime.toDateString();
    const dayString = nowDateString.slice(0, 3);
    const monthString = nowDateString.slice(4, 7);
    const dateString = nowDateString.slice(8, 10);

    return {
      dayString,
      monthString,
      dateString };

  };

  // Rendering
  const rotateHand = (timeUnit, factor, hand) => {
    // -90 degress accomodates for initial css layout position
    const position = timeUnit * factor - 90;
    hand.style.transform = `rotate(${position}deg)`;
  };

  const renderClock = () => {
    const domElements = getDomElements();
    const currentTime = getCurrentTime();
    // Hand values for animation
    // Seconds, minutes, hours reflect 100ms setInterval() iteration
    const seconds =
    (currentTime.seconds * 1000 + currentTime.milliseconds) / 1000;
    const minutes = (currentTime.minutes * 60 + seconds) / 60;
    const hours = (currentTime.hours * 60 + minutes) / 60;

    // Display strings for long-form readout
    const hourString = setTimeString(currentTime.hours);
    const minuteString = setTimeString(currentTime.minutes);
    let { dayString, monthString, dateString } = setDateDisplay(
    currentTime.date);


    // Populate DOM Elements
    domElements.hourDisplay.innerHTML = hourString;
    domElements.minuteDisplay.innerHTML = minuteString;
    domElements.dayDisplay.innerHTML = `${dayString} | `;
    domElements.monthDisplay.innerHTML = `${monthString} | `;
    domElements.dateDisplay.innerHTML = dateString;

    // Rotate Hands
    rotateHand(seconds, 6, domElements.secondHand);
    rotateHand(minutes, 6, domElements.minuteHand);
    rotateHand(hours, 30, domElements.hourHand);
  };

  const run = () => {
    setInterval(() => {
      renderClock();
    }, 100);
  };

  run();
})();

站长提示:
1. 苦力吧素材官方QQ群:950875342
2. 平台上所有素材资源,需注册登录会员方能正常下载。
3. 会员用户积极反馈网站、素材资源BUG或错误问题,每次奖励2K币
4. PHP源码类素材,如需协助安装调试,或你有二次开发需求,可联系苦力吧客服。
5. 付费素材资源,需充值后方能下载,如有任何疑问可直接联系苦力吧客服
相关资源 / 日期时间

jquery bootstrap制作带时间的日期选择插件代码

一款响应式日期选择弹出层,带时间选择:YYYY-MM-DD a hh:mm,可自定义格式。
  日期时间
 1382  0

纯CSS可自定义添加的在线闹钟UI界面

该界面允许设置指定日期和时间的多个闹钟事件,这是一个手机APP事件提醒页面模板。
  日期时间
 4618  0

jquery带日程安排事件提醒的日历组件代码

除了基础的日历功能外,还支持自定义添加日程安排内容,非常实用!
  日期时间
 5433  0

javascript支持连续选择多个日期的日历插件

一款多日期选择插件,可按住键盘shift按键,点选日期,可连续选中多个不同日期。
  日期时间
 9207  0

评论数(0) 回复有机会获得K币 用户协议

^_^ 还没有人评论,快来抢个沙发!
😀
  • 😀
  • 😊
  • 😂
  • 😍
  • 😑
  • 😷
  • 😵
  • 😛
  • 😣
  • 😱
  • 😋
  • 😎
  • 😵
  • 😕
  • 😶
  • 😚
  • 😜
  • 😭
发表评论