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

标签 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>

最佳答案

首先使用容器元素并将背景图像添加到 container具有特定的 height .然后使用 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属性(property)。

关于html - 视差代码不起作用,未显示在 chrome 预览中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50430921/

相关文章:

html - 如何在网页上水平正确显示html编码的汉字?

javascript - 在一个简单的 jquery 视差示例中使文本以不同的速度滚动

ios - 在 UIAlertController 中禁用视差 iOS

php - 通过 php 检测文档(表格)中的列 - 算法

javascript - Timesheet.js 未启动

javascript - jQuery 菜单系统 - 显示没有顶级菜单项扩展以适应的子菜单项

javascript - 如何制作适用于 iOS 和桌面的视差滚动网站?

html - 无法确定 16px 空白的原因

javascript - AngularJS 选择框在文档就绪时设置默认选择值

html - css3 将 div 与动态内容居中