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

2.5 KiB
Raw Blame History

id title challengeType videoUrl forumTopicId dashedName
587d781e367417b2b2512acc 固定定位的參照物是瀏覽器的窗口 0 https://scrimba.com/c/c2MDNUR 301061 lock-an-element-to-the-browser-window-with-fixed-positioning

--description--

接下來要介紹的是 fixed 定位它是一種特殊的絕對absolute定位將元素相對於瀏覽器窗口定位。 類似於絕對位置,它與 CSS 偏移屬性一起使用,並且也會將元素從當前的文檔流裏面移除。 其它元素會忽略它的存在,這樣也許需要調整其他位置的佈局。

fixedabsolute 的最明顯的區別在於,前者定位的元素不會隨着屏幕滾動而移動。

--instructions--

我們已經將代碼裏導航欄的 id 設置爲了 navbar。 請把它的 position 設置成 fixed,同時分別設定其 topleft 屬性值爲 0 像素。 修改後,你可以滑動預覽窗口,觀察導航欄的位置。

--hints--

#navbar 元素的 position 屬性值應爲 fixed

assert($('#navbar').css('position') == 'fixed');

#navbar 元素的 top 屬性值應爲 0px。

assert($('#navbar').css('top') == '0px');

#navbar 元素的 left 屬性值應爲 0px。

assert($('#navbar').css('left') == '0px');

--seed--

--seed-contents--

<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--

<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>