admin管理员组

文章数量:1333406

I've spent ages trying to use CSS to stick my footer to the bottom of my page, and have just about given up.

What I want is for the footer to have no extra CSS properties assigned if the height of the viewport is less than the height of the HTML document.

If the document height is less than the window height, I want the following CSS assigned to div#thefooter:

position: fixed;
bottom: 0px;
margin-left: -5px;

So here's my JS code. It does nothing, and the console log shows nothing.

$(document).ready(function(){

  $(window).bind("load", function(){ putFooter(); });
  $(window).resize(function(){ putFooter(); });

  function putFooter(){
    var wh = $(window).height();
    var dh = $(document).height();
    if(dh < wh) {
      $("#thefooter").css({"position": "fixed", "bottom": "0px", "margin-left": "-5px"});
      }
    }

});

EDIT: and here's what my HTML looks like:

<div id="allexceptfooter">
  <div id="themenu">...</div>
  <div id="actualcontent">...</div>
</div>
<div id="thefooter">...</div>

If you want to see the whole thing my website is duncannz

I've spent ages trying to use CSS to stick my footer to the bottom of my page, and have just about given up.

What I want is for the footer to have no extra CSS properties assigned if the height of the viewport is less than the height of the HTML document.

If the document height is less than the window height, I want the following CSS assigned to div#thefooter:

position: fixed;
bottom: 0px;
margin-left: -5px;

So here's my JS code. It does nothing, and the console log shows nothing.

$(document).ready(function(){

  $(window).bind("load", function(){ putFooter(); });
  $(window).resize(function(){ putFooter(); });

  function putFooter(){
    var wh = $(window).height();
    var dh = $(document).height();
    if(dh < wh) {
      $("#thefooter").css({"position": "fixed", "bottom": "0px", "margin-left": "-5px"});
      }
    }

});

EDIT: and here's what my HTML looks like:

<div id="allexceptfooter">
  <div id="themenu">...</div>
  <div id="actualcontent">...</div>
</div>
<div id="thefooter">...</div>

If you want to see the whole thing my website is duncannz .

Share Improve this question edited Jun 9, 2012 at 22:03 asked Jun 9, 2012 at 21:19 user1318194user1318194 2
  • 1 You can bine the three .css(...) parts into one by using an object .css({"position": "fixed", "bottom": "0px", "margin-left": "-5px"}) – Andreas Commented Jun 9, 2012 at 21:23
  • That was just meant as a hint for the future :D – Andreas Commented Jun 9, 2012 at 21:27
Add a ment  | 

4 Answers 4

Reset to default 5

Check this out, no javascript needed at all...

http://www.cssstickyfooter.

OK I've got it. Not with CSS - I've already spent ages trying with that.

But I have the jQuery working. The problem was that $(document).height(); was returning the height of the viewport, since I use body{ height: 100%; } in my CSS. The answer was to use this HTML:

<body>
<div id="everything">
...
</div>
</body>

and use $("#everything").height(); instead of $(document).height();. Still requires JS unfortunately, but better than nothing. No CSS solution worked for me.

EDIT AGAIN: Here's the again-updated code:

$(document).ready(function(){

  function putFooter(){
    var wh = $(window).height();
    var dh = $("#everything").height();
    if(dh < wh - 104) { /*104: #thefooter height in px*/
      $("#thefooter").addClass("footerissticky");
      }
    else {
      $("#thefooter").removeClass("footerissticky");
      }
    }

  putFooter();
  $(window).bind("load", function(){ putFooter(); });
  $(window).resize(function(){ putFooter(); });

});

and the CSS:

.footerissticky{
  position: fixed;
  bottom: 0px;
  width: 870px;
}

You can avoid Javascript at all using this technique:

http://ryanfait./sticky-footer/

JQuery has added data-position="fixed" to solve this. Answered in - jQuery Mobile: Stick footer to bottom of page

本文标签: javascriptjQuery to stick the footer to the bottom of the pageStack Overflow