修改页面滚动视差效果
# 前言
实现页面滚动时背景图随滚动发生位移的视差效果
# 实现
# 修改 Js
找到
themes\shoka\source\js\_app\global.js
;大概 20 行左右新增:
var imgMover = $('#imgs'); // 新增
位置参考:
const sideBar = $('#sidebar');
const siteBrand = $('#brand');
var toolBtn = $('#tool'), toolPlayer, backToTop, goToComment, showContents;
var angleBtn = $('#angle');
var imgMover = $('#imgs'); // 此处新增(参考位置)
var siteSearch = $('#search');
在 188 行左右:
const scrollHandle = function (event) {
var winHeight = window.innerHeight;
var docHeight = getDocHeight();
var contentVisibilityHeight = docHeight > winHeight ? docHeight - winHeight : document.body.scrollHeight - winHeight;
var SHOW = window.pageYOffset > headerHightInner;
var startScroll = window.pageYOffset > 0;
// 此处可以新增代码
if (SHOW) {
changeMetaTheme('#FFF');
} else {
changeMetaTheme('#222');
}
代码处新增以下代码:
var oVal = Math.round(Math.min(300 * window.pageYOffset / contentVisibilityHeight, 100)) / -5;
var max = 200 + window.pageYOffset;
if (oVal > max) {
oVal = max;
}
imgMover.style.transform = 'translate3d(0,' + oVal + 'vh,0)';
imgMover.style.webkitTransform = 'translate3d(0,' + oVal + 'vh,0)';
imgMover.style.msTransform = 'translate3d(0,' + oVal + 'vh,0)';
imgMover.style.OTransform = 'translate3d(0,' + oVal + 'vh,0)';
此处的
var oVal = Math.round(Math.min(300 * window.pageYOffset / contentVisibilityHeight, 100)) / -5;
中的数值根据自己实际情况喜好可做出修改修复切换页面时视差效果失效的问题:在
themes\shoka\source\js\_app\pjax.js
文件大概 53 行const siteRefresh = function (reload) {
LOCAL_HASH = 0
LOCAL_URL = window.location.href
// 此处新增
vendorCss('katex');
vendorJs('copy_tex');
新增如下代码
imgMover = $('#imgs')
完成!