为什么我的导航栏掩盖了我的文字?

时间:2019-01-24 17:16:39

标签: javascript jquery html css

我创建了一个标头,该标头在滚动时有一个生硬的导航栏。但是当我滚动时,我的文字最终显示在其下方。

var fixmeTop = $('.fixme').offset().top;
$(window).scroll(function() {
  var currentScroll = $(window).scrollTop();
  if (currentScroll >= fixmeTop) {
    $('.fixme').css({
      position: 'fixed',
      top: '0',
      left: '0'
    });
  } else {
    $('.fixme').css({
      position: 'static'
    });
  }
});
@import url(css/bootstrap.min.css);
@import url(css/font-awesome.min.css);

/*  Min CSS  */

::selection {
  background-color: #352e7e;
  color: #fff;
}

* {
  margin: 0;
  padding: 0;
}

body {
  background-color: #181818;
  color: #dddddd;
  font-size: 14px;
}

.container {
  padding: 0;
}

.fixme {
  width: 100%;
}

.right {
  float: right;
  margin: 0 34px;
  padding: 0 15px;
}

a,
a:hover,
a:focus {
  font-size: 16px;
  outline: 0 solid transparent;
  text-decoration: none;
  transition-duration: 500ms;
}

ul,
ol {
  margin: 0;
}

ul li,
ol li {
  list-style: none;
}


/* Header Area CSS */

.header-area {
  position: relative;
  width: 100%;
  z-index: 1000;
}

.header-area .top-header-area {
  border-bottom: 1px solid white;
  height: 85px;
  padding-right: 70px;
  position: relative;
  width: 100%;
  z-index: 1;
}

.header-area .top-header-area .top-search-area {
  position: relative;
  z-index: 1;
}

.header-area .top-header-area .top-search-area form {
  position: relative;
  width: 255px;
  z-index: 1;
}

.header-area .top-header-area .top-search-area form input {
  background-color: #1a1a1a;
  border: none;
  border-radius: 10px;
  color: #ababab;
  font-size: 12px;
  font-style: italic;
  height: 42px;
  padding: 0 20px;
  position: relative;
  transition-duration: 500ms;
  width: 100%;
  z-index: 1;
}

.header-area .top-header-area .top-search-area form input:focus {
  box-shadow: 0 0 5px 1px rgba(0, 0, 0, .15);
}

.header-area .top-header-area .top-search-area form button {
  background-color: transparent;
  border: none;
  color: #555555;
  cursor: pointer;
  height: 42px;
  position: absolute;
  right: 0;
  top: 0;
  width: 50px;
  z-index: 10;
}

.header-area .top-header-area .top-search-area form button:hover,
.header-area .top-heaer-area .top-search-area form button:focus {
  box-shadow: none;
  color: #20d8da;
}

.header-area .top-header-area .login-area {
  position: relative;
  z-index: 1;
}

.header-area .top-header-area .login-area a {
  border-radius: 10px;
  color: #555555;
  display: block;
  font-size: 14px;
  font-weight: 400;
  height: 42px;
  line-height: 42px;
  margin-left: 10px;
  text-align: center;
  transition: background-color color ease-in-out 400ms;
  width: 160px;
}

.header-area .top-header-area .login-area a:hover {
  background-color: black;
  color: #f06040;
}

.p {
  color: green;
  padding: 20px 0;
}


/*  NavBar CSS  */

.header-area .fixme #main-navigation {
  background-color: #181818e6;
  height: 70px;
  padding: 20px 110px 0 110px;
  width: auto;
}

.header-area .fixme #main-navigation li {
  background-color: #181818e6;
  position: relative;
}

.header-area .fixme #main-navigation>li {
  float: left;
  margin-right: 5px;
}

.header-area .fixme #main-navigation li a {
  color: #f06040;
  display: block;
  font-weight: 500;
  line-height: 1em;
  padding: .5em;
  text-decoration: none;
}

.header-area .fixme #main-navigation li a:hover {
  color: #20d8da;
}

.header-area .fixme ul.folder-child {
  height: 0;
  left: 0;
  margin-top: 15px;
  opacity: 0;
  overflow: hidden;
  position: absolute;
  top: 100%;
  transition: all 200ms ease-in-out;
  visibility: hidden;
  width: 150px;
}

.header-area .fixme #main-navigation li:hover>ul.folder-child {
  border-bottom-left-radius: 25px;
  border-bottom-right-radius: 25px;
  height: 100px;
  opacity: 1;
  visibility: visible;
}

.hidden-caret {
  visibility: hidden;
}
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<body>
  <header class="header-area">

    <div class="top-header-area">
      <p style="float: left; font-size: 32px;">Logo Here</p>
      <div class="row h-100 align-items-center right">
        <div class="d-flex">
          <div class="d-flex">
            <div class="top-search-area">
              <form action="#" method="post">
                <input type="search" name="top-search" id="topSearch" placeholder="Search">
                <button type="submit" class="btn"><i class="fa fa-search"></i></button>
              </form>
            </div>
            <div class="login-area">
              <a href="#"><span>Login / Register</span> <i class="fa fa-lock" aria-hidden="true"></i></a>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="fixme">
      <ul id="main-navigation">
        <li><a href="#">Fill <i class="fa fa-caret-down hidden-caret"></i></a></li>
        <li>
          <a href="#">In <i class="fa fa-caret-down"></i></a>
          <ul class="folder-child">
            <li><a href="">a</a></li>
            <li><a href="">b</a></li>
            <li><a href="">c</a></li>
          </ul>
        </li>
        <li><a href="#">This <i class="fa fa-caret-down hidden-caret"></i></a>
        </li>
        <li>
          <a href="#">Here! <i class="fa fa-caret-down"></i></a>
          <ul class="folder-child">
            <li><a href="">d</a></li>
            <li><a href="">e</a></li>
            <li><a href="">f</a></li>
          </ul>
        </li>
        <li><a href="#">Contact <i class="fa fa-caret-down hidden-caret"></i></a></li>
      </ul>
    </div>

  </header>

  <p class="p">This is a header that has a search bar, a login/rigister button, and a sticky dropdown menu. the "sticky" part was easy to make, since it was an empty div with a "fixme" class. I made it sticky through JavaScript.
    <br>
    <p class="p">blabwerfbvinjerdvs reeruhjb fkjhebfdhvbehjdb uhcjdsb hjdfbvhjdfbvhjdbvcfrcx,hj nedreyireuei iedfkjnc dkfjcn dkjfc dkfjn dkfjn </p>
    <br>
    <br>
    <br>
    <br>
    <p>blabwerfbvinjerdvs reeruhjb fkjhebfdhvbehjdb uhcjdsb hjdfbvhjdfbvhjdbvcfrcx,hj nedreyireuei iedfkjnc dkfjcn dkjfc dkfjn dkfjn </p>
    <br>
    <br>
    <br>
    <br>
    <br>
    <p>blabwerfbvinjerdvs reeruhjb fkjhebfdhvbehjdb uhcjdsb hjdfbvhjdfbvhjdbvcfrcx,hj nedreyireuei iedfkjnc dkfjcn dkjfc dkfjn dkfjn </p>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <p>blabwerfbvinjerdvs reeruhjb fkjhebfdhvbehjdb uhcjdsb hjdfbvhjdfbvhjdbvcfrcx,hj nedreyireuei iedfkjnc dkfjcn dkjfc dkfjn dkfjn </p>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <p>blabwerfbvinjerdvs reeruhjb fkjhebfdhvbehjdb uhcjdsb hjdfbvhjdfbvhjdbvcfrcx,hj nedreyireuei iedfkjnc dkfjcn dkjfc dkfjn dkfjn </p>
    <br>
    <br>
    <br>
    <p>blabwerfbvinjerdvs reeruhjb fkjhebfdhvbehjdb uhcjdsb hjdfbvhjdfbvhjdbvcfrcx,hj nedreyireuei iedfkjnc dkfjcn dkjfc dkfjn dkfjn </p>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <p>blabwerfbvinjerdvs reeruhjb fkjhebfdhvbehjdb uhcjdsb hjdfbvhjdfbvhjdbvcfrcx,hj nedreyireuei iedfkjnc dkfjcn dkjfc dkfjn dkfjn </p>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
</body>

激活粘性标头后,文本将被吸到其下方。 当我添加边距或填充时,它仍然会执行。这与我的JavaScript有关,但我不知道在哪里。

这是我的笔:https://codepen.io/SynergyOfLife/pen/rPVYQW

这是怎么回事?

1 个答案:

答案 0 :(得分:1)

您需要在页面内容之前创建一个间隙,该间隙等于粘性标头的高度。

在下面的示例中,我将您的内容包装在main元素中,并调整了JS,以便当我们切换到粘性导航时,将顶部填充添加到main元素中

+ 16在粘性导航栏和文本的底部之间添加了一些缓冲,因此它们几乎没有接触。

var $fixme   = $( '.fixme' );
var fixmeTop = $fixme.offset().top;
var $main    = $( 'main' );
var $win     = $( window );

$win.scroll( function () {

  var atSticky = $win.scrollTop() >= fixmeTop;
  
  $fixme.toggleClass( 'sticky', atSticky );
  $main.css( 'padding-top', atSticky ? fixmeTop + 16 : 0 );
  
} );
@import url(css/bootstrap.min.css);
@import url(css/font-awesome.min.css);

/*  Min CSS  */
.sticky {
  position: fixed;
  top: 0;
  left: 0;
}

::selection {
  background-color: #352e7e;
  color: #fff;
}

* {
  margin: 0;
  padding: 0;
}

body {
  background-color: #181818;
  color: #dddddd;
  font-size: 14px;
}

.container {
  padding: 0;
}

.fixme {
  width: 100%;
}

.right {
  float: right;
  margin: 0 34px;
  padding: 0 15px;
}

a,
a:hover,
a:focus {
  font-size: 16px;
  outline: 0 solid transparent;
  text-decoration: none;
  transition-duration: 500ms;
}

ul,
ol {
  margin: 0;
}

ul li,
ol li {
  list-style: none;
}


/* Header Area CSS */

.header-area {
  position: relative;
  width: 100%;
  z-index: 1000;
}

.header-area .top-header-area {
  border-bottom: 1px solid white;
  height: 85px;
  padding-right: 70px;
  position: relative;
  width: 100%;
  z-index: 1;
}

.header-area .top-header-area .top-search-area {
  position: relative;
  z-index: 1;
}

.header-area .top-header-area .top-search-area form {
  position: relative;
  width: 255px;
  z-index: 1;
}

.header-area .top-header-area .top-search-area form input {
  background-color: #1a1a1a;
  border: none;
  border-radius: 10px;
  color: #ababab;
  font-size: 12px;
  font-style: italic;
  height: 42px;
  padding: 0 20px;
  position: relative;
  transition-duration: 500ms;
  width: 100%;
  z-index: 1;
}

.header-area .top-header-area .top-search-area form input:focus {
  box-shadow: 0 0 5px 1px rgba(0, 0, 0, .15);
}

.header-area .top-header-area .top-search-area form button {
  background-color: transparent;
  border: none;
  color: #555555;
  cursor: pointer;
  height: 42px;
  position: absolute;
  right: 0;
  top: 0;
  width: 50px;
  z-index: 10;
}

.header-area .top-header-area .top-search-area form button:hover,
.header-area .top-heaer-area .top-search-area form button:focus {
  box-shadow: none;
  color: #20d8da;
}

.header-area .top-header-area .login-area {
  position: relative;
  z-index: 1;
}

.header-area .top-header-area .login-area a {
  border-radius: 10px;
  color: #555555;
  display: block;
  font-size: 14px;
  font-weight: 400;
  height: 42px;
  line-height: 42px;
  margin-left: 10px;
  text-align: center;
  transition: background-color color ease-in-out 400ms;
  width: 160px;
}

.header-area .top-header-area .login-area a:hover {
  background-color: black;
  color: #f06040;
}

.p {
  color: green;
  padding: 20px 0;
}


/*  NavBar CSS  */

.header-area .fixme #main-navigation {
  background-color: #181818e6;
  height: 70px;
  padding: 20px 110px 0 110px;
  width: auto;
}

.header-area .fixme #main-navigation li {
  background-color: #181818e6;
  position: relative;
}

.header-area .fixme #main-navigation>li {
  float: left;
  margin-right: 5px;
}

.header-area .fixme #main-navigation li a {
  color: #f06040;
  display: block;
  font-weight: 500;
  line-height: 1em;
  padding: .5em;
  text-decoration: none;
}

.header-area .fixme #main-navigation li a:hover {
  color: #20d8da;
}

.header-area .fixme ul.folder-child {
  height: 0;
  left: 0;
  margin-top: 15px;
  opacity: 0;
  overflow: hidden;
  position: absolute;
  top: 100%;
  transition: all 200ms ease-in-out;
  visibility: hidden;
  width: 150px;
}

.header-area .fixme #main-navigation li:hover>ul.folder-child {
  border-bottom-left-radius: 25px;
  border-bottom-right-radius: 25px;
  height: 100px;
  opacity: 1;
  visibility: visible;
}

.hidden-caret {
  visibility: hidden;
}
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<header class="header-area">

  <div class="top-header-area">
    <p style="float: left; font-size: 32px;">Logo Here</p>
    <div class="row h-100 align-items-center right">
      <div class="d-flex">
        <div class="d-flex">
          <div class="top-search-area">
            <form action="#" method="post">
              <input type="search" name="top-search" id="topSearch" placeholder="Search">
              <button type="submit" class="btn"><i class="fa fa-search"></i></button>
            </form>
          </div>
          <div class="login-area">
            <a href="#"><span>Login / Register</span> <i class="fa fa-lock" aria-hidden="true"></i></a>
          </div>
        </div>
      </div>
    </div>
  </div>
  <div class="fixme">
    <ul id="main-navigation">
      <li><a href="#">Fill <i class="fa fa-caret-down hidden-caret"></i></a></li>
      <li>
        <a href="#">In <i class="fa fa-caret-down"></i></a>
        <ul class="folder-child">
          <li><a href="">a</a></li>
          <li><a href="">b</a></li>
          <li><a href="">c</a></li>
        </ul>
      </li>
      <li><a href="#">This <i class="fa fa-caret-down hidden-caret"></i></a>
      </li>
      <li>
        <a href="#">Here! <i class="fa fa-caret-down"></i></a>
        <ul class="folder-child">
          <li><a href="">d</a></li>
          <li><a href="">e</a></li>
          <li><a href="">f</a></li>
        </ul>
      </li>
      <li><a href="#">Contact <i class="fa fa-caret-down hidden-caret"></i></a></li>
    </ul>
  </div>

</header>

<main>
  <p class="p">This is a header that has a search bar, a login/rigister button, and a sticky dropdown menu. the "sticky" part was easy to make, since it was an empty div with a "fixme" class. I made it sticky through JavaScript.
    <br>
    <p class="p">blabwerfbvinjerdvs reeruhjb fkjhebfdhvbehjdb uhcjdsb hjdfbvhjdfbvhjdbvcfrcx,hj nedreyireuei iedfkjnc dkfjcn dkjfc dkfjn dkfjn </p>
    <br>
    <br>
    <br>
    <br>
    <p>blabwerfbvinjerdvs reeruhjb fkjhebfdhvbehjdb uhcjdsb hjdfbvhjdfbvhjdbvcfrcx,hj nedreyireuei iedfkjnc dkfjcn dkjfc dkfjn dkfjn </p>
    <br>
    <br>
    <br>
    <br>
    <br>
    <p>blabwerfbvinjerdvs reeruhjb fkjhebfdhvbehjdb uhcjdsb hjdfbvhjdfbvhjdbvcfrcx,hj nedreyireuei iedfkjnc dkfjcn dkjfc dkfjn dkfjn </p>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <p>blabwerfbvinjerdvs reeruhjb fkjhebfdhvbehjdb uhcjdsb hjdfbvhjdfbvhjdbvcfrcx,hj nedreyireuei iedfkjnc dkfjcn dkjfc dkfjn dkfjn </p>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <p>blabwerfbvinjerdvs reeruhjb fkjhebfdhvbehjdb uhcjdsb hjdfbvhjdfbvhjdbvcfrcx,hj nedreyireuei iedfkjnc dkfjcn dkjfc dkfjn dkfjn </p>
    <br>
    <br>
    <br>
    <p>blabwerfbvinjerdvs reeruhjb fkjhebfdhvbehjdb uhcjdsb hjdfbvhjdfbvhjdbvcfrcx,hj nedreyireuei iedfkjnc dkfjcn dkjfc dkfjn dkfjn </p>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <p>blabwerfbvinjerdvs reeruhjb fkjhebfdhvbehjdb uhcjdsb hjdfbvhjdfbvhjdbvcfrcx,hj nedreyireuei iedfkjnc dkfjcn dkjfc dkfjn dkfjn </p>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br></main>