• JS应用案例:时钟,国庆倒计时


    时钟案例:

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <title>title>
    6. <style>
    7. div {
    8. width: 400px;
    9. height: 100px;
    10. background-color: #ccc;
    11. border: 1px solid #f00;
    12. margin: 130px auto;
    13. text-align: center;
    14. line-height: 100px;
    15. font-size: 24px;
    16. }
    17. style>
    18. head>
    19. <body>
    20. <div>div>
    21. body>
    22. <script>
    23. function dateTime() {
    24. //获取当前系统时间,并且拼接成“xxxx年xx月xx日 xx:xx:xx”的形式
    25. var date = new Date();
    26. console.log(date);
    27. //获取时分秒,如果时分秒是一位数字,给它拼接成两位数字
    28. var hour = date.getHours();
    29. if(hour<10){
    30. hour = '0'+hour;
    31. }
    32. var minutes = date.getMinutes();
    33. if(minutes<10){
    34. minutes ='0'+minutes;
    35. }
    36. var seconds =date.getSeconds();
    37. if(seconds<10){
    38. seconds = '0'+seconds;
    39. }
    40. var timeStr = date.getFullYear() + "年" + (date.getMonth() + 1) + "月" + date.getDate() + "日 " + hour +
    41. ":" + minutes + ":" + seconds;
    42. //获取div元素,并将timeStr存储到div元素中
    43. var divEle = document.querySelector('div');
    44. divEle.innerHTML = timeStr;
    45. }
    46. //注意:setInterval(要调用的函数,间隔的毫秒是)里第一个参数如果不是直接定义的函数,而是调用其它的函数,只需要写函数名称,不要带()
    47. dateTime();
    48. setInterval(dateTime, 1000);
    49. script>
    50. html>

     

     

     

     

    国庆倒计时案例:

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <title>title>
    6. <style>
    7. div{
    8. width: 600px;
    9. height: 200px;
    10. border: 1px solid black;
    11. margin: 150px auto;
    12. text-align: center;
    13. line-height: 200px;
    14. font-size: 24px;
    15. background-image: url(img/guoqin.jpg);
    16. }
    17. style>
    18. head>
    19. <body>
    20. <div> div>
    21. body>
    22. <script>
    23. function datetime(){
    24. var dateFur = new Date('2022','9','1');
    25. var fs = dateFur.getTime();
    26. console.log(dateFur);
    27. console.log(fs);
    28. var dateNow = new Date();
    29. var ns = dateNow.getTime();
    30. console.log(dateNow);
    31. console.log(ns);
    32. var time = fs - ns;
    33. console.log(time);
    34. days = parseInt(time/(24*60*60*1000));
    35. if(days<10){
    36. days = '0'+days;
    37. }
    38. hours = parseInt((time%(24*60*60*1000))/(60*60*1000));
    39. // console.log(hours);
    40. if(hours<10){
    41. hours = '0'+hours;
    42. }
    43. minutes = parseInt(((time%(24*60*60*1000))%(60*60*1000))/(60*1000));
    44. // console.log(minutes);
    45. if(minutes<10){
    46. minutes = '0'+minutes;
    47. }
    48. seconds = parseInt((((time%(24*60*60*1000))%(60*60*1000))%(60*1000))/1000);
    49. // console.log(seconds);
    50. if(seconds<10){
    51. seconds = '0'+seconds;
    52. }
    53. var timestr = "十一倒计时:"+days+"天"+hours+"时"+minutes+"分"+seconds+"秒";
    54. // console.log(timestr);
    55. var divEle = document.querySelector('div');
    56. divEle.innerHTML = timestr;
    57. }
    58. datetime();
    59. setInterval(datetime,1000);
    60. script>
    61. html>

     

     

  • 相关阅读:
    LeetCode 2678. 老人的数目【数组】简单
    【Android安全】Kotlin基础
    开发模式-敏捷开发
    Linux安装Nginx
    10、IOC 之类路径扫描和托管组件
    CentOS7下搭建Hadoop(3.3.4)集群
    jbase打印完善
    百度小程序模板制作_百度小程序模板平台
    拆分整数为2的幂次项和 → 理解多重背包问题二进制优化的核心思想
    C和C++的区别(3) const增强
  • 原文地址:https://blog.csdn.net/qq_51810428/article/details/126953568