gpt4 book ai didi

html - 视差模板中的中心 Div/section 元素

转载 作者:太空宇宙 更新时间:2023-11-04 06:44:29 26 4
gpt4 key购买 nike

所以我有这个我正在玩的视差模板,我想添加 DIV 元素,所以在向下滚动后 DIV 会出现,但是我遇到的问题是该元素移动到左侧以及何时我将浏览器缩小到 25%,右侧全部填满。我该如何纠正这个问题?

Div moves to left

Zoom out DIV is filled on right

HTML

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>Firewatch Parallax in CSS</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.3/modernizr.min.js" type="text/javascript"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
<link rel="stylesheet" href="css/style.css">
</head>

<body>

<div class="parallax">
<div class="parallax__layer parallax__layer__0">
<img src="https://sam.beckham.io/images/articles/firewatch/layer_0.png" />
</div>
<div class="parallax__layer parallax__layer__1">
<img src="https://sam.beckham.io/images/articles/firewatch/layer_1.png" />
</div>
<div class="parallax__layer parallax__layer__2">
<img src="https://sam.beckham.io/images/articles/firewatch/layer_2.png" />
</div>
<div class="parallax__layer parallax__layer__3">
<img src="https://sam.beckham.io/images/articles/firewatch/layer_3.png" />
</div>
<div class="parallax__layer parallax__layer__4">
<img src="https://sam.beckham.io/images/articles/firewatch/layer_4.png" />
</div>
<div class="parallax__layer parallax__layer__5">
<img src="https://sam.beckham.io/images/articles/firewatch/layer_5.png" />
</div>
<div class="parallax__layer parallax__layer__6">
<img src="https://sam.beckham.io/images/articles/firewatch/layer_6.png" />
</div>
<div class="parallax__cover">
<section class="section section_dark">
<h2>Section One</h2>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sunt, laudantium, quibusdam? Nobis, delectus,
commodi,
fugit amet tempora facere dolores nisi facilis consequatur, odio hic minima nostrum. Perferendis eos earum
</p>
</section>
</div>
</div>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
</body>

</html>

CSS

* {
box-sizing: border-box;
}

html,
body {
background-color: #FEDCC8;
}

.parallax {
-webkit-perspective: 100px;
perspective: 100px;
height: 100vh;
overflow-x: hidden;
overflow-y: auto;
position: absolute;
top: 0;
left: 50%;
right: 0;
bottom: 0;
margin-left: -1500px;
}

.parallax__layer {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}

.parallax__layer img {
display: block;
position: absolute;
bottom: 0;
}

.parallax__cover {
background: rgb(199, 27, 187);
display: block;
position: absolute;
top: 100%;
left: 0;
right: 0;
height: 2000px;
z-index: 2;
/* left: 20%; */

}

.parallax__layer__0 {
-webkit-transform: translateZ(-300px) scale(4);
transform: translateZ(-300px) scale(4);
}

.parallax__layer__1 {
-webkit-transform: translateZ(-250px) scale(3.5);
transform: translateZ(-250px) scale(3.5);
}

.parallax__layer__2 {
-webkit-transform: translateZ(-200px) scale(3);
transform: translateZ(-200px) scale(3);
}

.parallax__layer__3 {
-webkit-transform: translateZ(-150px) scale(2.5);
transform: translateZ(-150px) scale(2.5);
}

.parallax__layer__4 {
-webkit-transform: translateZ(-100px) scale(2);
transform: translateZ(-100px) scale(2);
}

.parallax__layer__5 {
-webkit-transform: translateZ(-50px) scale(1.5);
transform: translateZ(-50px) scale(1.5);
}

.parallax__layer__6 {
-webkit-transform: translateZ(0px) scale(1);
transform: translateZ(0px) scale(1);
}


.section {
text-align: center;
padding: 50px 80px;
/* position: fixed; */
/* z-index: 1; */
/* left: 20px;
right: 20px; */
line-height: auto;
box-shadow: 0px 0px 40px -1px #000000bf;
/* left: 80%; */

}

.section_dark {
background-color: #282e34;
color: #ddd;
}

最佳答案

您在 .parallax 元素上有一个负边距,因为它是绝对定位的,所以您应该将它定位在距它包含元素左侧 0 个单位的位置:

body {
margin: 0;
position: relative;
}

.parallax {
top: 0;
/* left: 50%; */
left: 0;
right: 0;
bottom: 0;
/* margin-left: -1500px; */
}

单独执行上述操作应该使元素居中,但您的 img 元素将变得比您提供的原始示例小。您可以通过将图像的高度设置为您满意的高度和 100% 的宽度然后使用 object-fit 来解决此问题。处理纵横比的倾斜:

.parallax__layer img {
display: block;
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 70%; // set this to what you want
object-fit: cover;
}

查看下面的完整演示:

* {
box-sizing: border-box;
}

html,
body {
background-color: #FEDCC8;
}

body {
margin: 0;
position: relative;
}

.parallax {
-webkit-perspective: 100px;
perspective: 100px;
height: 100vh;
overflow-x: hidden;
overflow-y: auto;
position: absolute;
top: 0;
/* left: 50%; */
left: 0;
right: 0;
bottom: 0;
/* margin-left: -1500px; */
}

.parallax__layer {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}

.parallax__layer img {
display: block;
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 70%;
object-fit: cover;
}

.parallax__cover {
background: rgb(199, 27, 187);
display: block;
position: absolute;
top: 100%;
left: 0;
right: 0;
height: 2000px;
z-index: 2;
/* left: 20%; */

}

.parallax__layer__0 {
-webkit-transform: translateZ(-300px) scale(4);
transform: translateZ(-300px) scale(4);
}

.parallax__layer__1 {
-webkit-transform: translateZ(-250px) scale(3.5);
transform: translateZ(-250px) scale(3.5);
}

.parallax__layer__2 {
-webkit-transform: translateZ(-200px) scale(3);
transform: translateZ(-200px) scale(3);
}

.parallax__layer__3 {
-webkit-transform: translateZ(-150px) scale(2.5);
transform: translateZ(-150px) scale(2.5);
}

.parallax__layer__4 {
-webkit-transform: translateZ(-100px) scale(2);
transform: translateZ(-100px) scale(2);
}

.parallax__layer__5 {
-webkit-transform: translateZ(-50px) scale(1.5);
transform: translateZ(-50px) scale(1.5);
}

.parallax__layer__6 {
-webkit-transform: translateZ(0px) scale(1);
transform: translateZ(0px) scale(1);
}


.section {
text-align: center;
padding: 50px 80px;
/* position: fixed; */
/* z-index: 1; */
/* left: 20px;
right: 20px; */
line-height: auto;
box-shadow: 0px 0px 40px -1px #000000bf;
/* left: 80%; */

}

.section_dark {
background-color: #282e34;
color: #ddd;
}
<div class="parallax">
<div class="parallax__layer parallax__layer__0">
<img src="https://sam.beckham.io/images/articles/firewatch/layer_0.png" />
</div>
<div class="parallax__layer parallax__layer__1">
<img src="https://sam.beckham.io/images/articles/firewatch/layer_1.png" />
</div>
<div class="parallax__layer parallax__layer__2">
<img src="https://sam.beckham.io/images/articles/firewatch/layer_2.png" />
</div>
<div class="parallax__layer parallax__layer__3">
<img src="https://sam.beckham.io/images/articles/firewatch/layer_3.png" />
</div>
<div class="parallax__layer parallax__layer__4">
<img src="https://sam.beckham.io/images/articles/firewatch/layer_4.png" />
</div>
<div class="parallax__layer parallax__layer__5">
<img src="https://sam.beckham.io/images/articles/firewatch/layer_5.png" />
</div>
<div class="parallax__layer parallax__layer__6">
<img src="https://sam.beckham.io/images/articles/firewatch/layer_6.png" />
</div>
<div class="parallax__cover">
<section class="section section_dark">
<h2>Section One</h2>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sunt, laudantium, quibusdam? Nobis, delectus,
commodi,
fugit amet tempora facere dolores nisi facilis consequatur, odio hic minima nostrum. Perferendis eos earum
</p>
</section>
</div>
</div>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>

关于html - 视差模板中的中心 Div/section 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53429271/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com