📜  使用 HTML、CSS 和 Vanilla JavaScript 的响应式模拟时钟

📅  最后修改于: 2021-10-29 04:31:12             🧑  作者: Mango

在本文中,我们将创建一个模拟时钟。这主要基于 HTML、CSS 和 Vanilla JavaScript。

方法:

  1. 创建一个 HTML 文件,我们将在其中添加主 div,然后我们将添加 4 个 div 标签,分别表示小时、分钟、秒针和大头针。
  2. 创建一个 CSS 文件来为我们的网页设计样式并为不同的手分配不同的长度。
  3. 创建一个 JavaScript 文件,用于创建不同时钟指针旋转的简短逻辑。

时钟指针旋转的逻辑:

1. 时针

For Achieving 12hrs,
hour hand moves 360deg.

i.e.  12hrs   ⇢   360degs

so,    1hr    ⇢   30degs

and, 60mins   ⇢   30degs
    
so,    1min   ⇢   0.5degs
    
Total Rotation of hour hand:
    (30deg * hrs) + (0.5deg * mins)

2. 分针

For Achieving 60mins,
hour hand moves 360deg.

i.e.  60mins   ⇢   360degs

so,    1min    ⇢   6degs
    
Total Rotation of minute hand:
        6deg * mins

3. 二手

For Achieving 60secs,
hour hand moves 360deg.

i.e.  60secs   ⇢   360degs

so,    1sec    ⇢   6degs
    
Total Rotation of minute hand:
        6deg * secs

HTML代码:

HTML


    

    Analog Clock
    

    

    
        
        
        
        
    
            


Javascript
// Selecting all of the css classes on which
// we want to apply functionalities
const hr = document.querySelector('.hr')
const min = document.querySelector('.min')
const sec = document.querySelector('.sec')
  
// Setting up the period of working
setInterval(() => {
  
    // Extracting the current time 
    // from DATE() function
    let day = new Date()
    let hour = day.getHours()
    let minutes = day.getMinutes()
    let seconds = day.getSeconds()
  
    // Formula that is explained above for 
    // the rotation of different hands
    let hrrotation = (30 * hour) + (0.5 * minutes);
    let minrotation = 6 * minutes;
    let secrotation = 6 * seconds;
  
    hr.style.transform =
        `translate(-50%,-100%) rotate(${hrrotation}deg)`
    min.style.transform =
        `translate(-50%,-100%) rotate(${minrotation}deg)`
    sec.style.transform =
        `translate(-50%,-85%) rotate(${secrotation}deg)`
});


HTML


  

    

  

    
        
        
        
        
    
          


代码说明:

  • 首先,创建一个 HTML 文件 (index.html)。
  • 现在在创建我们的 HTML 文件之后,我们将使用 标签为我们的网页提供一个标题。它应该放在 <head> 部分内。</li> <li>然后我们将提供所有样式的 CSS 文件链接到我们的 HTML。这也位于 <head> 标记之间。</li> <li>来到我们 HTML 代码的正文部分。 <ul> <li>首先,创建一个主div作为时钟。</li> <li>在那个 div 中添加 4divs 一小时、一分钟、秒针和大头针。</li> <li>在我们的正文末尾添加 <script> 标签,它将 JS 文件与我们的 HTML 文件链接起来。</li> </ul> </li> </ul> <p> <strong>CSS 代码:</strong> </p> <div class="noIdeBtnDiv"> <div class="hcb_wrap"> <pre class="prism line-numbers lang-css" data-lang="CSS"><code class="replace">/* Restoring browser effects */ * {     margin: 0;     padding: 0;     box-sizing: border-box;     ; }    /* All of the same styling to the body */ body {     height: 100vh;     display: flex;     justify-content: center;     align-items: center;     background-color: #000;     background-image: linear-gradient(             70deg, black, white); }    /* Sizing, positioning of main      dial of the clock */ .clock {     width: 40vw;     height: 40vw;     background-image: linear-gradient(                 70deg, black, white);     background-size: cover;     box-shadow: 0 3em 5.8em;     border-radius: 50%;     position: relative; }    .hr, .min, .sec {     width: 1%;     position: absolute;     top: 50%;     left: 50%;     transform: translate(-50%, -100%);     transform-origin: bottom;     z-index: 2;     border-radius: 2em; }    .pin {     position: absolute;     top: 0;     left: 0;     right: 0;     bottom: 0;     width: 1em;     height: 1em;     background: rgb(38, 0, 255);     border: 2px solid #ffffff;     border-radius: 10em;     margin: auto;     z-index: 10; }    /* Different length of different hands of clock */ .hr {     height: 25%;     background-color: #ff0000; }    .min {     height: 30%;     background-color: #ff9900; }    .sec {     height: 40%;     background-color: #99ff00;     transform-origin: 50% 85%; } </code></pre> </div> </div> <p><strong>代码说明:</strong> CSS 用于为我们的 HTML 页面提供不同类型的动画和效果,使其看起来对所有用户都是交互的。在CSS中,我们必须包括以下几点:</p> <ol> <li>恢复所有浏览器效果。</li> <li>使用类和 id 为 HTML 元素赋予效果。</li> </ol> <p> <strong>JS代码:</strong></p> <div class="noIdeBtnDiv"> <div class="responsive-tabs"> <h2 class="tabtitle"> Javascript </h2> <div class="tabcontent"> <div class="hcb_wrap"> <pre class="prism line-numbers lang-css" data-lang="CSS"><code class="replace">// Selecting all of the css classes on which // we want to apply functionalities const hr = document.querySelector('.hr') const min = document.querySelector('.min') const sec = document.querySelector('.sec')    // Setting up the period of working setInterval(() => {        // Extracting the current time      // from DATE() function     let day = new Date()     let hour = day.getHours()     let minutes = day.getMinutes()     let seconds = day.getSeconds()        // Formula that is explained above for      // the rotation of different hands     let hrrotation = (30 * hour) + (0.5 * minutes);     let minrotation = 6 * minutes;     let secrotation = 6 * seconds;        hr.style.transform =         `translate(-50%,-100%) rotate(${hrrotation}deg)`     min.style.transform =         `translate(-50%,-100%) rotate(${minrotation}deg)`     sec.style.transform =         `translate(-50%,-85%) rotate(${secrotation}deg)` }); </code></pre> </div> </div> </div> </div> <p><strong>代码说明:</strong></p> <ul> <li> setInterval()函数用于在特定时间段内执行函数。欲了解更多详情,请点击此处。</li> <li> Date()函数用于返回今天日期、当前时间(小时、分钟、秒)。</li> </ul> <p><strong>完整代码:</strong></p> <div class="responsive-tabs"> <h2 class="tabtitle"> HTML </h2> <div class="tabcontent"> <div class="hcb_wrap"> <pre class="prism line-numbers lang-css" data-lang="CSS"><code class="replace"><!DOCTYPE html> <html lang="en">    <head>     <style>            /* Restoring browser effects */                 * {             margin: 0;             padding: 0;             box-sizing: border-box;         }            /* All of the same styling to the body */                 body {             height: 100vh;             display: flex;             justify-content: center;             align-items: center;             background-color: #000;             background-image: linear-gradient(                 70deg, black, white);         }            /* Sizing, positioning of main dial of the clock */                 .clock {             width: 40vw;             height: 40vw;             background-image: linear-gradient(                 70deg, black, white);             background-size: cover;             box-shadow: 0 3em 5.8em;             border-radius: 50%;             position: relative;         }                    .hr,         .min,         .sec {             width: 1%;             position: absolute;             top: 50%;             left: 50%;             transform: translate(-50%, -100%);             transform-origin: bottom;             z-index: 2;             border-radius: 2em;         }                    .pin {             position: absolute;             top: 0;             left: 0;             right: 0;             bottom: 0;             width: 1em;             height: 1em;             background: rgb(38, 0, 255);             border: 2px solid #ffffff;             border-radius: 10em;             margin: auto;             z-index: 10;         }            /* Different length of different hands of clock */                 .hr {             height: 25%;             background-color: #ff0000;         }                    .min {             height: 30%;             background-color: #ff9900;         }                    .sec {             height: 40%;             background-color: #99ff00;             transform-origin: 50% 85%;         }     </style> </head>    <body>     <div class="clock">         <div class="hr"></div>         <div class="min"></div>         <div class="sec"></div>         <div class="pin"></div>     </div>        <script>            // Selecting all of the css classes          // on which we want to apply functionalities         const hr = document.querySelector('.hr')         const min = document.querySelector('.min')         const sec = document.querySelector('.sec')            // Setting up the period of working         setInterval(() => {                // Extracting the current time              // from DATE() function             let day = new Date()             let hour = day.getHours()             let minutes = day.getMinutes()             let seconds = day.getSeconds()                // Formula that is explained above for              // the rotation of different hands             let hrrotation = (30 * hour) + (0.5 * minutes);             let minrotation = 6 * minutes;             let secrotation = 6 * seconds;                hr.style.transform =                 `translate(-50%,-100%) rotate(${hrrotation}deg)`             min.style.transform =                 `translate(-50%,-100%) rotate(${minrotation}deg)`             sec.style.transform =                 `translate(-50%,-85%) rotate(${secrotation}deg)`         });     </script> </body>    </html> </code></pre> </div> </div> </div> <p><strong>输出:</strong> </p> <p><img class="img-fluid" src="https://mangodoc.oss-cn-beijing.aliyuncs.com/geek8geeks/Responsive_analog_clock_using_HTML,_CSS_and_Vanilla_JavaScript_0.jpg" width="600"/></p> <div class="_ap_apex_ad" id="82d2079a-8120-480f-9fc7-5cda825d56e7" style="min-height:280px"></div> <p></p></div> </article> </div> </div> </div> </div> </div> <footer> <div class="bg-white text-center pb-1"> <p class="text-body-tertiary pt-3 lh-lg text-opacity-50" id="footer-text">Copyright © 2020 - 2024 版权所有 <br> <a href="https://beian.miit.gov.cn/" target="_blank" class="text-opacity-50 text-body-tertiary mt-1 mb-1">蜀ICP备20006366号-1</a> <br> Made with ❤️ in Chengdu </p> </div> </footer> <!-- 引入Bootstrap JavaScript库 --> <script src="https://unpkg.com/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script> <!-- 引入Meilisearch搜索相关的JavaScript库 --> <script src="https://cdn.jsdelivr.net/npm/@meilisearch/instant-meilisearch/dist/instant-meilisearch.umd.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/instantsearch.js@4"></script> <script src="https://imangodoc.com/static/javascript/meili_custom.js"></script> <!-- 引入prismjs代码高亮相关的JavaScript库 --> <script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/components/prism-core.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/toolbar/prism-toolbar.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/autoloader/prism-autoloader.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/copy-to-clipboard/prism-copy-to-clipboard.min.js"></script> </body> </html>