title: hexo添加页面加载动画
author: HaoQi
top: false
cover: false
toc: true
mathjax: false
reprintPolicy: cc_by
tags:

  • hexo
  • 加载动画

date: 2022-04-13 14:17:38
coverlmg:
img:
keywords: hexo,加载动画,页面
summary: 为hexo博客添加页面加载动画
password:
categories: 博客篇


修改配置文件

在hexo博客主题文件_config.yml中添加

  1. # 开启页面加载动画
  2. preloader:
  3. enable: true

打开主题目录中 /layout/_partial /head.ejs文件,将 以下内容填入<head> 标签中。

  1. <link rel="stylesheet" type="text/css" href="<%- theme.jsDelivr.url %><%- url_for('/css/loading.css') %>">

打开主题目录 中/layout/layout.ejs文件,将 以下内容填入<body> 标签中。

  1. <%- partial('_widget/loading') %>

添加文件

在主题目录/layout/_widget下添加loading.ejs文件

  1. <% if (theme.preloader.enable) { %>
  2. <div id="loading-box">
  3. <div class="loading-left-bg"></div>
  4. <div class="loading-right-bg"></div>
  5. <div class="spinner-box">
  6. <div class="configure-border-1">
  7. <div class="configure-core"></div>
  8. </div>
  9. <div class="configure-border-2">
  10. <div class="configure-core"></div>
  11. </div>
  12. <div class="loading-word">加载中...</div>
  13. </div>
  14. </div>
  15. <script>
  16. window.addEventListener('load', function(){
  17. document.body.style.overflow = 'auto';
  18. document.getElementById('loading-box').classList.add("loaded")
  19. }, false)
  20. </script>
  21. <% } %>

在主题目录/source/css下添加loading.css文件

  1. #loading-box .loading-left-bg,
  2. #loading-box .loading-right-bg {
  3. position: fixed;
  4. z-index: 1000;
  5. width: 50%;
  6. height: 100%;
  7. background-color: #37474f;
  8. transition: all 0.5s;
  9. }
  10. #loading-box .loading-right-bg {
  11. right: 0;
  12. }
  13. #loading-box > .spinner-box {
  14. position: fixed;
  15. z-index: 1001;
  16. display: flex;
  17. justify-content: center;
  18. align-items: center;
  19. width: 100%;
  20. height: 100vh;
  21. }
  22. #loading-box .spinner-box .configure-border-1 {
  23. position: absolute;
  24. padding: 3px;
  25. width: 115px;
  26. height: 115px;
  27. background: #ffab91;
  28. animation: configure-clockwise 3s ease-in-out 0s infinite alternate;
  29. }
  30. #loading-box .spinner-box .configure-border-2 {
  31. left: -115px;
  32. padding: 3px;
  33. width: 115px;
  34. height: 115px;
  35. background: rgb(63, 249, 220);
  36. transform: rotate(45deg);
  37. animation: configure-xclockwise 3s ease-in-out 0s infinite alternate;
  38. }
  39. #loading-box .spinner-box .loading-word {
  40. position: absolute;
  41. color: #ffffff;
  42. font-size: 0.8rem;
  43. }
  44. #loading-box .spinner-box .configure-core {
  45. width: 100%;
  46. height: 100%;
  47. background-color: #37474f;
  48. }
  49. div.loaded div.loading-left-bg {
  50. transform: translate(-100%, 0);
  51. }
  52. div.loaded div.loading-right-bg {
  53. transform: translate(100%, 0);
  54. }
  55. div.loaded div.spinner-box {
  56. display: none !important;
  57. }
  58. @keyframes configure-clockwise {
  59. 0% {
  60. transform: rotate(0);
  61. }
  62. 25% {
  63. transform: rotate(90deg);
  64. }
  65. 50% {
  66. transform: rotate(180deg);
  67. }
  68. 75% {
  69. transform: rotate(270deg);
  70. }
  71. 100% {
  72. transform: rotate(360deg);
  73. }
  74. }
  75. @keyframes configure-xclockwise {
  76. 0% {
  77. transform: rotate(45deg);
  78. }
  79. 25% {
  80. transform: rotate(-45deg);
  81. }
  82. 50% {
  83. transform: rotate(-135deg);
  84. }
  85. 75% {
  86. transform: rotate(-225deg);
  87. }
  88. 100% {
  89. transform: rotate(-315deg);
  90. }
  91. }

部署

  1. hexo c
  2. hexo g
  3. hexo d

参考链接:https://www.jianshu.com/p/a81a629e2f9a