bloghexo魔改Blog首页背景图渐进式加载一图流
xiaoze 首页图片渐进式加载一图流
渐进式加载是一种优化网页性能和用户体验的技术,它可以让图片在加载过程中逐渐显示出清晰度,从而避免了图片加载缓慢或失败时出现的空白或占位符。这个方法同样适用于一图流的博客背景,因此我们只需要更改部分代码即可实现本站首页效果。
把首页的顶图配置启用,为了加一张背景图整个博客的加载速度都受到了影响,不仅访问体验不流畅,甚至一点都不美观(特别是图片资源卡住的时候)。以下为一个调整图片加载的方案。按照原文章提供的部分代码做出调整,适配了pjax以及增加了手机端的设备判断,最终得出了本博客使用的效果。原理是先加载小图文件并进行高斯模糊处理,在大图加载完成后再对大图进行加载。
reference:
Butterfly主题优化首页大图加载效果 - 假想国
1.首页背景图渐进式加载,解决卡顿难题
2.Hexo美化:自适应切换渐进式加载首页图
3.Kouseki式首页背景图渐进式加载 · 改
anzhiyu主题一图流
(source/js/imgloaded.js 改图)
操作步骤
- 新建文件
source/js/imgloaded.js
新增以下内容,并按照注释调整图片路径
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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
(function() { class ProgressiveLoad { constructor(smallSrc, largeSrc) { this.smallSrc = smallSrc; this.largeSrc = largeSrc; this.initTpl(); this.container.addEventListener('animationend', () => { this.smallStage.style.display = 'none'; }, {once: true}); } initTpl() { this.container = document.createElement('div'); this.smallStage = document.createElement('div'); this.largeStage = document.createElement('div'); this.smallImg = new Image(); this.largeImg = new Image(); this.container.className = 'pl-container'; this.smallStage.className = 'pl-img pl-blur'; this.largeStage.className = 'pl-img'; this.container.appendChild(this.smallStage); this.container.appendChild(this.largeStage); this.smallImg.onload = this._onSmallLoaded.bind(this); this.largeImg.onload = this._onLargeLoaded.bind(this); } progressiveLoad() { this.smallImg.src = this.smallSrc; this.largeImg.src = this.largeSrc; } _onLargeLoaded() { this.largeStage.classList.add('pl-visible'); this.largeStage.style.backgroundImage = `url('${this.largeSrc}')`; } _onSmallLoaded() { this.smallStage.classList.add('pl-visible'); this.smallStage.style.backgroundImage = `url('${this.smallSrc}')`; } } const executeLoad = (config, target) => { console.log('执行渐进背景替换'); const isMobile = window.matchMedia('(max-width: 767px)').matches; const loader = new ProgressiveLoad( isMobile ? config.mobileSmallSrc : config.smallSrc, isMobile ? config.mobileLargeSrc : config.largeSrc ); if (target.children[0]) { target.insertBefore(loader.container, target.children[0]); } loader.progressiveLoad(); }; const ldconfig = { light: { smallSrc: '/img/pc1.png', largeSrc: '/img/pc2.png', mobileSmallSrc: '/img/mobile1.png', mobileLargeSrc: '/img/mobile2.png', enableRoutes: ['/'], }, dark: { smallSrc: '/img/pc1.png', largeSrc: '/img/pc3.png', mobileSmallSrc: '/img/mobile1.png', mobileLargeSrc: '/img/mobile3.png', enableRoutes: ['/'], }, }; const getCurrentTheme = () => { return document.documentElement.getAttribute('data-theme'); } const onThemeChange = () => { const currentTheme = getCurrentTheme(); const config = ldconfig[currentTheme]; initProgressiveLoad(config); document.addEventListener("DOMContentLoaded", function() { initProgressiveLoad(config); }); document.addEventListener("pjax:complete", function() { onPJAXComplete(config); }); } let initTheme = getCurrentTheme(); let initConfig = ldconfig[initTheme]; initProgressiveLoad(initConfig); const observer = new MutationObserver(mutations => { mutations.forEach(mutation => { if (mutation.attributeName === "data-theme" && location.pathname === '/') { onThemeChange(); } }); }); observer.observe(document.documentElement, { attributes: true, attributeFilter: ["data-theme"] }); function initProgressiveLoad(config) { const container = document.querySelector('.pl-container'); if (container) { container.remove(); } const target = document.getElementById('page-header'); if (target && target.classList.contains('full_page')) { executeLoad(config, target); } } function onPJAXComplete(config) { const target = document.getElementById('page-header'); if (target && target.classList.contains('full_page')) { initProgressiveLoad(config); } } })();
|
- 新建文件
source/css/imgloaded.css
新增以下内容,并按照注释自行决定调整内容
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
| .pl-container { width: 100%; height: 100%; z-index: -2; position: fixed; overflow: hidden; will-change: transform; animation: blur-to-clear 2s cubic-bezier(.62,.21,.25,1) 0s 1 normal backwards running, scale 1.5s cubic-bezier(.62,.21,.25,1) 0s 1 both; } .pl-img { width: 100%; height: 100%; position: absolute; background-position: center; background-size: cover; background-repeat: no-repeat; opacity: 0; transition: opacity 1s; } @keyframes blur-to-clear { 0% { filter: blur(50px); opacity: 1; } 100% { filter: blur(0); opacity: 1; } } @keyframes scale { 0% { transform: scale(1.5) translateZ(0); opacity: 0; } to { transform: scale(1) translateZ(0); opacity: 1; } } .pl-visible { opacity: 1; } .pl-blur { filter: blur(50px); }
#footer { background: transparent !important; }
#page-header { background: transparent !important; }
#footer-bar{ background: transparent !important; }
#category-bar{ background: transparent !important; }
|
- 在
_config.anzhiyu.yml
主题配置文件下inject配置项中head和bottom处分别引入imgloaded.css
和imgloaded.js
文件
1 2 3 4 5 6
| inject: head: - <link rel="stylesheet" href="/css/imgloaded.css?1">
bottom: - <script async data-pjax src="/js/imgloaded.js?1"></script>
|
配置懒加载
- 在
_config.anzhiyu.yml
中修改懒加载lazyload将field
修改为post
1 2 3 4 5 6
| lazyload: enable: true field: post placeholder: blur: true progressive: true
|
配置图片
务必记得在主题配置文件中开启顶部图的功能,也可以像我这样配置空链接。因为js文件已经接替了图片加载功能,此处不需要配置图片(当然你也可以配置上)
1 2
| index_img: "background: url() top / cover no-repeat"
|
- 在imgloaded.js中第70行至73行处,也就是以下示例的部分配置自己的图片,可以是图片直链也可以是本地路径
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| const ldconfig = { light: { smallSrc: '/img/pc1.png', largeSrc: '/img/pc2.png', mobileSmallSrc: '/img/mobile1.png', mobileLargeSrc: '/img/mobile2.png', enableRoutes: ['/'], }, dark: { smallSrc: '/img/pc1.png', largeSrc: '/img/pc3.png', mobileSmallSrc: '/img/mobile1.png', mobileLargeSrc: '/img/mobile3.png', enableRoutes: ['/'], }, };
|
大功告成
按照代码中的注释进行修改就好了,在配置完成后hexo三连就能看到效果了