最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Navbar covering content when I click on a link to navigate to different sections of the page - Stack Overflow

programmeradmin0浏览0评论

Here is my html:

@import url(':wght@100&display=swap');

.project-img {
  width: 400px;
  height: 289px;
}

.projects-container {
  display: flex;
  flex-direction: row;
}

.li-nav {
  list-style: none;
  padding: 10px 10px 10px 0px;
}

#ul-nav {
  display: flex;
  flex-direction: row;
}

#navbar {
  position: fixed;
  background-color: white;
  width: 5000px;
}

#main {
  padding: 80px 0px 0px 0px;
}

a {
  text-decoration: none;
  color: black;
  font-weight: 500
}

.nav-link {
  font-size: 20px;
}

#all {
  font-family: Roboto;
}
<div id=all>
  <nav id="navbar">
    <ul id="ul-nav">
      <li class="li-nav"><a class="nav-link" href="#wele-text">Sobre</a></li>
      <li class="li-nav"><a class="nav-link" href="#projects">Projetos</a></l>
        <li class="li-nav"><a class="nav-link" id="profile-link" target="_blank" href="">Github</a></li>
    </ul>
  </nav>
  <div id="main">
    <div id="wele-section">
      <h1 id="wele-text">Olá, meu nome é Caio e sou estudante de Desenvolvimento Web. Aqui, você irá encontrar meus principais projetos.</h1>
    </div>

    <div id="projects">
      <h2>Projetos</h2>

      <div class="projects-container">
        <div class="project-tile">

          <h3>Tribute Page</h3>
          <a href="" target="_blank"> <img class="project-img" src=".png" alt="tribute page project"> </a>
        </div>

        <div class="project-tile">
          <h3>Technical Documentation Page
            <h2>
              <a href=""><img class="project-img" src=".png" alt="technical documentation page"></a>
        </div </div>
      </div>
    </div>
  </div>

Here is my html:

@import url('https://fonts.googleapis./css2?family=Roboto:wght@100&display=swap');

.project-img {
  width: 400px;
  height: 289px;
}

.projects-container {
  display: flex;
  flex-direction: row;
}

.li-nav {
  list-style: none;
  padding: 10px 10px 10px 0px;
}

#ul-nav {
  display: flex;
  flex-direction: row;
}

#navbar {
  position: fixed;
  background-color: white;
  width: 5000px;
}

#main {
  padding: 80px 0px 0px 0px;
}

a {
  text-decoration: none;
  color: black;
  font-weight: 500
}

.nav-link {
  font-size: 20px;
}

#all {
  font-family: Roboto;
}
<div id=all>
  <nav id="navbar">
    <ul id="ul-nav">
      <li class="li-nav"><a class="nav-link" href="#wele-text">Sobre</a></li>
      <li class="li-nav"><a class="nav-link" href="#projects">Projetos</a></l>
        <li class="li-nav"><a class="nav-link" id="profile-link" target="_blank" href="https://github./caiocavalcante063">Github</a></li>
    </ul>
  </nav>
  <div id="main">
    <div id="wele-section">
      <h1 id="wele-text">Olá, meu nome é Caio e sou estudante de Desenvolvimento Web. Aqui, você irá encontrar meus principais projetos.</h1>
    </div>

    <div id="projects">
      <h2>Projetos</h2>

      <div class="projects-container">
        <div class="project-tile">

          <h3>Tribute Page</h3>
          <a href="https://codepen.io/caiocavalcante063/full/BaWmxvy" target="_blank"> <img class="project-img" src="https://i.ibb.co/CnrgnXL/Captura-de-tela-de-2021-06-03-11-04-03.png" alt="tribute page project"> </a>
        </div>

        <div class="project-tile">
          <h3>Technical Documentation Page
            <h2>
              <a href="https://codepen.io/caiocavalcante063/pen/dyvdydM"><img class="project-img" src="https://i.ibb.co/8cCPT2k/Captura-de-tela-de-2021-06-03-10-26-27.png" alt="technical documentation page"></a>
        </div </div>
      </div>
    </div>
  </div>

The navbar is in a fixed position, so when I click on a navbar link the page scroll down to the refered content. But the problem is that when I do so the navbar is positioned just where the content should be, covering it.

Is there a way to make the navbar links scroll down just a bit less so that I can get the expected effect? or a more effective solution?

Share Improve this question edited Jun 5, 2021 at 8:37 Nat Riddle 1,0001 gold badge14 silver badges28 bronze badges asked Jun 4, 2021 at 14:39 user12048519user12048519 2
  • By the way, you have a few errors with the closing tags in your code, namely </l> instead of </li> on the "Projectos" link, </div instead of </div> at the end, and forgetting to close your <h3> and <h2> for the Technical Documentation Page. – Nat Riddle Commented Jun 4, 2021 at 14:50
  • 1 Thanks for the advices, I will fix it. – user12048519 Commented Jun 4, 2021 at 15:01
Add a ment  | 

2 Answers 2

Reset to default 6

Since the browser is doing what it is told to do properly (scrolling to the anchor's position), you have to do a little "hack" to get something like this to work. Here's the basic idea:

  • Create a container element for both a title and an (unseen) anchor
  • Create an element for the title, and put it in the container
  • Create an element for the anchor, and put it in the container
  • Use absolute positioning to move the anchor the appropriate amount up (generally something like FIXED_HEADER_HEIGHT + EXTRA_PADDING)

Here's a quick example:

.container {
  position: relative;
}

.anchor {
  position: absolute;
  top: -65px; /* given the fixed header is 50px tall, and you want 15px padding */
  left: 0px;
}

And the HTML:

<div class="container">
  <h2 class="title">Section Title</h2>
  <a class="anchor" name="target"></a>
</div>

Then, any link to #target will go to a location 65px above .title.

I just had the same issue and solved it using scroll-margin-top CSS property on the anchor link element

.anchor {       
     scroll-margin-top: 70px; // 70px is the offset value
  }
<a href="#anchor" class="anchor">Hyperlink</a>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>