【问题标题】:Parallax background image fit div视差背景图像适合 div
【发布时间】:2020-01-04 13:09:10
【问题描述】:

我正在尝试创建视差背景作为网站的分隔符。背景图像似乎只占据了它应该有的一半大小,周围有一个奇怪的白色边框。我似乎在我的代码中找不到任何可能导致这种情况的东西,无论我做什么,我要么有边框,要么在内部 div 上有滚动条。我只需要背景图片来适应 div 的空间,以便在该部分中产生滚动视差。

我尝试过溢出:隐藏使用各种高度/宽度组合,包括 calc(100vh * 2) 以使图像更大。它似乎唯一要做的就是更改它出现的容器中的图像(几乎就像一个子容器,但上面没有子容器)。我还玩过变换、变换原点、透视图、比例以及我在 google 和这里​​可以找到的任何其他内容。


html, body {
    height: 100%!important;
    margin: 0px;
    padding: 0px;
}

input, select, textarea {
    box-shadow: inset 0 2px 4px hsla(0, 0%, 0%, 0.13);
}

.ico {
    width: 50px!important;
    height: 50px!important;
}

nav {
    position: fixed!important;
    z-index: 999;
    width: 100vw;
}

.content_head {
    background-color: aqua;
}

.content_about {
    background-color: blueviolet;
}

.content_spacer {
    opacity: 0;
    height: 50vh;
}

.back_spacer {
    opacity: 0;
}

.back1 {
    background-image: url("images/IMG_1164.JPG");
    background-clip: border-box;
    background-size: cover;
    height: 100%!important;
    width: 100%!important;
    min-height: 100%!important;
    min-width: 100%!important;
}

.back-group {
    max-height: 75vh!important;
}

.container-fluid {
    padding: 0!important;

}

/* Parallax Styles Credit to: https://keithclark.co.uk/articles/pure-css-parallax-websites */

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

.parallax__layer {
    position: absolute;
    transform-origin-x: 100%;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
}

.parallax__layer--base {
    transform: translateZ(0);
    height: 100vh;
}

.parallax__layer--back {
      position: absolute;
      width: 30%!important;
      height: 50vh!important;
      top: 0;
      right: 0;
      bottom: 0;
      left: auto;
      margin: 0;
      transform: translateZ(-1px);
      -webkit-transform-origin-y: 100% 100% 0px;
      -moz-transform-origin: 100% 100% 0px;
      -ms-transform-origin: 100% 100% 0px;
      transform-origin: 100% 100% 0px;
}

.parallax__group {
    position: relative;
    height: 100vh;
    transform-style: preserve-3d;
    overflow: auto!important;
}


    <div class="parallax">
        <div class="parallax__group">
            <div class="parallax__layer--base content_head">
                <h1 class="text-center my-auto">Content base</h1>
            </div>
        </div>
        <div class="parallax__group back-group">
            <div class="parallax__layer--back back1"></div>
            <div class="parallax__layer--base content_spacer"></div>
        </div>
        <div class="parallax__group">
            <div class="parallax__layer--base content_about">
                <h1 class="text-center my-auto">Content base 2</h1>
            </div>
        </div>
        <div class="parallax__group">
            <div class="parallax__layer--back back">
            <div class="parallax__layer--base content_spacer"></div>
                <img src="images/IMG_1170.JPG" height="4096" width="3072" class="img-fluid"/>
            </div>
        </div>
    </div>

严格来说,我认为它实际上并不是一个边框,而是背景图片比整个 DIV 元素要小。

【问题讨论】:

  • 放置正确的图像路径后没有边框并且滚动是因为你的.parallax类overflow-y: auto
  • div 元素内部有一个水平和垂直滚动,我想去掉它。 “正确的图像路径”是什么意思?
  • 正确的图片路径 在img标签中放置正确的图片路径后,我没有看到任何边框,您可以截屏并告诉我们您说的是哪个边框.是否要从代码中删除所有滚动条?
  • 提供了截图并在当前编辑中进行了澄清。
  • 这样你的边框问题就解决了,现在你想在 div 中覆盖你的图像吗?正如您的屏幕截图所说,内容库 1 和 2 之间有图像,但您的代码表示不同,请提供完整代码^_^

标签: css bootstrap-4 parallax


【解决方案1】:

body,
html {
  margin: 0;
  padding: 0;
}

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

.section {
  position: relative;
  height: 100vh;
  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;
  transform: translateZ(-1px) scale(1.5);
  background-size: 100%;
  z-index: -1;
}

.static {
  background: red;
}

.bg1::after {
  background-image: url('https://images.unsplash.com/photo-1567170578400-9d182981f2a1?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=750&q=80');
}

.bg2::after {
  background-image: url('https://images.unsplash.com/photo-1567170566770-eea3bb0b16ed?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=750&q=80');
}
<main class="wrapper">
  <section class="section static">
    <h1>Static</h1>
  </section>
  <section class="section parallax bg1">
    <h1>Parallax</h1>
  </section>
  <section class="section static">
    <h1>Static</h1>
  </section>
  <section class="section parallax bg2">
    <h1>Parallax</h1>
  </section>
  <section class="section static">
    <h1>Static</h1>
  </section>
</main>

【讨论】:

  • 我喜欢。如果它与站点的其余部分一起使用,我将实现它并接受它作为答案。这正是我一直在寻找的。谢谢。
  • 效果很好,完全符合我的需要!谢谢!