前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >volantis多背景图的优化方案

volantis多背景图的优化方案

作者头像
DearXuan
发布2022-01-19 18:59:36
4020
发布2022-01-19 18:59:36
举报

原方案的不足之处

volantis的所有背景图都储存在配置文件中,如果图片过多,就会导致配置文件冗长

DearXuan
DearXuan

实际上,产生随机图的原理是遍历数组并随机交换,因此图片过多时加载速度也会大幅度降低

但是实际上这些图片的地址都高度相似,因此我们可以换个思路,每次生成一个随机数,并动态生成图片地址,这样就大大提高了加载速度,并且省去了填写地址的麻烦

备份

在修改源码之前先备份源文件

命名格式

使用数字命名图片,下标从1开始,例如 “1.jpg”,”2.jpg”。将所有图片上传至网页目录或图床中,请确保所有图片都在同一个文件夹下

修改代码

DearXuan
DearXuan

在_config.volantis.yml或volantis.yml里删除原images下的所有图片地址,改为total

代码语言:javascript
复制
parallax:
  enable: true
  position: fixed       # cover: sticky on the cover.   fixed: Fixed as background for the site.
  shuffle: true         # shuffle playlist
  duration: 60000       # Duration (ms)
  fade: 1500            # fade duration (ms) (Not more than 1500)
  images:               # For personal use only. At your own risk if used for commercial purposes !!!
    total: xxx

其中xxx表示图片的数量,假如你有100张图片,则total为100

打开volantis/layout/_plugins/parallax/script.ejs,如果主题更新导致文件位置改变,可以直接搜索

将代码替换为

代码语言:javascript
复制
<script>
  let total = <%-theme.plugins.parallax.images.total%>;
  let index = Math.floor(Math.random() * total) + 1;
  let IntervalParallax = null;
 
  function parallax(){
    let ParallaxWindow = document.querySelector("#parallax-window");
    <% if (theme.plugins.parallax.position=="fixed") { %>
      ParallaxWindow = document.querySelector("html");
    <% } %>
    Parallax.window = ParallaxWindow;
    Parallax.options.fade = <%- theme.plugins.parallax.fade %>;
    Parallax.cache = 1;
    next_parallax();
    Parallax.init();
    if (total>1) {
      IntervalParallax = setInterval(function () {
        next_parallax();
      }, '<%- theme.plugins.parallax.duration %>');
    }
  }
 
  function next_parallax() {
    if (typeof Parallax == "undefined") {
      return
    }
    <% if (theme.plugins.parallax.position!="fixed") { %>
      if (!document.querySelector("#full")&&!document.querySelector("#half")) {
        return
      }
    <% } %>
    if (total>=1) {
      Parallax.options.src = "你的图片地址" + index + ".jpg";
      Parallax.start();
      index++;
      if (Parallax.cache) {
        fetch("你的图片地址" + index + ".jpg?t=" + new Date().getTime());
      }
    }
  }
  var runningOnBrowser = typeof window !== "undefined";
  var isBot = runningOnBrowser && !("onscroll" in window) || typeof navigator !== "undefined" && /(gle|ing|ro|msn)bot|crawl|spider|yand|duckgo/i.test(navigator.userAgent);
  if (!isBot) {
    volantis.js('<%- theme.cdn.map.js.parallax %>').then(()=>{
      parallax()
    })
    volantis.pjax.send(()=>{
      clearInterval(IntervalParallax)
    },"clearIntervalParallax");
    volantis.pjax.push(parallax);
  }
</script>

将两处中文改为你的图片地址

刷新

打开网页,按下CTRL+F5强制刷新,如果背景正常显示,则修改成功

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021年11月29日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 原方案的不足之处
  • 备份
  • 命名格式
  • 修改代码
  • 刷新
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档