jQuery:滚动后恢复为默认值

标签 jquery html css

我创建了一个固定在页面底部的页脚。当用户向下滚动时,页脚边距底部更改为 20px,颜色从黑色变为白色。在这里,我使用了以下 jQuery:

 $(document).ready(function() {
        $(window).scroll(function() {
            var $this = $(this);
            if ($this.scrollTop() >= 10) {
                $("footer").animate({
                    marginBottom: "30px",
                    backgroundColor: "fff"
                }, 2000);
            }
        });
});

当用户滚动回顶部时,页脚应返回到屏幕底部,颜色应再次从白色变为黑色。我尝试使用 else 语句,但没有成功。

找到它:

$(document).ready(function() {
 $( window ).scroll(function() {
  var $this = $(this);
   if ($this.scrollTop() <= 5) {
   $("footer").stop().animate({marginBottom: "0px", color: "fff", backgroundColor: "000"}, 1000);
   } else if ($this.scrollTop() >= 5) {
   $("footer").stop().animate({marginBottom: "30px", color: "000", backgroundColor: "fff"}, 1000);
        } 
    });
});

最佳答案

如果可行,试试这个:

.footerback{
      margin-bottom:0;
      background-color:#000000;
    }

$(document).ready(function() {
    $(window).scroll(function() {
        var $this = $(this);
        if ($this.scrollTop() >= 10) {
            $("footer").animate({
                marginBottom: "30px",
                backgroundColor: "fff"
            }, 2000);
        }
       else{
        $("footer").addClass("footerback");
       }
    });
});

关于jQuery:滚动后恢复为默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24849298/

相关文章:

javascript - 使 Bootstrap 轮播导航仅在悬停时显示

html - 如何在移动 View 中制作响应式表格

javascript - 动画 Bootstrap 轮播标题和按钮

html - 为什么 IE9、FF、opera 比 chrome 多了 20% 以上的边距?

jquery - < IE 9 的背景大小和位置替代方案

javascript - jQuery UI - 在外部单击时关闭对话框

jquery - 谷歌地图只加载了一半

javascript - 在 jQuery 中使用模型中的 bool

html - 如何使用 laravel ajax 将另一个文件中的模态显示到我的 html 中

css - 在父div内显示div水平和垂直居中?