视差代码不起作用,未显示在 chrome 预览中

Parallax code not working, not showing up in chrome preview

我第一次尝试制作视差,遇到了麻烦。

我正在学习 this 教程,然后尝试倒退。但是代码不起作用,我不确定我在哪里犯了错误,我跳到其他一些教程并尝试调整不同 div 和 CSS 块的名称,所以代码有点乱现在。

.html {
  height: 100%;
  overflow: hidden;
}

.body {
  max-width: 30px color: #fff;
  margin: 0;
  padding: 0;
  perspective: 1px;
  transform-style: preserve-3d;
  height: 100% overflow-y: scroll;
  overflow-x: "Luna"
}

header {
  box-sizing: border-box;
  min-height: 100vh;
  padding 30vw 0 5vw;
  position: relative;
  transform-style: inherit;
  width: 100vw;
}

header h1 {
  margin-top: -100px;
}

header,
header:before {
  background: 50% 50% / cover;
}

header::before {
  bottom: 0;
  content: "";
  left: 0;
  position: absolute;
  right: 0;
  top: 0;
  display: block;
  background-image: url(picture1.jpg);
  background-size: cover;
  transform-origin: center center 0;
  transform: tranlasteZ(-1px) scale(2);
  z-index: -1;
  min-height: 100vh;
}

header * {
  font-weight: normal;
  letter-spacing: 0.2em;
  text-align: center;
  margin: 0;
  padding: 1em 0;
}

.image1 {
  background: url('img/(picture1.jpg') no-repeat center;
  background-size: cover;
  background-attachment: fixed;
  height: 500px
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>Schade's Parralax</title>
  <link rel="stylesheet" href="style.css">
</head>

<body>
  <div class="header">
    <p>Hi My name is schade I wrote this so I could have a test of my program.</p>
    <div class="image1"> </div>
  </div>
</body>

</html>

首先使用容器元素并使用特定 height 将背景图像添加到 container。然后使用 background-attachment: fixed 创建实际的视差效果。

body,
html {
  height: 100%;
}

h1 {
  width: 100%;
  height: 100%;
  padding: 0;
  margin: 0;
  text-transform: uppercase;
  text-align: center;
  font-family: Helvetica;
  font-size: 75px;
}

.parallax {
  background-image: url('https://images.pexels.com/photos/36764/marguerite-daisy-beautiful-beauty.jpg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260');
  height: 100%;
  /* Parallax scrolling effect */
  background-attachment: fixed; // Try to remove this property
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
}

.content {
  height: 300px;
  line-height: 300px;
  background: #ededed;
  position: relative;
  z-index: 1;
  box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.5);
}
<!DOCTYPE html>
<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>
  <div class="parallax"></div>

  <div class="content">
    <h1>content</h1>
  </div>

  <div class="parallax"></div>
</body>

</html>

部分移动设备 background-attachment: fixed 存在问题。您可以使用media queries关闭视差效果:

@media only screen and (max-device-width: 1366px) {
    .parallax {
        background-attachment: scroll;
    }
}

有关 fixed 属性 的更多信息。