简体   繁体   English

粘滞菜单上的CSS过渡不起作用

[英]css transition at sticky menu doesn`t work

I have a problem with animation at sticky menu. 我在粘性菜单上遇到了动画问题。 CSS is changed, but not with transition. CSS已更改,但未进行过渡。 Anybody has an idea why? 有人知道为什么吗?

$(window).scroll(function () {
    if ($(this).scrollTop() > 50) {
        $('.navbar-fixed-top').addClass("navbar-fixed-top-sticky");
    } else {
        $('.navbar-fixed-top').removeClass("navbar-fixed-top-sticky");
    }
});

And CSS: 和CSS:

.navbar-fixed-top {
  transition: 0.3 all ease;
  -webkit-transition: 0.3 all ease;
}
.navbar-fixed-top.navbar-fixed-top-sticky {
  background: #601a36;
  height: 80px;
  transition: 0.3 all ease-in-out;
  -webkit-transition: 0.3 all ease-in-out;
}

You need to add a unit behind your timings. 您需要在时间后面添加一个unit 0.3 is not valid, it has to be something like 0.3s or 300ms . 0.3无效,必须为0.3s300ms The background-color would work then, but for a height transition you need to add a default value in your .navbar-fixed-top css class too. 然后可以使用background-color ,但是要进行height转换,您还需要在.navbar-fixed-top css类中添加默认值。

 $(window).scroll(function() { if( $(this).scrollTop() > 50 ) { $('.navbar-fixed-top').addClass("navbar-fixed-top-sticky"); } else { $('.navbar-fixed-top').removeClass("navbar-fixed-top-sticky"); } }); 
 /* --- just for demo --- */ .navbar-fixed-top { position: fixed; width: 100%; line-height: 20px; background: #ccc; } .navbar-fixed-top-sticky { line-height: 80px; } .content { height: 1000px; } /* --- just for demo --- */ .navbar-fixed-top { height: 20px; /* default height */ transition: 0.3s all ease; /* added unit */ -moz-transition: 0.3s all ease; /* added unit */ -webkit-transition: 0.3s all ease; /* added unit */ } .navbar-fixed-top-sticky { background: #601a36; height: 80px; transition: 0.3s all ease-in-out; /* added unit */ -moz-transition: 0.3s all ease-in-out; /* added unit */ -webkit-transition: 0.3s all ease-in-out; /* added unit */ } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="navbar-fixed-top">.navbar-fixed-top</div> <div class="content"></div> 

The reason you have no transition happening is because you don't have an initial property defined for it to transition to. 您没有进行过渡的原因是因为您没有定义要过渡到的初始属性。 Try this. 尝试这个。

.navbar-fixed-top {
   transition: 0.3s all ease;
   -webkit-transition: 0.3s all ease;

   background-color: transparent;
   height: 0;
}

.navbar-fixed-top.navbar-fixed-top-sticky {
   background-color: #601a36;
   height: 80px;
}

Hope that works! 希望能成功!

This should sort it out. 这应该解决。 Keep the CSS, only exclude the transition parts. 保留CSS,仅排除transition部分。

$(window).scroll(function () {
           if ($(this).scrollTop() > 50) {
              $('.navbar-fixed-top').addClass("navbar-fixed-top-sticky").fadeIn('slow');
           } else {
        $('.navbar-fixed-top').removeClass("navbar-fixed-top-sticky").fadeOut('slow');

    }
         });

or you can try adding time units, as you should to your transitions : 或者,您可以尝试添加时间单位,如要transitions

.navbar-fixed-top {
       transition: 0.3s all ease;
       -webkit-transition: 0.3s all ease;
    }

   .navbar-fixed-top.navbar-fixed-top-sticky {
       background: #601a36;
       height: 80px;
       transition: 0.3s all ease-in-out;
       -webkit-transition: 0.3s all ease-in-out;
    }

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

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