gpt4 book ai didi

javascript - 视差滚动 : Unwanted gap in between layers

转载 作者:行者123 更新时间:2023-11-28 15:11:28 27 4
gpt4 key购买 nike

在我的网站上 here ,我用 CSS 和 Javascript 实现了多层视差滚动。页面上下滚动,我有 6 个图层都在视差中移动。向后的每一层以比前面的层稍慢的速度滚动。

在我单击右上角的汉堡菜单之前,此实现一直有效。

Image of the Bug

我不确定为什么在幻灯片菜单飞出后图层没有正确地堆叠在一起。

这是视差滚动的 CSS:

.layer {
background-position: bottom center;
background-size: auto;
background-repeat: no-repeat;
width: 100%;
height: 800px;
position: fixed;
z-index: -1;
}

.layer-bg {
background-image: url("http://www.reynelee.com/wp-content/uploads/2017/12/layer1.png");
}

.layer-1 {
background-image: url("http://www.reynelee.com/wp-content/uploads/2017/12/layer2.png\a ");
background-position: left bottom;
}

.layer-2 {
background-image: url("http://www.reynelee.com/wp-content/uploads/2017/12/layer3.png");
}

.layer-3 {
background-image: url("http://www.reynelee.com/wp-content/uploads/2017/12/layer4.png\a ");
background-position: right bottom;
}

这是 JS:

(function() {
window.addEventListener('scroll', function(event) {
var depth, i, layer, layers, len, movement, topDistance, translate3d;
topDistance = this.pageYOffset;
layers = document.querySelectorAll("[data-type='parallax']");
for (i = 0, len = layers.length; i < len; i++) {
layer = layers[i];
depth = layer.getAttribute('data-depth');
movement = -(topDistance * depth);
translate3d = 'translate3d(0, ' + movement + 'px, 0)';
layer.style['-webkit-transform'] = translate3d;
layer.style['-moz-transform'] = translate3d;
layer.style['-ms-transform'] = translate3d;
layer.style['-o-transform'] = translate3d;
layer.style.transform = translate3d;
}
});

}).call(this);

HTML:

<div id='hero'>
<div class='layer-bg layer' data-depth='0.10' data-type='parallax'></div>
<div class='layer-1 layer' data-depth='0.20' data-type='parallax'></div>
<div class='layer-2 layer' data-depth='0.50' data-type='parallax'></div>
<div class='layer-3 layer' data-depth='0.80' data-type='parallax'></div>
<div class='layer-overlay layer' data-depth='0.85' data-type='parallax'></div>
<!-- <div class='layer-4 layer' data-depth='0.90' data-type='parallax'></div> -->
<div class='layer-logo layer' data-depth='0.95' data-type='parallax'> <a id='portfolio-button' href="http://www.reynelee.com/my-work/">View Work</a></div> <!-- my logo-->
<!-- <div class='layer-close layer' data-depth='1.00' data-type='parallax'></div> -->

</div>

最佳答案

问题是由 transform css 属性引起的。例如,如果您应用以下规则:

#page {
transform: translateX(0);
}

您会注意到它会在不打开菜单的情况下导致同样的问题。我以前遇到过这个,但我忘记了术语的实际名称,但是应用转换会导致浏览器以某种方式对其进行不同的处理。我的建议是将此 translateX(0) 应用于 #page 元素,并根据此默认位置重新计算视差 div 的位置。我知道这不是一个完整的答案,但它应该让你继续。

关于javascript - 视差滚动 : Unwanted gap in between layers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48308832/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com