freeCodeCamp/curriculum/challenges/chinese/01-responsive-web-design/applied-visual-design/lock-an-element-to-the-brow...

121 lines
2.5 KiB
Markdown
Raw Normal View History

---
id: 587d781e367417b2b2512acc
title: 固定定位的参照物是浏览器的窗口
challengeType: 0
videoUrl: 'https://scrimba.com/c/c2MDNUR'
forumTopicId: 301061
dashedName: lock-an-element-to-the-browser-window-with-fixed-positioning
---
# --description--
接下来要介绍的是 `fixed` 定位它是一种特殊的绝对absolute定位区别是其包含块是浏览器窗口。和绝对定位类似`fixed` 定位的元素中,我们也可以使用 top、bottom、left、right 属性来调整元素的位置,并且也会将元素从当前的文档流里面移除,其它元素会忽略它的存在。
`fixed``absolute` 的最明显的区别在于,`fixed` 定位元素不会随着屏幕滚动而移动。
# --instructions--
我们已经将代码里导航栏的 id 设置为了 `navbar`。请把它的 `position` 设置成 `fixed`,同时分别设定其 `top``left` 属性值为 0 像素。修改后,你可以滑动预览窗口,观察导航栏的位置。
# --hints--
`#navbar` 元素的 `position` 属性值应为 `fixed`
```js
assert($('#navbar').css('position') == 'fixed');
```
`#navbar` 元素的 `top` 属性值应为 `0px`
```js
assert($('#navbar').css('top') == '0px');
```
`#navbar` 元素的 `left` 属性值应为 `0px`
```js
assert($('#navbar').css('left') == '0px');
```
# --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>
```
# --solutions--
```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>
```