将底栏固定在网页底部的好方法 bootstrap的navbar-static-bttom

本文介绍了如何使用Bootstrap和jQuery实现网页底部元素的固定及滚动跟随效果,详细讲解了navbar-static-bottom和navbar-fixed-bottom类的区别,同时提供了一个利用jQuery动态调整底部元素位置的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

navbar-static-bottom这个类可以使其固定在网页底部,并且不随网页滚动

<footer class="navbar-static-bottom">
   testteset
</footer>

如果需要随网页滚动,则使用navbar-fixed-bottom

<footer class="navbar-fixed-bottom">
   testteset
</footer>

备注:要记得加上bootstrap的CDN,否则会失败

<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>


除此之外,再介绍一种jQuery实现的方法,亲测可用(我当时用的就是jQuery实现的方案)

  • css
footer {
    height: 70px;
    padding: 5px;
    bottom: 0px;
    width: 100%;
    font-size: x-small;
    background-color: lightgrey !important;
}
  • html
<body>
....
<footer id="footer">testtest</footer>
</body>
  • js
 $(window).bind("load", function () {
    var footerHeight = 0, footerTop = 0, $footer = $("#footer");
    positionFooter();
    function positionFooter() {
        footerHeight = $footer.height();
        footerTop = ($(window).scrollTop() + $(window).height() - footerHeight) + "px";
        if (($(document.body).height() + footerHeight) < $(window).height()) {
            $footer.css({position: "absolute"}).stop().animate({top: footerTop});
        } else {
            $footer.css({position: "static"});
        }
    }

    $(window).scroll(positionFooter).resize(positionFooter);
});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值