视差效果 - 强制文本块表现得像背景附件:固定 [英] Parallax effect - force text block to behave like background-attachment: fixed

查看:72
本文介绍了视差效果 - 强制文本块表现得像背景附件:固定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的视差效果,其中每个 100vh 部分向上滚动以显示下一部分(新背景颜色,背景图像和文本块),同时保持文本块相对于其父容器是固定的。

I'm attempting to create a simple parallax effect where each 100vh section scrolls up to reveal the next section (new background color, background image, and text block) while keeping the text block fixed relative to its parent container.

我已经使用每个部分的屏幕截图组合了一个我想要实现的静态示例:静态示例。当然,我希望内容是动态的,而不是平面图像。

I've put together a static example of what I'm trying to achieve using screenshots of each section: static example. Of course I'd like the content to be dynamic rather than flat images.

到目前为止,这是我的代码的简单版本:

Here's a simple version of my code thus far:

body {
  margin: 0;
  padding: 0;
}
h2 {
  font-size: 48px;
}
p {
  font-size: 18px;
}
section {
  min-height: 100vh;
  width: 100%;
  text-align: center;
  position: relative;
  background-attachment: fixed !important;
  background-size: cover !important;
  background-repeat: no-repeat !important;
}
section.first {
  background: url(https://picsum.photos/1920/500/?image=1057);
}
section.first .content {
  background-color: rgba(74, 180, 220, .85);
}
section.second {
  background: url(https://picsum.photos/1920/500/?image=1067);
}
section.second .content {
  background-color: rgba(103, 198, 180, .85)
}
section.third {
  background: url(https://picsum.photos/1920/500/?image=1033);
}
section.third .content {
  background-color: rgba(5, 123, 188, .85);
}
section.fourth {
  background: url(https://picsum.photos/1920/500?image=1063);
}
section.fourth .content {
  background-color: rgba(187, 216, 100, .85)
}
.content {
  position: relative;
  height: 100vh;
  width: 100%;
  padding: 50px 0;  
}
.copy {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: #fff;
  font-family: 'Noto Serif', serif;
  font-weight: 300;
}
.button {
  border: 2px solid #fff;
  border-radius: 3px;
  padding: 15px 25px;
  display: inline-block;
  width: auto;
  font-family: 'Assistant', sans-serif;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1px;
  transition: .2s ease all;
}
.button:hover {
  background: #fff;
  color: #333;
  cursor: pointer;
}

<body>
	<section class="first">
		<div class="content">
			<div class="copy">
				<h2>Header 1  </h2>
				<p>Nullam id dolor id nibh ultricies vehicula ut id elit. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.</p>
			</div>
		</div>
	</section>
	<section class="second">
		<div class="content">
			<div class="copy">
				<h2>Header 2</h2>
				<p>Nullam id dolor id nibh ultricies vehicula ut id elit. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.</p>
			</div>
		</div>
	</section>
	<section class="third">
		<div class="content">
			<div class="copy">
				<h2>Header 3</h2>
				<p>Nullam id dolor id nibh ultricies vehicula ut id elit. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.</p>
			</div>
		</div>
	</section>
	<section class="fourth">
		<div class="content">
			<div class="copy">
				<h2>Call to action</h2>
				<a class="button">Button</a>
			</div>
		</div>
	</section>
</body>

视差效果是使用CSS 后台附件:固定实现的,并且工作得很好;麻烦在于文本块。我想让他们固定到位,并在他们的部分中心。如果它们设置为 position:fixed ,它们当然会相互重叠,并且都显示在第一部分中。如果它们被设置为任何其他位置属性,它们将像任何其他元素一样滚动。

The parallax effect is achieved using CSS background-attachment: fixed and works just fine; the trouble is with the text blocks. I'd like to keep them "pinned" in place and centered within their section. If they are set to position: fixed they of course overlap each other and all show up in the first section. If they are set to any other position attribute, they will simply scroll like any other element.

现在,我意识到将元素的位置设置为固定意味着它不再能够相对于其父元素;它可以说是逃避了流程,但我试图确定是否有办法通过一些高级CSS甚至是JS替代方案来实现效果。

Now, I realize that setting an element's position to fixed means it can no longer be relative to its parent element; it escapes the flow so to speak, but I'm trying to determine if there's a way to achieve the effect with some advanced CSS or even a JS alternative.

我'尝试了很多HTML / CSS组合(包装器中的包装器等),我也尝试了各种javascript解决方案,如rellax,jarallax和ScrollMagic,但我遇到的一切都太强大了,无法满足我的需求。我一直在寻找更好的一天,希望找到一个我正在尝试的例子,但没有运气。

I've tried numerous HTML/CSS combinations (wrappers within wrappers, etc.) and I've also attempted various javascript solutions such as rellax, jarallax, and ScrollMagic, but everything I've come across is far too robust for my needs. I've searched around for the better part of the day hoping to find an example of what I'm attempting, but no luck.

推荐答案

上一个问题我对图像和使用一些JS做了类似的效果,所以我将使用相同的技术使用内容重现这一点,因为我认为没有纯CSS解决方案。所以我的想法是通过使用绝对位置来模拟固定位置,并在滚动时动态调整top属性。

In a previous question I did a similar effect with image and using some JS so am going to use the same technique to reproduce this using content as I don't think there is a pure CSS solution. So the idea is to simulate the fixed position by using absolute position and adjusting the top property dynamically on scroll.

这是一个例子,我也将一些CSS调整为让它更容易。我还将依赖CSS变量来使JS代码非常轻松,因此我们可以使用CSS管理所有内容。

Here is an example where I also adjusted some of the CSS to make it easier. I will also rely on CSS variables to make the JS code very light so we can manage everything with CSS.

window.onscroll = function() {
  var scroll = window.scrollY || window.scrollTop || document.getElementsByTagName("html")[0].scrollTop;
  document.documentElement.style.setProperty('--scroll-var', scroll + "px");
}

:root {
  --scroll-var: 0px
}

body {
  margin: 0;
  padding: 0;
}

h2 {
  font-size: 48px;
}

p {
  font-size: 18px;
}

section {
  min-height: 100vh;
  width: 100%;
  text-align: center;
  overflow: hidden;
  background-attachment: fixed !important;
  background-size: cover !important;
  background-repeat: no-repeat !important;
  position: relative; /*Mandatory for the overflow effect*/
  height: 100vh;
}

section.first {
  background: linear-gradient(rgba(74, 180, 220, .85), rgba(74, 180, 220, .85)), url(https://picsum.photos/1920/500/?image=1057);
}

section.first .content {
  /* the first section so top start from 0*/
  top: calc((0 * 100vh) + var(--scroll-var));
}

section.second {
  background: linear-gradient(rgba(103, 198, 180, .85), rgba(103, 198, 180, .85)), url(https://picsum.photos/1920/500/?image=1067);
}

section.second .content {
  /* the second section so we need to remove the height of top section
     to have the same position so -100vh and we do the same for the other sections  
  */
  top: calc((-1 * 100vh) + var(--scroll-var));
}

section.third {
  background: linear-gradient(rgba(5, 123, 188, .85), rgba(5, 123, 188, .85)), url(https://picsum.photos/1920/500/?image=1033);
}

section.third .content {
  top: calc((-2 * 100vh) + var(--scroll-var));
}

section.fourth {
  background: linear-gradient(rgba(187, 216, 100, .85), rgba(187, 216, 100, .85)), url(https://picsum.photos/1920/500?image=1063);
}

section.fourth .content {
  top: calc((-3 * 100vh) + var(--scroll-var));
}

.content {
  position: absolute;
  height: 100%;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.copy {
  color: #fff;
  font-family: 'Noto Serif', serif;
  font-weight: 300;
  max-width: 300px;
}

.button {
  border: 2px solid #fff;
  border-radius: 3px;
  padding: 15px 25px;
  display: inline-block;
  width: auto;
  font-family: 'Assistant', sans-serif;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1px;
  transition: .2s ease all;
}

.button:hover {
  background: #fff;
  color: #333;
  cursor: pointer;
}

<body>
  <section class="first">
    <div class="content">
      <div class="copy">
        <h2>Header 1 </h2>
        <p>Nullam id dolor id nibh ultricies vehicula ut id elit. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.</p>
      </div>
    </div>
  </section>
  <section class="second">
    <div class="content">
      <div class="copy">
        <h2>Header 2</h2>
        <p>Nullam id dolor id nibh ultricies vehicula ut id elit. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.</p>
      </div>
    </div>
  </section>
  <section class="third">
    <div class="content">
      <div class="copy">
        <h2>Header 3</h2>
        <p>Nullam id dolor id nibh ultricies vehicula ut id elit. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.</p>
      </div>
    </div>
  </section>
  <section class="fourth">
    <div class="content">
      <div class="copy">
        <h2>Call to action</h2>
        <a class="button">Button</a>
      </div>
    </div>
  </section>
</body>

这篇关于视差效果 - 强制文本块表现得像背景附件:固定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆