首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用伪元素的视差(及其高度)

使用伪元素的视差(及其高度)
EN

Stack Overflow用户
提问于 2019-11-28 19:22:39
回答 1查看 914关注 0票数 1

我正试图在我的站点上创建一个视差效应,并且已经研究了几个css解决方案。我选了一个约书亚·贝门德费尔可以看到的这里

我稍微编辑了示例代码,以使问题更加明显,但因为它使用的是一个伪元素,它与其父元素的大小相匹配,所以当我调整视差部分的大小时,我很难阻止图像周围的空白在移动时可见。

(注意,您必须在片段上全屏查看问题):

*编辑:因为代码段没有在全屏或预览中显示问题

代码语言:javascript
复制
/* Tiny reset thingy */
body,html{margin:0;padding:0;}

.wrapper {
  /* The height needs to be set to a fixed value for the effect to work.
   * 100vh is the full height of the viewport. */
  height: 100vh;
  /* The scaling of the images would add a horizontal scrollbar, so disable x overflow. */
  overflow-x: hidden;
  /* Enable scrolling on the page. */
  overflow-y: auto;
  /* Set the perspective to 2px. This is essentailly the simulated distance from the viewport to transformed objects.*/
  perspective: 2px;
}

.section {
  /* Needed for children to be absolutely positioned relative to the parent. */
  position: relative;
  /* The height of the container. Must be set, but it doesn't really matter what the value is. */
  height: 200px;
  
  /* For text formatting. */
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  text-shadow: 0 0 5px #000;
}

.parallax::after {
  /* Display and position the pseudo-element */
  content: " ";
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  
  /* Move the pseudo-element back away from the camera,
   * then scale it back up to fill the viewport.
   * Because the pseudo-element is further away, it appears to move more slowly, like in real life. */
  transform: translateZ(-1px) scale(1.5);
  /* Force the background image to fill the whole element. */
  background-size: 100%;
  /* Keep the image from overlapping sibling elements. */ 
  z-index: -1;
}

/* The styling for the static div. */
.static {
  background: red;
}

/* Sets the actual background images to adorable kitties. This part is crucial. */
.bg1::after {
  background-image: url('https://placekitten.com/g/900/700');
}

.bg2::after {
  background-image: url('https://placekitten.com/g/800/600');
}
代码语言:javascript
复制
<main class="wrapper">
  <section class="section static">
    <h1>Boring</h1>
  </section>
  <section class="section parallax bg2">
    <h1>SO FWUFFY AWWW</hi>
  </section>
    <section class="section static">
    <h1>Boring</h1>
  </section>
</main>

有什么办法解决这个问题吗?我似乎无法在不破坏效果的情况下使它发挥作用

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-28 19:42:39

使伪元素变大,以掩盖这种不良影响。只需使顶部底部值为负值:

代码语言:javascript
复制
/* Tiny reset thingy */
body,html{margin:0;padding:0;}

.wrapper {
  height: 100vh;
  overflow-x: hidden;
  overflow-y: auto;
  perspective: 2px;
}

.section {
  position: relative;
  height: 200px;
  
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  text-shadow: 0 0 5px #000;
}

.parallax::after {
  content: " ";
  position: absolute;
  top: -100px;
  right: 0;
  bottom: -100px;
  left: 0;
  
  transform: translateZ(-1px) scale(1.5);
  background-size: 100% auto;
  z-index: -1;
}

/* The styling for the static div. */
.static {
  background: red;
}

/* Sets the actual background images to adorable kitties. This part is crucial. */
.bg1::after {
  background-image: url('https://placekitten.com/g/900/700');
}

.bg2::after {
  background-image: url('https://placekitten.com/g/800/600');
}
代码语言:javascript
复制
<main class="wrapper">
  <section class="section static">
    <h1>Boring</h1>
  </section>
  <section class="section parallax bg2">
    <h1>SO FWUFFY AWWW</h1>
  </section>
    <section class="section static">
    <h1>Boring</h1>
  </section>
</main>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59095131

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档