美文网首页Node.js
路径-时钟案例

路径-时钟案例

作者: 马佳乐 | 来源:发表于2022-07-05 11:56 被阅读0次
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>index首页</title>
      <style>
        html,
        body {
          margin: 0;
          padding: 0;
          height: 100%;
          background-image: linear-gradient(to bottom right, red, gold);
        }
    
        .box {
          width: 400px;
          height: 250px;
          background-color: rgba(255, 255, 255, 0.6);
          border-radius: 6px;
          position: absolute;
          left: 50%;
          top: 40%;
          transform: translate(-50%, -50%);
          box-shadow: 1px 1px 10px #fff;
          text-shadow: 0px 1px 30px white;
    
          display: flex;
          justify-content: space-around;
          align-items: center;
          font-size: 70px;
          user-select: none;
          padding: 0 20px;
    
           /* 盒子投影 */     
          -webkit-box-reflect: below 0px -webkit-gradient(linear, left top, left bottom, from(transparent), color-stop(0%, transparent), to(rgba(250, 250, 250, .2)));
        }
      
      </style>
    </head>
    
    <body>
      <div class="box">
        <div id="HH">00</div>
        <div>:</div>
        <div id="mm">00</div>
        <div>:</div>
        <div id="ss">00</div>
      </div>
    
      <script>
    
        window.onload = function () {
          // 定时器,每隔 1 秒执行 1 次
          setInterval(() => {
            var dt = new Date()
            var HH = dt.getHours()
            var mm = dt.getMinutes()
            var ss = dt.getSeconds()
    
            // 为页面上的元素赋值
            document.querySelector('#HH').innerHTML = padZero(HH)
            document.querySelector('#mm').innerHTML = padZero(mm)
            document.querySelector('#ss').innerHTML = padZero(ss)
          }, 1000)
        }
    
        // 补零函数
        function padZero(n) {
          return n > 9 ? n : '0' + n
        }
      </script>
    </body>
    
    </html>
    

    用js代码将上面代码分为.html 、.css 、.js三个文件

    // 1.1 导入 fs 模块
    const fs = require('fs')
    // 1.2 导入 path 模块
    const path = require('path')
    
    // 1.3 定义正则表达式,分别匹配 <style></style> 和 <script></script> 标签
    const regStyle = /<style>[\s\S]*<\/style>/
    const regScript = /<script>[\s\S]*<\/script>/
    
    // 2.1 调用 fs.readFile() 方法读取文件
    fs.readFile(path.join(__dirname, '../素材/index.html'), 'utf8', function(err, dataStr) {
      // 2.2 读取 HTML 文件失败
      if (err) return console.log('读取HTML文件失败!' + err.message)
      // 2.3 读取文件成功后,调用对应的三个方法,分别拆解出 css, js, html 文件
      resolveCSS(dataStr)
      resolveJS(dataStr)
      resolveHTML(dataStr)
    })
    
    // 3.1 定义处理 css 样式的方法
    function resolveCSS(htmlStr) {
      // 3.2 使用正则提取需要的内容
      const r1 = regStyle.exec(htmlStr)
      // 3.3 将提取出来的样式字符串,进行字符串的 replace 替换操作
      const newCSS = r1[0].replace('<style>', '').replace('</style>', '')
      // 3.4 调用 fs.writeFile() 方法,将提取的样式,写入到 clock 目录中 index.css 的文件里面
      fs.writeFile(path.join(__dirname, './clock/index.css'), newCSS, function(err) {
        if (err) return console.log('写入 CSS 样式失败!' + err.message)
        console.log('写入样式文件成功!')
      })
    }
    
    // 4.1 定义处理 js 脚本的方法
    function resolveJS(htmlStr) {
      // 4.2 通过正则,提取对应的 <script></script> 标签内容
      const r2 = regScript.exec(htmlStr)
      // 4.3 将提取出来的内容,做进一步的处理
      const newJS = r2[0].replace('<script>', '').replace('</script>', '')
      // 4.4 将处理的结果,写入到 clock 目录中的 index.js 文件里面
      fs.writeFile(path.join(__dirname, './clock/index.js'), newJS, function(err) {
        if (err) return console.log('写入 JavaScript 脚本失败!' + err.message)
        console.log('写入 JS 脚本成功!')
      })
    }
    
    // 5.1 定义处理 HTML 结构的方法
    function resolveHTML(htmlStr) {
      // 5.2 将字符串调用 replace 方法,把内嵌的 style 和 script 标签,替换为外联的 link 和 script 标签
      const newHTML = htmlStr.replace(regStyle, '<link rel="stylesheet" href="./index.css" />').replace(regScript, '<script src="./index.js"></script>')
      // 5.3 写入 index.html 这个文件
      fs.writeFile(path.join(__dirname, './clock/index.html'), newHTML, function(err) {
        if (err) return console.log('写入 HTML 文件失败!' + err.message)
        console.log('写入 HTML 页面成功!')
      })
    }
    

    在终端中运行 node .\js文件名即可

    相关文章

      网友评论

        本文标题:路径-时钟案例

        本文链接:https://www.haomeiwen.com/subject/rilsbrtx.html