此文实现了如下效果:

jquery获得窗口高宽,文档高宽值。

通过CSS设置,实现DIV置顶,置底,置左,置右,居中。

利用jquery实现DIV四个方向随滚动条,窗体大小浮动。

一个漂浮反弹的DIV。

 

 
   
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html xmlns="http://www.w3.org/1999/xhtml"> 
  3. <head> 
  4.     <title>获取窗口高度宽度,DIV置顶,置底,置左,置右,居中,随滚动条居中</title> 
  5.     <script src="http://code.jquery.com/jquery-1.7.2.min.js" type="text/javascript"></script> 
  6.     <script type="text/javascript"> 
  7.         $(function () { 
  8.             showwidth_height(); 
  9.             $(window).resize(showwidth_height); 
  10.             $(window).scroll(movesmallleft); 
  11.             $(window).resize(movesmallleft); 
  12.  
  13.             $(window).scroll(movesmallright); 
  14.             $(window).resize(movesmallright); 
  15.  
  16.             $(window).scroll(movesmalltop); 
  17.             $(window).resize(movesmalltop); 
  18.  
  19.             $(window).scroll(movesmallbottom); 
  20.             $(window).resize(movesmallbottom); 
  21.  
  22.             var ivl = setInterval("floatdiv()", 10); //ivl变量,用来可以使得setInterval停止。通过clearInterval方法。 
  23.             $("#floatdiv").hover(function () { clearInterval(ivl) }, function () { ivl = setInterval("floatdiv()", 10); }); 
  24.         }) 
  25.  
  26.         function showwidth_height() { 
  27.             var s = "" 
  28.                 + "浏览器当前窗口可视区域高度:" + $(window).height() + "<br/>
  29.                 + "浏览器当前窗口文档的高度:" + $(document).height() + "<br/>
  30.                 + "浏览器当前窗口文档body的高度:" + $(document.body).height() + "<br/>
  31.                 + "浏览器当前窗口文档body的总高度 包括border padding margin :" + $(document.body).outerHeight(true) + "<br/>
  32.                 + "浏览器当前窗口可视区域宽度:" + $(window).width() + "<br/>
  33.                 + "浏览器当前窗口文档对象宽度:" + $(document).width() + "<br/>
  34.                 + "浏览器当前窗口文档body的宽度:" + $(document.body).width() + "<br/>
  35.                 + "浏览器当前窗口文档body的总宽度 包括border padding margin:" + ($(document.body).outerWidth(true)) + "<br/>
  36.             $("#center").html(s); 
  37.         } 
  38.  
  39.         function movesmallleft() { 
  40.             var topvalue = $(window).height() / 2 + $(window).scrollTop(); //滚动条滚动了多少偏移量,就要把这个偏移量加上去。 
  41.             var leftvalue = 0 + $(window).scrollLeft(); //滚动条滚动了多少偏移量,就要把这个偏移量加上去。 
  42.             //这里的stop方法相当必要 
  43.             //停止正在进行的动画 
  44.             //因为滚动条滚动的时候,会触发多次scroll事件,每次触发的时候,都做动画影响效率,因此要加上stop方法 
  45.             //使之立即停止后再做动画。 
  46.             $("#smallleft").stop().animate({ top: topvalue, left: leftvalue, marginTop: "-10px" }, "slow"); 
  47.         } 
  48.         function movesmallright() { 
  49.             var topvalue = 150 + $(window).scrollTop(); //滚动条滚动了多少偏移量,就要把这个偏移量加上去。 
  50.             var rightvalue = 0 - $(window).scrollLeft(); //滚动条滚动了多少偏移量,就要把这个偏移量加上去。 
  51.             $("#smallright").stop().animate({ top: topvalue, right: rightvalue }, "slow"); 
  52.         } 
  53.         function movesmalltop() { 
  54.             var topvalue = 0 + $(window).scrollTop(); //滚动条滚动了多少偏移量,就要把这个偏移量加上去。 
  55.             var leftvalue = $(window).width() / 2 + $(window).scrollLeft(); //滚动条滚动了多少偏移量,就要把这个偏移量加上去。 
  56.             $("#smalltop").stop().animate({ top: topvalue, left: leftvalue }, "slow"); 
  57.         } 
  58.         function movesmallbottom() { 
  59.             var bottomvalue = 0 - $(window).scrollTop(); //滚动条滚动了多少偏移量,就要把这个偏移量加上去。 
  60.             var leftvalue = $(window).width() / 2 + $(window).scrollLeft(); //滚动条滚动了多少偏移量,就要把这个偏移量加上去。 
  61.             $("#smallbottom").stop().animate({ bottom: bottomvalue, left: leftvalue }, "slow"); 
  62.         } 
  63.  
  64.         var direct = "leftup"
  65.         //根据当前方向和偏移量,判断接下来的方向。 
  66.         function floatdiv() { 
  67.             var top = $("#floatdiv").offset().top; 
  68.             var left = $("#floatdiv").offset().left; 
  69.             //加上偏移量,主要用来处理滚动条的滚动时候对top,left的影响 
  70.             if (top < 0 + $(window).scrollTop() && direct == "rightup") { 
  71.                 direct = "rightdown"
  72.             } 
  73.             else if (top < 0 + $(window).scrollTop() && direct == "leftup") { 
  74.                 direct = "leftdown"
  75.             } 
  76.             else if (left < 0 + $(window).scrollLeft() && direct == "leftup") { 
  77.                 direct = "rightup"
  78.             } 
  79.             else if (left < 0 + $(window).scrollLeft() && direct == "leftdown") { 
  80.                 direct = "rightdown"
  81.             } 
  82.             //加上div的高度 
  83.             else if (top + 80 > $(window).height() + $(window).scrollTop() && direct == "leftdown") { 
  84.                 direct = "leftup"
  85.             } 
  86.             else if (top + 80 > $(window).height() + $(window).scrollTop() && direct == "rightdown") { 
  87.                 direct = "rightup"
  88.             } 
  89.             //加上div的宽度 
  90.             else if (left + 80 > $(window).width() + $(window).scrollLeft() && direct == "rightdown") { 
  91.                 direct = "leftdown"
  92.             } 
  93.             else if (left + 80 > $(window).width() + $(window).scrollLeft() && direct == "rightup") { 
  94.                 direct = "leftup"
  95.             } 
  96.  
  97.             if (direct == "leftdown") { 
  98.                 toptop = top + 1; 
  99.                 leftleft = left - 5; 
  100.             } else if (direct == "rightdown") { 
  101.                 toptop = top + 1; 
  102.                 leftleft = left + 5; 
  103.  
  104.             } else if (direct == "rightup") { 
  105.                 toptop = top - 1; 
  106.                 leftleft = left + 5; 
  107.  
  108.             } else if (direct == "leftup") { 
  109.                 toptop = top - 1; 
  110.                 leftleft = left - 5; 
  111.             } 
  112.  
  113.             $("#floatdiv").html(Date()); 
  114.             $("#floatdiv").offset({ "top": top, "left": left }) 
  115.         } 
  116.  
  117.     </script> 
  118.     <style type="text/css"> 
  119.         #top 
  120.         { 
  121.             position: fixed; 
  122.             top: 20px; 
  123.             left: 0px; 
  124.             height: 50px; 
  125.             width: 100%; 
  126.             background: #666666; 
  127.             z-index: 6; 
  128.             text-align: center; 
  129.         } 
  130.          
  131.         #left 
  132.         { 
  133.             position: fixed; 
  134.             top: 50%; 
  135.             left: 10px; 
  136.             height: 300px; 
  137.             width: 100px; 
  138.             background: #666666; 
  139.             z-index: 6; 
  140.             text-align: center; 
  141.             margin-top: -150px; /*因为不是50%不是真正的居中,所以要上移高度的一半*/ 
  142.         } 
  143.          
  144.         #smallleft 
  145.         { 
  146.             position: absolute; 
  147.             top: 50%; 
  148.             left: 10px; 
  149.             height: 20px; 
  150.             width: 20px; 
  151.             background: red; 
  152.             z-index: 6; 
  153.             text-align: center; 
  154.             margin-top: -10px; /*因为不是50%不是真正的居中,所以要上移高度的一半*/ 
  155.         } 
  156.          
  157.         #smallright 
  158.         { 
  159.             position: absolute; 
  160.             top: 150px; 
  161.             right: 0px; 
  162.             height: 20px; 
  163.             width: 20px; 
  164.             background: red; 
  165.             z-index: 6; 
  166.             text-align: center; 
  167.         } 
  168.          
  169.         #smalltop 
  170.         { 
  171.             position: absolute; 
  172.             top: 0px; 
  173.             left: 50%; 
  174.             height: 20px; 
  175.             width: 20px; 
  176.             background: red; 
  177.             z-index: 6; 
  178.             text-align: center; 
  179.         } 
  180.          
  181.         #smallbottom 
  182.         { 
  183.             position: absolute; 
  184.             bottom: 0px; 
  185.             left: 50%; 
  186.             height: 20px; 
  187.             width: 20px; 
  188.             background: red; 
  189.             z-index: 6; 
  190.             text-align: center; 
  191.         } 
  192.          
  193.         #floatdiv 
  194.         { 
  195.             position: fixed; 
  196.             top: 200px; 
  197.             left: 300px; 
  198.             height: 80px; 
  199.             width: 80px; 
  200.             background: blue; 
  201.             z-index: 6; 
  202.             text-align: center; 
  203.         } 
  204.          
  205.         #bottom 
  206.         { 
  207.             z-index: 6; 
  208.             position: fixed; 
  209.             bottom: 0px; 
  210.             background: #666666; 
  211.             height: 50px; 
  212.             width: 100%; 
  213.             text-align: center; 
  214.         } 
  215.          
  216.         #right 
  217.         { 
  218.             position: fixed; 
  219.             top: 50%; 
  220.             right: 10px; 
  221.             height: 300px; 
  222.             width: 100px; 
  223.             background: #666666; 
  224.             z-index: 6; 
  225.             text-align: center; 
  226.             margin-top: -150px; /*因为不是50%不是真正的居中,所以要上移高度的一半*/ 
  227.         } 
  228.          
  229.         #center 
  230.         { 
  231.             position: fixed; 
  232.             top: 50%; 
  233.             left: 50%; 
  234.             height: 200px; 
  235.             width: 400px; 
  236.             background: #666666; 
  237.             z-index: 6; 
  238.             text-align: center; 
  239.             margin-top: -100px; /*因为不是50%不是真正的居中,所以要上移高度的一半*/ 
  240.             margin-left: -200px; /*因为不是50%不是真正的居中,所以要左移宽度的一半*/ 
  241.         } 
  242.     </style> 
  243. </head> 
  244. <body> 
  245.     <div id="top"> 
  246.         TOP 
  247.     </div> 
  248.     <div id="left"> 
  249.         LEFT 
  250.     </div> 
  251.     <div id="bottom"> 
  252.         BOTTOM 
  253.     </div> 
  254.     <div id="right"> 
  255.         RIGHT 
  256.     </div> 
  257.     <div id="center"> 
  258.         CENTER 
  259.     </div> 
  260.     <div id="smallleft"> 
  261.     </div> 
  262.     <div id="smallright"> 
  263.     </div> 
  264.     <div id="smallbottom"> 
  265.     </div> 
  266.     <div id="smalltop"> 
  267.     </div> 
  268.     <div id="floatdiv"> 
  269.     </div> 
  270.     <div style="height: 1024px; background-color: Silver"> 
  271.         <pre> 
  272.  
  273.  
  274.  
  275.  
  276.     绝对定位使元素的位置与文档流无关,因此不占据空间。 
  277.     这一点与相对定位不同,相对定位实际上被看作普通流定位模型的一部分,因为元素的位置相对于它在普通流中的位置。 
  278.     普通流中其它元素的布局就像绝对定位的元素不存在一样: 
  279.  
  280.     绝对定位的元素的位置相对于最近的已定位祖先元素,如果元素没有已定位的祖先元素,那么它的位置相对于最初的包含块。 
  281.  
  282. </pre> 
  283.         <table> 
  284.             <tr> 
  285.                 <td> 
  286.                     absolute 
  287.                 </td> 
  288.                 <td> 
  289.                     <p> 
  290.                         生成绝对定位的元素,相对于 static 定位以外的第一个父元素进行定位。</p> 
  291.                     <p> 
  292.                         元素的位置通过 &quot;left&quot;, &quot;top&quot;, &quot;right&quot; 以及 &quot;bottom&quot; 
  293.                         属性进行规定。</p> 
  294.                 </td> 
  295.             </tr> 
  296.             <tr> 
  297.                 <td> 
  298.                     fixed 
  299.                 </td> 
  300.                 <td> 
  301.                     <p> 
  302.                         生成绝对定位的元素,相对于浏览器窗口进行定位。</p> 
  303.                     <p> 
  304.                         元素的位置通过 &quot;left&quot;, &quot;top&quot;, &quot;right&quot; 以及 &quot;bottom&quot; 
  305.                         属性进行规定。</p> 
  306.                 </td> 
  307.             </tr> 
  308.             <tr> 
  309.                 <td> 
  310.                     relative 
  311.                 </td> 
  312.                 <td> 
  313.                     <p> 
  314.                         生成相对定位的元素,相对于其正常位置进行定位。</p> 
  315.                     <p> 
  316.                         因此,&quot;left:20&quot; 会向元素的 LEFT 位置添加 20 像素。</p> 
  317.                 </td> 
  318.             </tr> 
  319.             <tr> 
  320.                 <td> 
  321.                     static 
  322.                 </td> 
  323.                 <td> 
  324.                     默认值。没有定位,元素出现在正常的流中(忽略 top, bottom, left, right 或者 z-index 声明)。 
  325.                 </td> 
  326.             </tr> 
  327.             <tr> 
  328.                 <td> 
  329.                     inherit 
  330.                 </td> 
  331.                 <td> 
  332.                     规定应该从父元素继承 position 属性的值。 
  333.                 </td> 
  334.             </tr> 
  335.         </table> 
  336.     </div> 
  337. </body> 
  338. </html>