如何使用CSS创建图像滚动视差效果? [英] How to create image scrolling parallax effect with CSS?

查看:62
本文介绍了如何使用CSS创建图像滚动视差效果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上看到了很酷的滚动效果...

I saw this cool scrolling effect online...

在各部分中滚动时图像与下一张图像融合的位置。我一直在尝试重现它,但似乎无法弄清楚?
如何在网络上创建此效果?

Where the image blends with the next image when scrolling through sections. I've been trying to reproduce it, but I can't seem to figure it out? How can I create this effect on the web?

这里是我看到效果的链接... http://readingbuddysoftware.com/how-it-works/

Here is the link to where I saw the effect... http://readingbuddysoftware.com/how-it-works/

我尝试在屏幕截图中使用 position:fixed ,其中该部分的 z-index 高于图像,但最后一个屏幕截图始终位于顶部。

I've tried using position: fixed on the screenshots with the z-index of the section higher then the image, but the last screenshot is always on the top.

有什么想法吗?

更新:由于各种原因(包括放置,使用倾斜...),我不能使用 background-image css解决方案。我需要使用< img> 元素的解决方案。

Update: For various reasons (including placement, using slants...), I can't use the background-image css solution. I need a solution for using the <img> element.

推荐答案

可以使用 background-附件 :fixed 和两个相似的图像。

This can be done using background-attachement:fixed and two similar images.

这里是一个简单示例:

body {
  min-height:200vh;
  margin:0;
  background:url(https://picsum.photos/id/1069/150/150?grayscale) 20px 20px no-repeat;
  background-attachment:fixed;
}

.box {
  margin-top:220px;
  height:200px;
  background:url(https://picsum.photos/id/1069/150/150) 20px 20px no-repeat,
  grey;
  background-attachment:fixed;
}

<div class="box">

</div>

轻松缩放许多图像:

body {
  min-height:250vh;
  margin:0;
  background:url(https://picsum.photos/id/1069/150/150?grayscale) 50px 50px/auto no-repeat;
  background-attachment:fixed;
}

.box {
  height:200px;
  background:url(https://picsum.photos/id/1069/150/150) 50px 50px/auto no-repeat,
  grey;
  background-attachment:fixed;
}
.box:first-child {
  margin-top:200px;
}

<div class="box">
</div>
<div class="box" style="background-image:url(https://picsum.photos/id/11/150/150);background-color:yellow">
</div>
<div class="box" style="background-image:url(https://picsum.photos/id/106/150/150);background-color:pink">
</div>

您还可以考虑使用 img position:fixed ,但是您将需要一些技巧使用剪切路径

You can also consider the use of img and position:fixed but you will need some trick to hide the overflow using clip-path

body {
  min-height: 250vh;
  margin: 0;
  padding-top: 100px;
}

img {
  position: fixed;
  top: 50px;
  left: 50px;
}

.box {
  height: 200px;
  background: grey;
  clip-path:polygon(0 0,100% 0,100% 100%,0 100%);
}

<div class="box">
  <img src="https://picsum.photos/id/1074/200/120?grayscale">
</div>
<div class="box" style="background-color:red;">
  <img src="https://picsum.photos/id/1074/200/120">
</div>
<div class="box" style="background-color:yellow;">
  <img  src="https://picsum.photos/id/1024/200/120?grayscale">
</div>
<div class="box" style="background-color:pink;">
  <img src="https://picsum.photos/id/1024/200/120">
</div>

或使用 mask

body {
  min-height: 250vh;
  margin: 0;
  padding-top: 100px;
}

img {
  position: fixed;
  top: 50px;
  left: 50px;
}

.box {
  height: 200px;
  background: grey;
  -webkit-mask:linear-gradient(#fff,#fff);
          mask:linear-gradient(#fff,#fff);
}

<div class="box">
  <img src="https://picsum.photos/id/1074/200/120?grayscale">
</div>
<div class="box" style="background-color:red;">
  <img src="https://picsum.photos/id/1074/200/120">
</div>
<div class="box" style="background-color:yellow;">
  <img  src="https://picsum.photos/id/1024/200/120?grayscale">
</div>
<div class="box" style="background-color:pink;">
  <img src="https://picsum.photos/id/1024/200/120">
</div>


为了获得更好的支持,有些JS的想法与此类似,以避免使用剪切路径或蒙版

For better support, here is a similar idea with some JS to avoid the use of clip-path or mask

我将使用CSS变量更新图像的位置,但您可以轻松地做到以下几点:

I will update the position of the image using a CSS variables but you can easily do without:

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 {
  min-height: 150vh;
  margin: 0;
}

img {
  position: fixed;
  top: 20px;
  left: 20px;
}

.box {
  margin-top: 220px;
  height: 200px;
  background: grey;
  position: relative;
  overflow: hidden;
}

.box img {
  top: calc(-220px + 20px + var(--scroll-var));
  /* margin of box + top of the other image + scroll*/
  position: absolute;
}

<img src="https://picsum.photos/id/1069/150/150?grayscale">
<div class="box">
  <img src="https://picsum.photos/id/1069/150/150">
</div>

具有很多图像:

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 {
  min-height: 250vh;
  margin: 0;
  padding-top:200px;
}

img {
  position: fixed;
  top: 50px;
  left: 50px;
}

.box {
  height: 200px;
  background: grey;
  position: relative;
  overflow: hidden;
}
img.f1 {
  top: calc(-200px + 50px + var(--scroll-var));
  position: absolute;
}
img.f2 {
  top: calc(-400px + 50px + var(--scroll-var));
  position: absolute;
}
img.f3 {
  top: calc(-600px + 50px + var(--scroll-var));
  position: absolute;
}

<img src="https://picsum.photos/id/1069/100/100?grayscale">
<div class="box">
  <img class="f1" src="https://picsum.photos/id/1069/100/100">
</div>
<div class="box" style="background-color:yellow;">
  <img class="f2" src="https://picsum.photos/id/107/100/100">
</div>
<div class="box" style="background-color:pink;">
  <img class="f3" src="https://picsum.photos/id/1072/100/100">
</div>

这篇关于如何使用CSS创建图像滚动视差效果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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