【发布时间】:2014-09-20 18:02:02
【问题描述】:
我想问一下是否有办法使用 jQuery animate() 方法在窗口滚动时为水平导航栏的顶部属性设置动画。
这是我使用的代码:
window.addEventListener("scroll", function() {
if (window.scrollY > 200) {
$('#navbar').css({top:"100px"});
}
else {
$('#navbar').css({top:"0px"});
}
},false);
CSS:
#navbar{
top:0;
position:fixed;
transition: top 0.5s;
}
当您向下滚动 200 像素时,导航栏的顶部位置从 0 更改为 100 像素; 这很好用,但是如果我更改方法并使用 .animate 而不是 .css,
$('#navbar').animate({top:"100px"});
它停止工作。任何想法为什么?
【问题讨论】:
-
这可以通过使用 CSS3 过渡来实现。 stackoverflow.com/questions/20185001/…
-
请查看更新后的我的第二个链接。
标签: javascript jquery css