javascript - 取消粘附在屏幕底部的 div

标签 javascript jquery css sticky

目前,我的侧边栏比浏览器窗口长,贴在屏幕底部。当页脚出现时,我需要它停止粘在屏幕底部,所以它不会覆盖页脚。

我还想让我的页眉贴在屏幕顶部,但不想让侧边栏覆盖它。应该注意的是,我已经尝试使用 jQuery 的 hcsticky 插件,但我根本无法让它工作。

$(window).load(function(){ 

var $sidebar = $("#sidebar"),
$thefoot = $("#thefoot"),
$window = $(window),
offset = $sidebar.offset(),
sbBottom = Math.abs($window.height() - (offset.top + $sidebar.height())),
prevScrollTop = 0;

$window.scroll(function() {

if (prevScrollTop < $window.scrollTop()) {
    $sidebar.removeClass('fixedTop');
    if ($window.scrollTop() > (sbBottom + 12)) {
        $sidebar.addClass('fixedBtm');
    } else {
        $sidebar.removeClass('fixedBtm');
    }
} else {

    $sidebar.removeClass('fixedBtm');
    if ($window.scrollTop() > sbBottom) {
        $sidebar.addClass('fixedTop');
    } else {
        $sidebar.removeClass('fixedTop');
    }

} });

});

#sidebar {
    width: 300px;
    margin-bottom: 10px;
    overflow: hidden;
    margin: 0 auto;
    float: left;
    clear: right;
}

.fixedBtm {
    margin-left: 660px !important;
    position: fixed;
    bottom: 0;
}

.fixedTop {
    margin-left: 660px !important;
    position: fixed;
    bottom: 0;
}

.footBtm {
    bottom: 350px;
}

#thefoot {
    background-color: #5774F2;
    clear: both;
    background-color: #ffffff;
    background-repeat: no-repeat;
    background-position: center;
    background-image: url("../images/footer.png");
    height: 340px;
    width: 100%;
}

最佳答案

我没有看到 CSS,基本上这是根据您提供的代码寻找的内容。

在 CSS 文件中,除了您可能拥有的其他属性之外,将这些属性添加到适当的类中。

/*for header element*/
.header{
  position:fixed;
  display: block;
  clear: both;
}

/*for footer element*/
.footer{
  display: block;
  clear: both;
}

/* for sidebar element*/
.sidebar{
  float:left;
}

关于javascript - 取消粘附在屏幕底部的 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22237169/

相关文章:

JavaScript 计数器

javascript - Jquery UI - 在一个 DIV 到另一个 Div 之间可排序,在 DIV 内不可排序

javascript - 为什么顶部没有正确对齐?

css - 如何编写 cssSelector 来查找保存按钮

javascript - Greasemonkey setTimeout 和 setInterval 不触发

html - IE10 下拉菜单 CSS 不工作

javascript - 将客户端数据发送到另一台计算机

javascript - 如何让 vim ctags 适用于现代 javascript

javascript - 如何检查多个数组上的键值对并使用 javascript 进行过滤?

javascript - 如何从cropper js获取裁剪后的图像?