javascript将页尾悬置在页面底部

需求描述:

网站的页尾常常需要被放置在底部。但是普通文档流布局的话,这个底部会随着文档流移动,在不同高度的显示器上的表现效果就不同了。这里就需要:

1.当文档高度大于可视区域高度的时候,跟随文档流布局。

2.当文档高度小于可视区域高度的时候,固定在底部。


几个知识点:

a.首先是文档高度的确定

这里使用的是document.body.offsetHeight这个属性可以获取你整个文档的高度(包括margin、body的padding、滚动条);兼容性可以做到IE8


b.其次是页面可视区域的大小

这里用的是document.documentElement.clientHeight这个值,这个值可以兼容到IE8(IE8会比其他的浏览器少4个像素,但是这里就不去纠结了,考虑性能方面)。


c.onresize事件

当浏览器尺寸发生变化时,这个尾部要动态的发生改变,所以需要监听这个事件。


代码:

<span style="font-size:14px;">(function() {
    window.onload = function() {
        position("需要传入的尾部id");
    }
    window.onresize = function() {
        position("需要传入的尾部id");
    };

    function position(footId) {
        var footer = document.getElementById(footId);
        var eleHeight = document.body.offsetHeight;
        var screenHeight = document.documentElement.clientHeight;
        if (eleHeight < screenHeight) {
            footer.style.position = "fixed";
            footer.style.bottom = "0";
            if (footer.offsetWidth == 0) {
                footer.style.width = "100%";
            }
        } else {
            footer.style.position = "static";
        }
    }
}());</span>

相关提示:

还有不用js处理的方法,详见这篇文章:

http://mp.weixin.qq.com/s?__biz=MjM5NzE0MjQ2Mw==&mid=404985661&idx=1&sn=a54c3efb61ed982f010822ff5bf06034&scene=23&srcid=0415gt250uRNmeT8LN2WqCQ2#rd


XHTML 40个例子显示的内容一样,通过CSS实现不一样的布局效果, 是学习div + css排版的绝佳教程.body标签内的内容如下: Header 1) Content here. column long long column very long fill fill fill long text text column text silly very make long very fill silly make make long make text fill very long text column silly silly very column long very column filler fill long make filler long silly very long silly silly silly long filler make column filler make silly long long fill very. very make make fill silly long long filler column long make silly silly column filler fill fill very filler text fill filler column make fill make text very make make very fill fill long make very filler column very long very filler silly very make filler silly make make column column fill long make long text very make long fill column make text very silly column filler silly text fill text filler filler filler make make make make text filler fill column filler make silly make text text fill make very filler column very column text long column make silly long text filler silly very very very long filler fill very fill silly very make make filler text filler text make silly text text long fill fill make text fill long text very silly long long filler filler fill silly long make column make silly long column long make very 2) Navigation here. long long fill filler very fill column column silly filler very filler fill fill filler text fill very silly fill text filler silly silly filler fill very make fill column text column very very column fill fill very silly column silly silly fill fill long filler 3) More stuff here. very text make long silly make text very very text make long filler very make column make silly column fill silly column long make silly filler column filler silly long long column fill silly column very Here it goes the footer
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值