【发布时间】:2022-01-06 07:18:25
【问题描述】:
我正在构建一个 Shopify 网站,并希望在您登陆该网站时覆盖图片以覆盖顶部内容。然后,当您滚动时,此叠加图像会在屏幕上上下移动,从而显示网站。
我找到了一些执行此操作的 javascript,但主网站会随着覆盖图像滚动。有没有办法让它自己滚动而不滚动后面的内容?
这是我目前所拥有的:
var container = document.getElementById('overlay-container');
var windowHeight = window.innerHeight;
var windowWidth = window.innerWidth;
var scrollArea = 1000 - windowHeight;
var square1 = document.getElementsByClassName('overlay-background-image')[0];
// var square2 = document.getElementsByClassName('overlay-logo')[1];
// update position of square 1 and square 2 when scroll event fires.
window.addEventListener('scroll', function() {
var scrollTop = window.pageYOffset || window.scrollTop;
var scrollPercent = scrollTop / scrollArea || 0;
square1.style.bottom = -100 * (1 - scrollPercent * 1.5) + 'vh';
// square2.style.top = 800 - scrollPercent*window.innerHeight*0.6 + 'px';
});
// Global variable to control the scrolling behavior
const step = 30; // For each 30px, change an image
function trackScrollPosition() {
const y = window.scrollY;
const label = Math.min(Math.floor(y / 30) + 1, 20);
const imageToUse = fruitImages[label];
// Change the background image
$('.image-container').css('background-image', `url('${imageToUse}')`);
}
$(document).ready(() => {
$(window).scroll(() => {
trackScrollPosition();
})
})
.overlay-container {
position: relative;
max-width: 100%;
}
.overlay-background-image {
position: absolute;
z-index: 5;
left: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div class="overlay-container">
<div class="overlay-background-image">
<img style="height: 100vh;" src="https://cdn.shopify.com/s/files/1/0608/2225/7886/files/01_-_Landing_Page.png?v=1636020266" />
</div>
</div>
【问题讨论】:
-
我们给你做了一个sn-p。缺少一些数组
-
我没有在 sn-p 中做任何事情。我将您的代码转换为 sn-p - 我为什么要回答您的问题?
-
很抱歉我误解了您最初的评论。这是我第一次使用 Stack Overflow,我没有意识到这是你所做的。道歉。
-
只需添加css样式“position:sticky;”到图片下方的页面内容
标签: javascript html css animation