Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/420.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 删除和添加页边空白顶部,删除和添加背景图像到页眉-滚动-_Javascript_Html_Css - Fatal编程技术网

Javascript 删除和添加页边空白顶部,删除和添加背景图像到页眉-滚动-

Javascript 删除和添加页边空白顶部,删除和添加背景图像到页眉-滚动-,javascript,html,css,Javascript,Html,Css,当我向下滚动时,我尝试将背景图像添加到页眉,并删除页边空白顶部。我想添加一些动画,但它没有像我计划的那样工作 $(document).scroll(function () { if ($(window).scrollTop() > 450) { //$(".header").css("margin-top", "0"); <- This works perfectly but its not animated $(".header").css(

当我向下滚动时,我尝试将背景图像添加到页眉,并删除页边空白顶部。我想添加一些动画,但它没有像我计划的那样工作

$(document).scroll(function () {
    if ($(window).scrollTop() > 450) {
        //$(".header").css("margin-top", "0"); <- This works perfectly but its not animated
        $(".header").css("background-image", "url('/images/header.jpg')");
        $(".header").animate({'margin-top': 0}, "slow");
    } else if ($(window).scrollTop() < 450) {
        // $(".header").css("margin-top", "40");<- This works perfectly but its not animated
        $(".header").animate({'margin-top': 40}, "slow");
        $(".header").css("background-image", "none");
    }
});
$(文档)。滚动(函数(){
如果($(窗口).scrollTop()>450){

//$(“.header”).css(“页边空白顶部”、“0”);您可以将css添加到进行每次更改转换的页眉中。在css文件中添加
.header{transition:1s ease;margin top:0;}
这将使它看起来像动画