首页 > html中元素定位问题

html中元素定位问题

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


如果你确定一定要

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;
    ...
}
【热门文章】
【热门文章】