简体   繁体   English

视差与背景位置:中心中心没有跳跃

[英]Parallax with background-position: center center without jump

I couldn't think of a solution myself after trying dozens of approaches尝试了几十种方法后,我自己想不出解决方案

#banner .slide {
    position: static;
    top: 0px;
    left: 0px;
    z-index: 99;
    opacity: 1;
    display: block;
    visibility: hidden;
    background-image: url(http://wp-content/uploads/2260-000-01.jpg);
}


#banner .slide {
    background-attachment: inherit;
    background-repeat: no-repeat;
    background-position: center center;
    background-size: cover;
    position: relative;
}

The background-image is positioned center center as you can see and it is intentional that the background-attachement is not fixed (as it would always in combination with cover lead to a to narrow view of the image.)如您所见,背景图像位于center center ,并且background-attachementfixed是有意的(因为它总是与封面结合导致图像的视图变窄。)

In order to achieve the desired parallax I use this snippet.为了实现所需的视差,我使用了这个片段。

$(window).on('scroll', function(e) {
        $('#banner .slide').css('background-position', 'center ' + $(window).scrollTop()*0.4 + 'px')
    })

The problem I have of course is that the initial scroll (very first change in scrollTop) leads to a jump of the image because of course JS does not know what center means for me.我当然遇到的问题是初始滚动(scrollTop 中的第一个变化)导致图像跳转,因为 JS 不知道center对我意味着什么。

So my goal is to keep the image position intially center center as I want to always see the very vertical and horizontal cenver of the image.所以我的目标是将图像位置保持在初始center center因为我希望始终看到图像的垂直和水平中心点。

Do you know any trick or idea how I could tell the JS the current vertical center of the image, so the parallax calculation would start from this point instead of starting from 0.你知道我如何告诉 JS 图像的当前垂直中心的任何技巧或想法,所以视差计算将从这一点开始而不是从 0 开始。

Thanks, Matt谢谢,马特

I would suggest taking a slightly different approach by changing transform value of the slide instead. 我建议采取一种略有不同的方法,即更改幻灯片的变换值。

 const parallaxables = document.querySelectorAll('.parallax'); function runParallaxLoop() { requestAnimationFrame(runParallaxLoop); parallax(); } function parallax() { parallaxables.forEach(parallaxable => { let distance = window.scrollY * 0.5; parallaxable.style.transform = `translate3d(0,-${distance}px, 0)`; }); } runParallaxLoop(); 
 .parallax { width: 100vw; height: 500px; margin-bottom: 400px; background: url(http://lorempixel.com/1200/800/sports/) no-repeat center center; background-attachment: fixed; } 
 <div class="parallax"></div> 

Every frame, you transform the element that needs to be parallaxed by a certain percentage of speed with which the browser is scrolling. 在每一帧中,您都需要将需要视差的元素转换为浏览器滚动的一定百分比的速度。

For this to work, you need the background-attachment of the parallax-able element to be fixed. 为此,需要固定可视差元素的背景附件。

I know this is a little bit old but I just wanted to throw an alternative way in that I have used in the past.我知道这有点旧,但我只是想提出一种我过去使用过的替代方法。 This is 100% vanilla javascript.这是 100% 原版 JavaScript。 One thing to note: you need to call init seperately inside the function so you don't get that ju需要注意的一件事:你需要在函数内部单独调用 init ,这样你就不会得到那个 ju

 let parallax = (id, rate) => { const parallaxObject = document.querySelector(id); const init = () => { const x = parallaxObject.getBoundingClientRect().top / rate; const y = Math.round(x * 100 / 100); parallaxObject.style.backgroundPosition = 'center ' + y + 'px'; } init(); window.addEventListener('scroll', () => { init(); }) } parallax('.hero', 5)
 .hero { background: #fff url('https://source.unsplash.com/random') no-repeat fixed center center / cover; margin-top: 1000px; margin-bottom: 1000px; min-height: 75vh; display: grid; place-items: center; }
 <div class="hero"> <h1>Hero Text</h1> <div> // Scroll Down To See Effect

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM