简体   繁体   English

导航不会在滚动时更改大小

[英]Nav won't change size upon scrolling

So I found this jquery code to make my nav change its size when a user begins to scroll. 因此,当用户开始滚动时,我发现此jquery代码使我的nav更改其大小。 It doesn't appear to be working, though... 它似乎不起作用,但是...

$(function(){
    $('.nav').data('size','big');
});

$(window).scroll(function(){
    var $nav = $('.nav');
    if ($('body').scrollTop() > 0) {
        if ($nav.data('size') == 'big') {
            $nav.data('size','small').stop().animate({
                height:'60px'
            }, 600);
        }
    } else {
        if ($nav.data('size') == 'small') {
            $nav.data('size','big').stop().animate({
                height:'40px'
            }, 600);
        }  
    }

Here is all the code: http://codepen.io/DerekDev/pen/Bydgpz 这是所有代码: http : //codepen.io/DerekDev/pen/Bydgpz

So if any of you have a solution, it would be greatly appreciated. 因此,如果您有任何解决方案,将不胜感激。 Thanks :) 谢谢 :)

try this: 尝试这个:

$(function() {
    $(window).scroll(function() {    
    var nav = $('.nav');
    if ($(window).scrollTop() >= 0) {
      if(!nav.hasClass("smallNav")) {
        nav.hide();
        nav.removeClass('bigNav').addClass("smallNav").fadeIn( "slow");
      }
    } else {
      if(!nav.hasClass("bigNav")) {
        nav.hide();
        nav.removeClass("sml-logo").addClass('bigNav').fadeIn( "slow");
      }
    }

  });
});

and CSS rules: 和CSS规则:

.bigNav{
height:60px;
}
.smallNav{
height:40px;
}

Firstly, you want to check the window scrollTop, not the body 首先,您要检查window scrollTop,而不是body

Secondly, you want to change the height of the UL, not the .nav element, and you seem to have the order mixed up, you probably want 40px when it's small, and 60px when it's big ! 其次,您想更改UL的高度,而不是.nav元素,并且似乎混合了顺序,小的尺寸可能要40px ,大的尺寸要60px

$(window).scroll(function(){
    var $nav = $('.nav');
    if ($(window).scrollTop() > 0) {
      console.log($nav.data('size'))
        if ($nav.data('size') == 'big') {
            $nav.data('size','small').find('ul').stop().animate({
                height:'40px'
            }, 600);
        }
    } else {
        if ($nav.data('size') == 'small') {
            $nav.data('size','big').find('ul').stop().animate({
                height:'60px'
            }, 600);
        }  
    }
});

PEN 钢笔

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

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