我使用jquery .height()来获取文档高度。
请检查此http://jsfiddle.net/rkumarnirmal/86q37/
获取高度后,我将元素放在该位置。你可以看到的文档高度实际上是错误的,因为它超出了区域。
我该如何解决这个问题呢?
谢谢!
发布于 2012-05-17 05:58:56
top是相对于#dummy的顶部的。
所以你需要从页面的高度中减去#dummy的高度:
$(function(){
var h = $(document).height() - $("#dummy").height();
$("#dummy").css("top", h+"px");
})我应该提一下,你可以在纯CSS中做到这一点:
但是,如果你想支持老的浏览器,你要么需要查找一些CSS技巧来实现,要么就坚持使用JS。
发布于 2012-05-17 06:00:23
.height正在返回正确的值。但是您将框的顶部位置设置为文档的高度。为了更好地理解,请参阅下面的demo。
$(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;
});
})发布于 2012-05-17 06:01:54
一种解决方案是使用jQuery,并从主体的高度中减去元素的高度(并且您不需要担心将px添加到高度,jQuery在内部处理它):
$(function(){
var h = $(document).height();
$("#dummy").css("top", h - $('#dummy').height());
})更简单的解决方案是简单地使用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;
}https://stackoverflow.com/questions/10627334
复制相似问题