当我滚动时,我编写了代码以在导航栏扩展合同中创建徽标,就像Wall Street Journal网站的工作原理一样。但是,当我滚动它现在导致徽标和导航栏内的其他元素摆动/振动。
Jquery的:
$(window).on('scroll', function () {
var scrollTop = $(window).scrollTop();
if (scrollTop > 50) {
$('#logo').stop().animate({height: "50px"},100);
$('#nav').css({height: "110px"});
}
else {
$('#logo').stop().animate({height: "70px"},100);
$('#nav').css({height: "130px"});
}
});
CSS:
#logo {
height: 70px;
width: auto;
margin: auto;
display: block;
margin: auto;
}
#nav{
display: block;
max-width: 100%;
height: 120px;
position: relative;
margin: 0;
background-color: rgba(249,249,249,1);
font-family: MyriadPro-RegularImport;
}
答案 0 :(得分:1)
我建议使用jQuery添加一个类,然后使用CSS过渡动画:
$('#logo').addClass('scrolled');
CSS:
#logo {
height: 70px;
transition: all 200ms ease-out; // using 'all' so that all properties are animated
}
#logo.scrolled {
height: 50px;
}
CSS动画将更顺畅,更容易排除故障。