开发者社区> 问答> 正文

html中元素定位问题

如果希望footer区域永远定位在浏览器底部,有什么比较好的实现方法吗?
一般的footer区域都是根据页面的内容自动撑大,当内容不够时footer会飞上去导致页面看起来
很丑!

展开
收起
a123456678 2016-03-26 10:32:04 1922 0
1 条回答
写回答
取消 提交回答
  • 如果你确定一定要

    footer区域永远定位在浏览器底部
    的话:

    footer {

    position: fixed;
    left: 0;
    bottom: 0;
    z-index: 999;
    width: 100%;
    ...

    }
    PS. 额外赠送根据页面内容是否超出一屏来判断是否要让 footer 固定在页面底部:

    /**

    • Detect whether the element has scrollbar
    • @param {HTMLElement} elem
    • @return {Boolean} true: has scrollbar; false: hasn't
      */

    function isScroll(elem) {

    return elem.scrollHeight > elem.clientHeight;

    }
    /**

    • Set the position of footer
      */

    function setFooterPosition() {

    var pageContainer = $('footer').parents('.page-container') || $('body');
    $('footer').removeClass('fixed-bottom');
    if (!isScroll(pageContainer[0])) {
        $('footer').addClass('fixed-bottom');
    }

    }
    .fixed-bottom {

    position: fixed;
    bottom: 0;
    z-index: 999;
    ...

    }

    2019-07-17 19:15:37
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
《零基础HTML入门教程》 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载