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;}
这将使它看起来像动画