Hexo Fluid背景固定以及添加加载动画

背景固定

参考了清山的博客

  1. 添加 background.js 文件
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    document
    .querySelector('#web_bg')
    .setAttribute('style', `background-image: ${document.querySelector('.banner').style.background.split(' ')[0]};position: fixed;width: 100%;height: 100%;z-index: -1;background-size: cover;`);

    document
    .querySelector("#banner")
    .setAttribute('style', 'background-image: url()')

    document
    .querySelector("#banner .mask")
    .setAttribute('style', 'background-color:rgba(0,0,0,0)')
  2. hexo 中注入html
    Fluid主题下由两种方式,一种是 Hexo 注入, 另一种方式是 Fluid 提供的注入方式, 注入方式是在博客文件根目录下新建一个 scripts 目录,在其中建立一个 js 文件, 详细可见 Fluid官方文档
    1
    2
    hexo.extend.injector.register("body_begin", `<div id="web_bg"></div>`);
    hexo.extend.injector.register("body_end",`<script src="${siteRoot}js/backgroundize.js"></script>`);

添加加载动画

我选择的加载动画是在 codepen.io 中找的彩虹动画
当然,此处直接拿到的 htmlcss 都是不能直接使用的,还需要对样式进行处理,完整的代码如下:

  • html 之上添加一个容器 loader-container 以包裹全部的样式:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    <div id="loader-container"> 
    <div id="loader" class="loader"></div>
    <div class="loader-inner">
    <div class="loader-line-wrap">
    <div class="loader-line"></div>
    </div>
    <div class="loader-line-wrap">
    <div class="loader-line"></div>
    </div>
    <div class="loader-line-wrap">
    <div class="loader-line"></div>
    </div>
    <div class="loader-line-wrap">
    <div class="loader-line"></div>
    </div>
    <div class="loader-line-wrap">
    <div class="loader-line"></div>
    </div>
    </div>
    </div>
  • css 中添加 loader-container 的样式,并确保所有动画均在最上层:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    #loader-container {
    position: fixed; /* 或 absolute,根据需求 */
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 99999; /* 确保在最上层 */
    display: flex;
    justify-content: center;
    align-items: center;
    background: rgba(0, 0, 0, 0.7); /* 可选:添加背景以增加可见性 */
    }

    .loader,
    .loader-inner,
    .loader-line-wrap,
    .loader-line {
    position: absolute; /* 或 fixed,根据需要选择 */
    z-index: 99999; /* 确保在最上层 */
    }

    .loader {
    background: #000;
    background: radial-gradient(#222, #000);
    bottom: 0;
    left: 0;
    overflow: hidden;
    position: fixed;
    right: 0;
    top: 0;
    z-index: 99999;
    }

    .loader-inner {
    bottom: 0;
    height: 60px;
    left: 0;
    margin: auto;
    position: absolute;
    right: 0;
    top: 0;
    width: 100px;
    }

    .loader-line-wrap {
    animation:
    spin 2000ms cubic-bezier(.175, .885, .32, 1.275) infinite
    ;
    box-sizing: border-box;
    height: 50px;
    left: 0;
    overflow: hidden;
    position: absolute;
    top: 0;
    transform-origin: 50% 100%;
    width: 100px;
    }
    .loader-line {
    border: 4px solid transparent;
    border-radius: 100%;
    box-sizing: border-box;
    height: 100px;
    left: 0;
    margin: 0 auto;
    position: absolute;
    right: 0;
    top: 0;
    width: 100px;
    }
    .loader-line-wrap:nth-child(1) { animation-delay: -50ms; }
    .loader-line-wrap:nth-child(2) { animation-delay: -100ms; }
    .loader-line-wrap:nth-child(3) { animation-delay: -150ms; }
    .loader-line-wrap:nth-child(4) { animation-delay: -200ms; }
    .loader-line-wrap:nth-child(5) { animation-delay: -250ms; }

    .loader-line-wrap:nth-child(1) .loader-line {
    border-color: hsl(0, 80%, 60%);
    height: 90px;
    width: 90px;
    top: 7px;
    }
    .loader-line-wrap:nth-child(2) .loader-line {
    border-color: hsl(60, 80%, 60%);
    height: 76px;
    width: 76px;
    top: 14px;
    }
    .loader-line-wrap:nth-child(3) .loader-line {
    border-color: hsl(120, 80%, 60%);
    height: 62px;
    width: 62px;
    top: 21px;
    }
    .loader-line-wrap:nth-child(4) .loader-line {
    border-color: hsl(180, 80%, 60%);
    height: 48px;
    width: 48px;
    top: 28px;
    }
    .loader-line-wrap:nth-child(5) .loader-line {
    border-color: hsl(240, 80%, 60%);
    height: 34px;
    width: 34px;
    top: 35px;
    }

    @keyframes spin {
    0%, 15% {
    transform: rotate(0);
    }
    100% {
    transform: rotate(360deg);
    }
    }

将加载动画的 html 文件和 css 文件在本地建立对应的文件并引入
html 以注入方式加载到 Fluid 中:

1
2
3
4
5
6
hexo.extend.filter.register('theme_inject', function(injects) {

// 页面加载动画
injects.bodyBegin.file('loader', 'source/html/loader.html');

});
  • 编写 loader.js, 使用 jquery 方式使网页加载完成后动画消失
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    function loadScript(url, callback) {
    const script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = url;

    script.onload = function() {
    callback();
    };

    document.head.appendChild(script);
    }

    // 加载 jQuery
    loadScript('https://code.jquery.com/jquery-3.6.0.min.js', function() {
    $(function(){
    $("#loader-container").fadeOut(560);
    });
    });

    最后在 _config.fluid.yml 中引入 loader.cssloader.js 并执行 hexo 三联就完成加载动画的添加了

参考资料

清山博客Fluid主题背景固定与毛玻璃底页
Fluid官方文档 注入代码
CSDN 为你的网站加上Loading等待加载效果吧 | Loading页面加载添加教程
codepen.io


Hexo Fluid背景固定以及添加加载动画
http://example.com/2024/10/04/Hexo-Fluid背景固定以及添加加载动画/
作者
Emberff
发布于
2024年10月4日
许可协议