【发布时间】:2019-08-04 11:58:57
【问题描述】:
我正在使用剪切路径根据背景颜色更改我的徽标颜色。
除此之外,徽标会根据用户在页面上的垂直位置从上到下滚动。页面顶部 = 顶部徽标,页面底部 = 底部徽标等。
不幸的是,当我添加剪切路径时,徽标失去了滚动位置,在第一个之后,根本不起作用。
有没有办法解决这个问题?此外,徽标位置开始时有点偏离,所以如果有任何方法可以同时解决这个问题。
您可以在此处查看原始问题: div position based on scroll position
我已经尝试过了,但我似乎无法让它工作。
Scroll position lost when hiding div
我正在使用高级自定义字段,并且 PHP 文件的每个部分在标题中都有这个作为剪切路径的一部分,相应地使用白色或深色版本的徽标。其父级相对定位,其子级绝对定位。
div class="logo-scroll">
<div class="scroll-text">
<a href="/home"><img width="53px" height="260px" src="/wp-content/uploads/2019/07/sheree-walker-web-design-edinburgh-vertical-01.svg"/></a>
</div>
</div>
Javascript
const docHeight = Math.max(document.documentElement.scrollHeight, document.body.scrollHeight);
const logo = document.querySelector('.scroll-text');
const logoHeight = logo.offsetHeight;
// to get the pseudoelement's '#page::before' top we use getComputedStyle method
const barTopMargin = parseInt(getComputedStyle(document.querySelector('#page'), '::before').top);
let viewportHeight, barHeight, maxScrollDist, currentScrollPos, scrollFraction;
logo.style.top = barTopMargin + 'px';
window.addEventListener('load', update);
window.addEventListener('resize', setSizes);
document.addEventListener('scroll', update);
setSizes();
function update() {
currentScrollPos = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
scrollFraction = currentScrollPos / (docHeight - viewportHeight);
logo.style.top = barTopMargin + (scrollFraction * maxScrollDist) + 'px';
}
function setSizes() {
viewportHeight = window.innerHeight;
// to get the pseudoelement's '#page::before' height we use getComputedStyle method
barHeight = parseInt(getComputedStyle(document.querySelector('#page'), '::before').height);
maxScrollDist = barHeight - logoHeight;
update();
}
CSS
.logo-scroll .scroll-text img {
padding: 0 6px 0 17px;
}
#page::before {
content: "";
position: fixed;
top: 30px;
bottom: 30px;
left: 30px;
right: 30px;
border: 2px solid white;
pointer-events: none;
-webkit-transition: all 2s; /* Safari prior 6.1 */
transition: all 2s;
}
.logo-scroll {
position: fixed;
left: 30px;
top: 30px;
bottom: 30px;
border-right: 2px solid white;
width: 75px;
z-index: 10;
}
.scroll-text {
position: fixed;
}
【问题讨论】:
标签: javascript jquery html css