内容高度小于窗口高度时版权 div 固定在底部

网站建设中经常遇到文档内容高度小于窗口高度时底部版权 div 固定在底部的问题,纯 css div 底部不太好解决这个问题,这里使用 js 代码来对检测文档高度和窗口高度来实现。


//js 代码调用方法说明:lrFixFooter("div.footerwarp"); 传入 div 固定底部的类名或者 ID 名


在制作这个 js 的时候发现个 IE8 的 bug

$(document).height() 竟然比其他浏览器多出 4 像素,于是就有了这个 js 判断 if(doc.height()-4 <= $(window).height()) 里面做了个减去 4 的处理。


<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>文档内容高度小于窗口高度时底部版权始终在底部-懒人建站</title>
<meta name="description" content="网站建设中经常遇到文档内容高度小于窗口高度时底部版权始终在底部的问题,纯css不太好解决这个问题,这里使用js代码来对检测文档高度和窗口高度来实现" />
</head>
<body>
<div style="height:500px; background:#ddd;">
<p>网站建设中经常遇到文档内容高度小于窗口高度时底部版权始终在底部的问题,纯css不太好解决这个问题,这里使用js代码来对检测文档高度和窗口高度来实现。</p>
    
</div>
<div class="footerwarp">底部版权始终位于底部</div>
<script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.9.0/jquery.min.js"></script>
<script>
$(function(){
lrFixFooter("div.footerwarp");//调用方法:lrFixFooter("div.footerwarp"); 传入底部的类名或者ID名
function lrFixFooter(obj){
var footer = $(obj),doc = $(document);
function fixFooter(){
if(doc.height()-4 <= $(window).height()){
footer.css({
width:"100%",
position:"absolute",
left:0,
bottom:0
});
}else{
footer.css({
position:"static"
});
}
}
fixFooter();
$(window).on('resize.footer', function(){
fixFooter();
});
$(window).on('scroll.footer',function(){
fixFooter();
});
}
})
</script>
</body>
</html>

打赏

取消

感谢支持 Savalone !

扫码支持

打开支付宝扫一扫,即可进行扫码打赏哦


「本文由 Savalone 原创或搜集整理发布,转载请遵守 CC BY-NC-ND 4.0 许可!」
  • 22