Javascript jquery-使用.height()
我正在使用jquery.height()获取文档高度 请检查这个 获取高度后,我将元素放置在该位置。您可以看到文档高度实际上是错误的,因为它超出了该区域 我怎样才能解决这个问题Javascript jquery-使用.height(),javascript,jquery,html,css,Javascript,Jquery,Html,Css,我正在使用jquery.height()获取文档高度 请检查这个 获取高度后,我将元素放置在该位置。您可以看到文档高度实际上是错误的,因为它超出了该区域 我怎样才能解决这个问题 谢谢 顶部与#假人的顶部相对 因此,您需要从页面高度中减去#dummy的高度: $(function(){ var h = $(document).height() - $("#dummy").height(); $("#dummy").css("top", h+"px"); }) 我应该提到的是,
谢谢 顶部与#假人的顶部相对 因此,您需要从页面高度中减去#dummy的高度:
$(function(){
var h = $(document).height() - $("#dummy").height();
$("#dummy").css("top", h+"px");
})
我应该提到的是,您可以在纯CSS中实现这一点:
如果你想支持旧浏览器,你要么需要查找一些CSS技巧来实现,要么就坚持使用JS。top是相对于#dummy的top 因此,您需要从页面高度中减去#dummy的高度:
$(function(){
var h = $(document).height() - $("#dummy").height();
$("#dummy").css("top", h+"px");
})
我应该提到的是,您可以在纯CSS中实现这一点:
如果你想支持旧的浏览器,你要么需要查找一些CSS技巧,要么就坚持使用JS。
。height
返回正确的值。但您将框的顶部位置设置为文档的高度。请参阅下面的演示以更好地理解
$(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;
});
})
。height
正在返回正确的值。但您将框的顶部位置设置为文档的高度。请参阅下面的演示以更好地理解
$(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;
});
})
一种解决方案是使用jQuery,并从主体高度中减去元素高度(您不必担心将
px
添加到高度,jQuery在内部处理):
更简单的解决方案是简单地使用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;
}
更简单的解决方案是简单地使用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;
}
.
.height
正在返回正确的值,您正在将框的顶部位置设置为高度。为了更好地理解这个演示,你可能打算从这个高度中减去假人的高度,从而使假人位于页面底部?当然,这可以通过css直接完成。如果高度返回正确的值,则将框的顶部位置设置为高度。为了更好地理解这个演示,你可能打算从这个高度中减去假人的高度,从而使假人位于页面底部?这当然可以通过css直接实现