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
4 Answers
Reset to default 5Check 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
版权声明:本文标题:javascript - jQuery to stick the footer to the bottom of the page - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742331604a2454806.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论