2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 587d78a3367417b2b2512acf
|
2020-12-16 00:37:30 -07:00
|
|
|
|
title: 使用 z-index 属性更改重叠元素的位置
|
2018-10-10 18:03:03 -04:00
|
|
|
|
challengeType: 0
|
2020-02-11 15:46:34 +08:00
|
|
|
|
videoUrl: 'https://scrimba.com/c/cM94aHk'
|
|
|
|
|
forumTopicId: 301046
|
2021-01-13 03:31:00 +01:00
|
|
|
|
dashedName: change-the-position-of-overlapping-elements-with-the-z-index-property
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --description--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
|
当一些元素在位置上重叠时(例如,使用 `position: absolute | relative | fixed | sticky` 时),在 HTML 里后出现的元素会默认显示在更早出现的元素的上面。 你可以使用 `z-index` 属性指定元素的堆叠次序。 `z-index` 的取值是整数,数值大的元素会叠放到数值小的元素上面。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --instructions--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
|
给 class 为 `first` 的元素(红色的方块)添加 `z-index` 属性并将属性值设置为 2,使它显示在蓝色方块的上方。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --hints--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
|
class 为 `first` 的元素的 `z-index` 属性值应为 2。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert($('.first').css('z-index') == '2');
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
<style>
|
|
|
|
|
div {
|
|
|
|
|
width: 60%;
|
|
|
|
|
height: 200px;
|
|
|
|
|
margin-top: 20px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.first {
|
|
|
|
|
background-color: red;
|
|
|
|
|
position: absolute;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
.second {
|
|
|
|
|
background-color: blue;
|
|
|
|
|
position: absolute;
|
|
|
|
|
left: 40px;
|
|
|
|
|
top: 50px;
|
|
|
|
|
z-index: 1;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
|
|
|
|
|
<div class="first"></div>
|
|
|
|
|
<div class="second"></div>
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --solutions--
|
2020-02-11 15:46:34 +08:00
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
```html
|
|
|
|
|
<style>
|
|
|
|
|
div {
|
|
|
|
|
width: 60%;
|
|
|
|
|
height: 200px;
|
|
|
|
|
margin-top: 20px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.first {
|
|
|
|
|
background-color: red;
|
|
|
|
|
position: absolute;
|
|
|
|
|
z-index: 2;
|
|
|
|
|
}
|
|
|
|
|
.second {
|
|
|
|
|
background-color: blue;
|
|
|
|
|
position: absolute;
|
|
|
|
|
left: 40px;
|
|
|
|
|
top: 50px;
|
|
|
|
|
z-index: 1;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
<div class="first"></div>
|
|
|
|
|
<div class="second"></div>
|
|
|
|
|
```
|