简体   繁体   English

jQuery-固定的导航菜单,其高度根据滚动而变化

[英]jQuery - Fixed navigation menu with variable height according to the scroll

I'm fairly new in jQuery and it's been a few days that I'm trying to make a top-fixed navigation menu in my page with variable height according to the scroll, but so far it has been a struggle to figure out how this works. 我是jQuery的新手,已经有几天我试图在页面中制作一个顶部固定的导航菜单,其高度根据滚动条而变化,但是到目前为止,要弄清楚如何做到这一点一直很困难。作品。 I managed to kinda simulate the effect with just CSS Transition but it's not what I want. 我设法仅用CSS Transition来模拟效果,但这不是我想要的。

Here a perfect exemple of what I'm trying to accomplish: www.bulo.com and here: http://d.pr/i/fqaB 这是我要完成的工作的完美范例:www.bulo.com,然后在这里: http ://d.pr/i/fqaB

I examined the code of Bulo.com, and find it really hard to comprehend, since the part that apparently make it all works, it's inside jQuery.js and it's all compressed with no spaces or linebreaks whatsoever, making me go bananas. 我检查了Bulo.com的代码,发现它真的很难理解,因为显然可以使它全部工作的部分都在jQuery.js内,并且都被压缩而没有空格或换行符,这使我成为香蕉。

I stripped down what I thought it's important: 我删除了我认为重要的内容:

HTML: HTML:

<header class="mod-header" data-target-padding="40">
  <div class="container">
    <nav class="navigation">
      <ul>
          <li class="nav-item">
            <a href="collections.html">Collections</a>
          </li>
          <li class="nav-item">
            <a href="products.html">Products</a>
          </li>
          <li class="nav-item">
            <a href="for-sale.html">For sale</a>
          </li>
      </ul>
    </nav>
  </div>
</header>

CSS: CSS:

.mod-header {
background-color: #FFFFFF;
border-bottom: 1px solid #DDDDDD;
z-index: 901;
}
.mod-header .navigation {
text-align: center;
}
.mod-header .navigation ul {
display: inline-block;
margin: 0;
overflow: visible;
padding: 0;
text-align: center;
}
.mod-header .navigation li {
display: block;
float: left;
height: 18px;
margin: 0;
overflow: visible;
padding: 64px 0;
position: relative;
z-index: 910;
}
.mod-header .navigation li a {
color: #171617;
display: inline-block;
font-family: 'Droid Sans',sans-serif;
font-size: 18px;
height: 18px;
margin: 0 20px;
text-align: center;
text-decoration: none;
}
.mod-header.mod-header-fixed {
left: 0;
margin: 0;
position: fixed;
top: 0;
width: 100%;
}

Not going to post the jquery.js files because it's freaking gigantic, but you can easily see it for yourself with Firebug or Inspect Element in Safari. 不会发布jquery.js文件,因为它异常庞大,但是您可以通过Safari中的Firebug或Inspect Element轻松地自己查看它。

So, can anyone explain it to me? 那么,有人可以向我解释吗?

Thanks in advance. 提前致谢。

You would just need to add a class mod-header-fixed to that navigation bar just when the page 70px scrolled down and then remove it when the page is scrolled back. 您只需要在页面向下滚动70px时向该导航栏添加mod-header-fixed类,然后在页面向后滚动时将其删除。

The following jQuery example would easily catch that kind of event and do what you want: 以下jQuery示例将轻松捕获此类事件并执行您想要的操作:

$(document).ready(function(){
     $(window).scroll(function(){
         if ($(window).scrollTop() > 70){
            $(".navigation").addClass("mod-header-fixed");
         } else if ($(window).scrollTop() < 70) {
            $(".navigation").removeClass("mod-header-fixed");
         }
    });
});

jsFiddle example jsFiddle示例

Read more about .scrollTop() method on jQuery API Documentation . 阅读jQuery API Documentation上有关.scrollTop()方法的更多信息。

EDIT 1: 编辑1:

You said that you can't read compressed code? 您说您无法阅读压缩代码? Please, just copy/paste the code you can't read in to JavaScript Beautifier and get read-friendly version of it! 请,只需将您无法阅读的代码复制/粘贴到JavaScript Beautifier中,并获得易于阅读的版本!

EDIT 2: 编辑2:

If you want the object to change .height() you could manipulate padding property using .animate() method: 如果希望对象更改.height() ,则可以使用.animate()方法来处理padding属性:

$(document).ready(function(){
     $(window).scroll(function(){
         if ($(window).scrollTop() > 70){
             $(".navigation").addClass("mod-header-fixed");
             $(".mod-header .navigation li").stop().animate({ 
                padding: "45px 0"
             }, 600 );
             $(".mod-header .navigation li").stop().animate({ 
                padding: "45px 0"
             }, 600 );
         } else if ($(window).scrollTop() < 70) {
             $(".navigation").removeClass("mod-header-fixed");
             $(".mod-header .navigation li").stop().animate({ 
                padding: "15px 0"
             }, 600 );
             $(".mod-header .navigation li").stop().animate({ 
                padding: "15px 0"
             }, 600 );
         }
     });
 });

jsFiddle example jsFiddle示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM