视差代码不起作用,没有出现在Chrome预览中

时间:2018-05-20 01:31:53

标签: html css parallax

我试图第一次做出视差而且遇到了麻烦。

我正在关注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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:3)

首先使用容器元素并将背景图片添加到具有特定container的{​​{1}}。然后使用height创建实际的视差效果。

&#13;
&#13;
background-attachment: fixed
&#13;
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);
}
&#13;
&#13;
&#13;

某些移动设备遇到<!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

有关fixed财产的更多信息。