[英]css transition at sticky menu doesn`t work
我在粘性菜單上遇到了動畫問題。 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");
}
});
和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;
}
您需要在時間后面添加一個unit
。 0.3
無效,必須為0.3s
或300ms
。 然后可以使用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>
您沒有進行過渡的原因是因為您沒有定義要過渡到的初始屬性。 嘗試這個。
.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;
}
希望能成功!
這應該解決。 保留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');
}
});
或者,您可以嘗試添加時間單位,如要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.