zoukankan      html  css  js  c++  java
  • 将页脚保持在页面的底部——Javascript+Css实现

    功能:无论将浏览器拖大或拉小,将页脚DIV块保持在页面的底部

    1、将Javascript代码和DIV/CSS代码写在同一个文件里:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>让页脚保持在未满屏页面的底部</title>
    <!--DIV块的CSS-->
    <style type="text/css">
    *
    {margin:0;padding:0;}
    #top
    {background:#33CCFF;text-align:center;}
    #bottom
    {background:#FFCC00;text-align:center;width:100%;}
    </style>

    </head>

    <body>
    <div id="top">top</div>
    <div id="bottom">bottom</div>
    <!--javascript代码,之所以写在后面,是为了等完全加载top和bottom的DIV块后,便于代码读取相关信息-->
    <script language="javascript" type="text/javascript">
    function calcDistance(){
    var topHeight = document.getElementById("top").scrollHeight;
    var bottomHeight = document.getElementById("bottom").scrollHeight;
    var allHeight = document.documentElement.clientHeight;
    var bottom = document.getElementById("bottom");
    if((topHeight + bottomHeight) < allHeight){
    bottom.style.position
    = "absolute";
    bottom.style.bottom
    = "0";
    }
    else{
    bottom.style.position
    = "";
    bottom.style.bottom
    = "";
    }
    setTimeout(
    function(){calcDistance();},10);
    }
    calcDistance();
    </script>

    </body>
    </html>

    效果:两DIV块未相交时:

    两DIV块相交时,没有产生覆盖现象(设置第二块bottom为0时常有的现象):

    2、将Html、DIV/CSS、Javascript分别写在不同的文件里:

    html文件index.html(其中调用了index.js和index.css):

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>让页脚保持在未满屏页面的底部</title>
    <link href="index.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" language="javascript" src="index.js"></script>
    </head>

    <body>
    <div id="top">top</div>
    <div id="bottom">bottom</div>
    <script language="javascript" type="text/javascript">
    calcDistance();
    </script>
    </body>
    </html>

    index.css文件:

    @charset "utf-8";
    /* CSS Document */
    *
    {
    margin
    :0;
    padding
    :0;
    }
    #top
    {
    background
    :#33CCFF;
    text-align
    :center;
    }
    #bottom
    {
    background
    :#FFCC00;
    text-align
    :center;
    width
    :100%;
    }

    注意:此处最好给出top和bottom两个DIV块的高度,便于javascript代码计算,有的情况下(比如top块中包含其它DIV块时,可能会造成有的浏览器下计算不准确)

    index.js文件:

    // JavaScript Document
    function calcDistance(){
    var topHeight = document.getElementById("top").scrollHeight;
    var bottomHeight = document.getElementById("bottom").scrollHeight;
    var allHeight = document.documentElement.clientHeight;
    var bottom = document.getElementById("bottom");
    if((topHeight + bottomHeight) < allHeight){
    bottom.style.position = "absolute";
    bottom.style.bottom = "0";//设置距底部距离时如果用数字apx出错,则试试apx
    }else{
    bottom.style.position = "";
    bottom.style.bottom = "";
    }
    setTimeout(function(){calcDistance();},10);
    }

    3、如果想底栏DIV块距离其之上最后一个DIV块的最小距离为A(假设为150px),那么只需修改index.js文件即可,修改如下:

    // JavaScript Document
    function calcDistance(){
    var topHeight = document.getElementById("top").scrollHeight;
    var bottomHeight = document.getElementById("bottom").scrollHeight;
    var allHeight = document.documentElement.clientHeight;
    var bottom = document.getElementById("bottom");
    if((topHeight + bottomHeight + 150) < allHeight){
    bottom.style.position = "absolute";
    bottom.style.bottom = "0";
    bottom.style.top = "";
    }else{
    bottom.style.position = "relative";
    bottom.style.bottom = "";
    bottom.style.top = "150px";//距离上一个DIV块150像素,如果用150px达不到想要的结果,则试试150(去掉px)
    }
    setTimeout(function(){calcDistance();},10);
    }

    效果图(两个DIV块之间距离小于150像素时,垂直滚动条出现):

    推荐一个自己业余时间开发的网盘搜索引擎,360盘搜www.360panso.com






  • 相关阅读:
    华为OJ机试训练(一)
    mount CIFS return ERR -12 and report Cannot allocate memory
    ftk学习记(icon篇)
    使用jquery-mockjax模拟ajax请求做前台測试
    Objective-C 内存管理之 _ARC
    [LeetCode]Decode Ways
    设计模式六大原则——迪米特法则(LoD)
    ACM/ICPC2014鞍山现场赛E hdu5074Hatsune Miku
    2015届校园招聘笔试/面试 基础知识点 总结
    依据Path取Json指定节点的值
  • 原文地址:https://www.cnblogs.com/eczhou/p/2381110.html
Copyright © 2011-2022 走看看