css - 将 :nth-child 选择器与 CSS 网格一起使用会产生意想不到的行为
问题描述
我正在使用 CSS 网格来定位元素并具有淡入效果。但是在将文本放置在图像下方并使用 :nth-child() CSS 属性选择每个 h4 标记后,导致第 4 和第 5 图像没有显示(因为它们的不透明度没有改变)。我猜,nth-child 选择网格中的第一个 5 个元素,不管它是否是 h4。
请提出我错在哪里。
这是 JSFiddle 的链接:- https://jsfiddle.net/aew2tjhr/1/
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.min.css"
integrity="sha256-L/W5Wfqfa0sdBNIKN9cG6QA5F2qx4qICmU2VgLruv9Y=" crossorigin="anonymous" />
<style>
.container {
margin-top: 500px;
}
.working-process {
background-color: #000;
}
.working-process h2 {
color: #fff;
margin-bottom: 4rem;
}
.emphasize {
color: #ff7810;
}
.single-work {
display: grid;
grid-template-columns: repeat(5, 200px);
grid-template-rows: 150px;
grid-auto-rows: 150px;
justify-content: center;
}
.single-work h4 {
color: #000;
grid-row: 2/3;
}
.single-work-icon {
position: relative;
height: 120px;
width: 120px;
border: 1px solid #ff7810;
background-color: #e49152;
border-radius: 50%;
display: flex;
justify-content: center;
align-items: center;
flex-basis: 120px;
margin-right: 4rem;
}
.single-work-icon:not(:nth-child(5))::after {
content: " ";
position: absolute;
top: 50%;
left: 50%;
border-right: 70px solid #fff;
width: 70px;
height: 2px;
}
.single-work,
.single-work>.single-work-icon {
opacity: 0;
}
.single-work.show {
animation: showWorks 1s ease-in-out 0.5s forwards;
}
.single-work.show>.single-work-icon:nth-child(1) {
animation: showWorks 1s ease-in-out 0.5s forwards;
}
.single-work.show>.single-work-icon:nth-child(2) {
animation: showWorks 1s ease-in-out 1.5s forwards;
}
.single-work.show>.single-work-icon:nth-child(3) {
animation: showWorks 1s ease-in-out 2.5s forwards;
}
.single-work.show>.single-work-icon:nth-child(4) {
animation: showWorks 1s ease-in-out 3.5s forwards;
}
.single-work.show>.single-work-icon:nth-child(5) {
animation: showWorks 1s ease-in-out 4.5s forwards;
}
.single-work.show .single-work-icon:nth-child(1)::after {
animation: slideInRight 2s ease-in-out 0s forwards;
}
.single-work.show .single-work-icon:nth-child(2)::after {
animation: slideInRight 1s ease-in-out 2s forwards;
}
.single-work.show .single-work-icon:nth-child(3)::after {
animation: slideInRight 1s ease-in-out 3s forwards;
}
.single-work.show .single-work-icon:nth-child(4)::after {
animation: slideInRight 1s ease-in-out 4s forwards;
}
@keyframes showWorks {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
@keyframes showArrows {
0% {
opacity: 0;
transform: translateX(0);
}
100% {
opacity: 1;
transform: translateX(100%);
}
}
@keyframes slideInRight {
0% {
left: 50%;
}
100% {
left: 100%;
}
}
</style>
</head>
<body>
<section class="pos-r working-process">
<div class="container">
<h2 class="text-center">How <span class="emphasize">Digital Marketing Agency</span> work</h2>
<div class="row">
<div class="col-lg-12 col-md-12 ml-auto md-mt-5">
<div class="single-work">
<div class="single-work-icon">
<img src="img/working_process/research.png" alt="Research" />
</div>
<h4>Research</h4>
<div class="single-work-icon">
<img src="img/working_process/design.png" alt="Design" />
</div>
<h4>Design</h4>
<div class="single-work-icon">
<img src="img/working_process/Implement.png" alt="Implement" />
</div>
<h4>Implement</h4>
<div class="single-work-icon">
<img src="img/working_process/measure.png" alt="Measure" />
</div>
<h4>Measure</h4>
<div class="single-work-icon">
<img src="img/working_process/result.png" alt="Result" />
</div>
<h4>Result</h4>
</div>
</div>
</div>
</div>
</section>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script>
function isInViewport(element) {
var elementTop = $(element).offset().top;
var elementBottom = elementTop + $(element).outerHeight();
var viewportTop = $(window).scrollTop();
var viewportBottom = viewportTop + $(window).height();
// console.log(elementTop, elementBottom, viewportTop, viewportBottom);
return elementBottom > viewportTop && elementTop < viewportBottom;
}
$(window).on("resize scroll", function () {
if (isInViewport(document.querySelector(".working-process"))) {
document.querySelector(".single-work").classList.add("show");
} else {
if (
document.querySelector(".single-work").classList.contains("show")
) {
document.querySelector(".single-work").classList.remove("show");
}
}
});
</script>
</body>
</html>
解决方案
推荐阅读
- mysql - 需要一些帮助从查询中获取表,以备将来在我自己的数据库中使用
- python - python中的延迟微分方程
- ios - Is it possible to use CoreBluetooth instead of iBeacons to measure distance in the background?
- elasticsearch - 检查多个字段是否存在
- json - 当函数签名明确说明返回类型时,Swift 期望返回 void
- android - 使用 sqlCipher 时,DBflow 不适用于现有数据库
- android - 无法解析方法 setSupportActionBar(androidx.appcompat.widget.Toolbar)
- jenkins - 如何在远程节点上使用机密文件
- three.js - three.js - 为什么看起来多个像素具有相同的 gl_FragCoord.xy?
- jwasm - 我可以在 ASP.NET 中提供 wasm 文件吗?