Javascript Jquery:将导航栏设置为scoll顶部的动画

Javascript Jquery:将导航栏设置为scoll顶部的动画,javascript,jquery,Javascript,Jquery,我想设置我的导航栏的动画以设置页面滚动上的top:0。现在我的导航在滑块图像之后 $(document).ready(function() { $(window).scroll(function() { if ($(this).scrollTop() === 100) { // this refers to window $('.navbar').animate('top': 0); } else { $('

我想设置我的
导航栏的动画
以设置页面滚动上的
top:0
。现在我的导航在滑块图像之后

$(document).ready(function() {
    $(window).scroll(function() {
        if ($(this).scrollTop() === 100) { // this refers to window
            $('.navbar').animate('top': 0);
        } else {
            $('.navbar').animate('top': 300);
        }
    });
})

但此代码不起作用。

Jquery animate函数错误

$('.navbar').animate({top: "0px"});
$('.navbar').animate({top: "300px"});
在css中,需要有固定或绝对位置