Reputation: 2204
I'm using jquery .height() to fetch the document height.
Please check this http://jsfiddle.net/rkumarnirmal/86q37/
After fetching the height, I'm placing the element in that position. Where you can see the document height is actually wrong, because it is going beyond the area.
How can I solve this?
Thanks!
Upvotes: 0
Views: 81
Reputation: 33437
The top
is relative to the top of #dummy.
So you need to subtract the height of #dummy from the height of the page:
$(function(){
var h = $(document).height() - $("#dummy").height();
$("#dummy").css("top", h+"px");
})
I should mention though that you can do this in pure CSS:
If you're wanting to support old browsers though, you're going to either need to look up some CSS tricks to do it, or just stick with JS.
Upvotes: 3
Reputation: 253308
One solution is to use jQuery, and subtract the height of the element from the height of the body (and you don't need to worry about adding the px
to the height, jQuery handles it internally):
$(function(){
var h = $(document).height();
$("#dummy").css("top", h - $('#dummy').height());
})
The easier solution is to simply use CSS:
#dummy {
position: absolute;
width: 100px;
height: 100px;
bottom: 0; /* aligns the bottom of the element zero pixels from the bottom of the document */
left: 0; /* aligns the left side of the element zero pixels from the left side of the document */
background-color: red;
}
Upvotes: 1
Reputation: 79830
.height
is returning the correct value. But you are setting top position of the box to be the height of the document. See below demo for better understanding,
$(function(){
var h = $(document).height();
$("#dummy").css("top", function () {
return (h - $(this).height()) +"px";
//This will place the box at top position subtracting
//the documentHeight - dummyHeight;
});
})
Upvotes: 1