html - 如何使跨度出现在另一个跨度下?
问题描述
我试图为我的网站制作动画介绍,但我不知道如何让动画背景出现在文本跨度之前。
我已经尝试过使用z-index
,但似乎没有用。
#frontcolor {
z-index: 2;
animation: revBackDrop 1s linear 0s;
animation-fill-mode: forwards;
display: block;
background-color: #eceff4;
height: 100%;
width: auto;
box-shadow: inset 3px 3px 5px 6px #ccc;
}
#titletext {
z-index: 1;
position: absolute;
display: block;
top: 8px;
left: 50%;
margin-left: -65px;
color: red;
font-size: 48px;
font-family: 'Oswald', sans-serif;
}
<span id="frontcolor"></span>
<span id="titletext">Autorzy</span>
解决方案
正如@Mike 上面指出的那样,一个元素需要 aposition
才能z-index
有效。
请参见下面的示例。#frontcolor
已分配position: relative
,因此您现在可以调整它z-index
。单击按钮将更改其z-index
以使其显示在前面或后面#titletext
。
添加该position
属性应该允许您对网站上的动画执行所需的操作。
var btn = document.getElementById('switchBtn');
var span1 = document.getElementById('frontcolor');
btn.addEventListener('click', function() {
// on click, switch which span is in front
if (span1.style.zIndex == 0) {
span1.style.zIndex = 2;
} else {
span1.style.zIndex = 0;
}
});
#frontcolor {
position: relative; /* added to apply z-index */
z-index: 0;
animation: revBackDrop 1s linear 0s;
animation-fill-mode: forwards;
display: block;
background-color: #eceff4;
height: 100%;
width: auto;
box-shadow: inset 3px 3px 5px 6px #ccc;
}
#titletext {
z-index: 1;
position: absolute; /* added to apply z-index */
display: block;
top: 8px;
left: 50%;
margin-left: -65px;
color: red;
font-size: 48px;
font-family: 'Oswald', sans-serif;
}
<span id="frontcolor">frontcolor span</span>
<span id="titletext">titletext span</span>
<br /><br />
<button id="switchBtn">Switch</button>
推荐阅读
- linux - 两个文件之间的 AWK 部分字符串搜索
- java - 在 Java 中执行 /usr/bin/env bash -c "command"
- android - 启动特定的 Flutter 屏幕
- node.js - Docker - 撰写我无法创建 postgress 服务器
- php - Phalcon 3.4: Inject a component in a controller action without using dispatcher events
- postgresql - 如何配置从 Vanilla Postgresql 9.6 DB 到 AWS RDS Postgresql 的流复制
- android - 实时数据中的两个对象相互引用
- java - 如何以正确的方式制作高负载 Java 应用程序?
- python - cv2.imwrite 没有写入正确的像素值
- c++ - 为什么擦除其他字符时最后一个字符会加倍,如何防止?