【问题标题】:How to make nav bar stick when scroll [duplicate]滚动时如何使导航栏粘住[重复]
【发布时间】:2017-08-12 08:30:39
【问题描述】:

我在浏览器窗口的底部,所有 div 元素的上方和外部放置了一个导航栏。当用户滚动并到达顶部时,我试图让导航栏粘在窗口的顶部..

我已尝试执行以下操作,但似乎无法使其正常工作;还在线尝试了其他一些解决方案,但没有运气。有没有人有任何想法?

      
    $(document).ready(function() {
        // Cache selectors for faster performance.
        var $window = $(window),
            $mainNavBar = $('#navBar'),
            $mainNavBarAnchor = $('#navBarAnchor');
    
        // Run this on scroll events.
        $window.scroll(function() {
            var window_top = $window.scrollTop();
            var div_top = $mainNavBarAnchor.offset().top;
            if (window_top > div_top) {
                // Make the div sticky.
                $mainNavBar.addClass('stick');
                $mainNavBarAnchor.height($mainNavBar.height());
            }
            else {
                // Unstick the div.
                $mainNavBar.removeClass('stick');
                $mainNavBarAnchor.height(0);
            }
        });
 });
    
    nav { height: 75px; width: 100%; position: absolute; bottom: 0; margin 0; padding 0; float: none; background-color: none; border: 0px solid #000; z-index: 100; color: #fff;}

ol { margin: auto; width: auto; height: auto; text-align: center; background-color: none; margin-top: 20px;}
ol li { display: inline-block; font-family: 'dinregular'; font-size: 1.6em; width: auto; margin: auto;  background-color: none; color: #fff }
ol li:nth-child(1) { margin-right: 25px; margin-left: 0; }
ol li:nth-child(2) { margin-right: 25px; margin-left: 0;}
ol li:nth-child(3) { margin-right: 25px; margin-left: 0; }
    
.stick {
  position: fixed;
  top: 0;
  background-color: white; 
 }
    <script src="https://code.jquery.com/jquery-1.11.0.js"></script>


    <div id="navBarAnchor"></div>
    <nav id="navbar">
      <ol>
       <a href="#About"><li>About</li> </a>
       <a href="#Services"><li>Services</li> </a>
       <a href="#Work"><li>Work</li> </a>
       <a href="#Contact"><li>Contact</li> </a>
      </ol>
    </nav> 

【问题讨论】:

标签: javascript jquery html css


【解决方案1】:

你先得到导航的位置,这样你才能得到“触发”位置this will be different on other screen resolutions

然后你只需监听窗口的滚动事件并添加/删除一个类来修复导航栏。

看看我的 jsFiddle:https://jsfiddle.net/xopq0r2h/1/

HTML

<div class="content">
  CONTENT 0
</div>
<div class="navigation">
<a>Link 1</a>
<a>Link 2</a>
<a>Link 3</a>
</div>
<div class="content">
  CONTENT 1
</div>
<div class="content">
  CONTENT 2
</div>
<div class="content">
  CONTENT 3
</div>

jQuery

$(function()
{
    var navigationYPosition = $('.navigation').position().top;
     $(window).scroll(function () { 
   var $navigation = $('.navigation');
    if ($(window).scrollTop() > 400) {
      $navigation.addClass('fixed-top');
    }
    if ($(window).scrollTop() < 401) {
      $navigation.removeClass('fixed-top');
    }
  });
});

CSS

.navigation{
  margin: 0;
  width: 100%;
  min-height: 32px;
  background-color: #000;
  padding: 5px;
}

.content{
  height: 400px;
  border: solid 1px #f0f;
}

.fixed-top{
  position: fixed;
  top: 0;
}

a{
  margin-left: 3px;
  background-color: #fff;
}

【讨论】:

    【解决方案2】:

    window.scrollTop 为> 时,您可以只使用add/removeposition: fixed 进行类> 然后偏移元素的顶部或在这种情况下为nav。您还可以在调整大小时重新计算偏移顶部。

    var nav = $('nav'), w = $(window), sTop = nav.offset().top
    
    w.resize(function() {
      sTop = nav.offset().top
    })
    
    w.scroll(function() {
      if(w.scrollTop() > sTop) {
        nav.addClass('fixed')
      } else {
        nav.removeClass('fixed')
      }
    })
    .content {
      height: 100vh;
    }
    nav {
      width: 100%;
    }
    ol {
      display: flex;
      justify-content: space-between;
      padding: 0;
      border: 1px solid black;
      margin: 0;
    }
    body {
      height: 2500px;
      margin: 0;
    }
    .fixed {
      position: fixed;
      top: 0;
      left: 0;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="content"></div>
    <nav id="navbar">
      <ol>
       <a href="#About"><li>About</li> </a>
       <a href="#Services"><li>Services</li> </a>
       <a href="#Work"><li>Work</li> </a>
       <a href="#Contact"><li>Contact</li> </a>
      </ol>
    </nav>

    【讨论】:

    • JavaScript、HTML 和 CSS 中的金牌徽章,您认为不会有重复吗?
    【解决方案3】:

    我想你可能正在寻找这样的东西:

    var header = $("#navbar");
    $(window).scroll(function() {
      var scroll = $(window).scrollTop();
      if (scroll >= window.innerHeight) {
        header.addClass("fixed");
      } else {
        header.removeClass("fixed");
      }
    });
    #navbar.fixed {
      position: fixed;
      top: 0;
    }
    
    nav {
      height: 100px;
      width: 100%;
      position: absolute;
      bottom: 0;
      margin 0;
      padding 0;
      float: none;
      background-color: white;
      border: 0px solid #000;
      z-index: 100;
      color: #fff;
    }
    
    .container {
      padding-bottom: 50px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    
    <nav id="navbar">
      <ol>
        <a href="#About">
          <li>About</li>
        </a>
        <a href="#Services">
          <li>Services</li>
        </a>
        <a href="#Work">
          <li>Work</li>
        </a>
        <a href="#Contact">
          <li>Contact</li>
        </a>
      </ol>
    </nav>
    
    <div class="container">
      What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type
      specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently
      with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using
      Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default
      model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). Where does it come from? Contrary
      to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of
      the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et
      Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
      The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions
      from the 1914 translation by H. Rackham. What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley
      of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing
      Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when
      looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page
      editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and
      the like). Where does it come from? Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney
      College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32
      and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..",
      comes from a line in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original
      form, accompanied by English versions from the 1914 translation by H. Rackham. What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
      when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with
      the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Why do we use it? It is a long established fact that a reader will be distracted
      by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop
      publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes
      on purpose (injected humour and the like). Where does it come from? Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock,
      a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem
      Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem
      Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero
      are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham. What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's
      standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
      It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Why do we use it? It is a long established
      fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making
      it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over
      the years, sometimes by accident, sometimes on purpose (injected humour and the like). Where does it come from? Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making
      it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature,
      discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during
      the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from
      "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham. What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesetting
      industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic
      typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
      Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to
      using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their
      infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). Where does it come from? Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of
      classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through
      the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise
      on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested.
      Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham. What is Lorem Ipsum? Lorem Ipsum is simply dummy text
      of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries,
      but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker
      including versions of Lorem Ipsum. Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution
      of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many
      web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). Where does it come from? Contrary to popular belief, Lorem Ipsum is not simply random text. It
      has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum
      passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in
      45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s
      is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.
    </div>

    【讨论】:

      猜你喜欢
      • 2017-06-04
      • 1970-01-01
      • 2018-12-08
      • 2019-03-08
      • 1970-01-01
      • 1970-01-01
      • 2019-05-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多