2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
id: 587d781e367417b2b2512acc
|
2020-12-16 07:37:30 +00:00
|
|
|
|
title: 固定定位的参照物是浏览器的窗口
|
2018-10-10 22:03:03 +00:00
|
|
|
|
challengeType: 0
|
2020-02-11 07:46:34 +00:00
|
|
|
|
videoUrl: 'https://scrimba.com/c/c2MDNUR'
|
|
|
|
|
forumTopicId: 301061
|
2021-01-13 02:31:00 +00:00
|
|
|
|
dashedName: lock-an-element-to-the-browser-window-with-fixed-positioning
|
2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --description--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
|
接下来要介绍的是 `fixed` 定位,它是一种特殊的绝对(absolute)定位,区别是其包含块是浏览器窗口。和绝对定位类似,在 `fixed` 定位的元素中,我们也可以使用 top、bottom、left、right 属性来调整元素的位置,并且也会将元素从当前的文档流里面移除,其它元素会忽略它的存在。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
|
但 `fixed` 和 `absolute` 的最明显的区别在于,`fixed` 定位元素不会随着屏幕滚动而移动。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --instructions--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
|
我们已经将代码里导航栏的 id 设置为了 `navbar`。请把它的 `position` 设置成 `fixed`,同时分别设定其 `top` 和 `left` 属性值为 0 像素。修改后,你可以滑动预览窗口,观察导航栏的位置。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --hints--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
|
`#navbar` 元素的 `position` 属性值应为 `fixed`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert($('#navbar').css('position') == 'fixed');
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
|
`#navbar` 元素的 `top` 属性值应为 `0px`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert($('#navbar').css('top') == '0px');
|
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
|
`#navbar` 元素的 `left` 属性值应为 `0px`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert($('#navbar').css('left') == '0px');
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
2020-02-11 07:46:34 +00:00
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
<style>
|
|
|
|
|
body {
|
|
|
|
|
min-height: 150vh;
|
|
|
|
|
}
|
|
|
|
|
#navbar {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
width: 100%;
|
|
|
|
|
background-color: #767676;
|
|
|
|
|
}
|
|
|
|
|
nav ul {
|
|
|
|
|
margin: 0px;
|
|
|
|
|
padding: 5px 0px 5px 30px;
|
|
|
|
|
}
|
|
|
|
|
nav li {
|
|
|
|
|
display: inline;
|
|
|
|
|
margin-right: 20px;
|
|
|
|
|
}
|
|
|
|
|
a {
|
|
|
|
|
text-decoration: none;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
<body>
|
|
|
|
|
<header>
|
|
|
|
|
<h1>Welcome!</h1>
|
|
|
|
|
<nav id="navbar">
|
|
|
|
|
<ul>
|
|
|
|
|
<li><a href="">Home</a></li>
|
|
|
|
|
<li><a href="">Contact</a></li>
|
|
|
|
|
</ul>
|
|
|
|
|
</nav>
|
|
|
|
|
</header>
|
|
|
|
|
<p>I shift up when the #navbar is fixed to the browser window.</p>
|
|
|
|
|
</body>
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
```html
|
|
|
|
|
<style>
|
|
|
|
|
body {
|
|
|
|
|
min-height: 150vh;
|
|
|
|
|
}
|
|
|
|
|
#navbar {
|
|
|
|
|
position: fixed;
|
|
|
|
|
top: 0;
|
|
|
|
|
left: 0;
|
|
|
|
|
width: 100%;
|
|
|
|
|
background-color: #767676;
|
|
|
|
|
}
|
|
|
|
|
nav ul {
|
|
|
|
|
margin: 0px;
|
|
|
|
|
padding: 5px 0px 5px 30px;
|
|
|
|
|
}
|
|
|
|
|
nav li {
|
|
|
|
|
display: inline;
|
|
|
|
|
margin-right: 20px;
|
|
|
|
|
}
|
|
|
|
|
a {
|
|
|
|
|
text-decoration: none;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
<body>
|
|
|
|
|
<header>
|
|
|
|
|
<h1>Welcome!</h1>
|
|
|
|
|
<nav id="navbar">
|
|
|
|
|
<ul>
|
|
|
|
|
<li><a href="">Home</a></li>
|
|
|
|
|
<li><a href="">Contact</a></li>
|
|
|
|
|
</ul>
|
|
|
|
|
</nav>
|
|
|
|
|
</header>
|
|
|
|
|
<p>I shift up when the #navbar is fixed to the browser window.</p>
|
|
|
|
|
</body>
|
|
|
|
|
```
|