简体   繁体   English

为什么我的代码只能使用height:100px,但不能使用height:100%?

[英]Why does my code work with height:100px, but not height:100%?

Following is the piece of code that gets more content from DB when scrolled down. 以下是向下滚动时从DB获取更多内容的代码段。 Its mainly JQuery. 其主要是JQuery。

I don't understand why it works when height is changed as 100px, but it doesn't work when height is given as 100% or auto. 我不明白为什么在将高度更改为100px时它可以工作,但是在将高度设置为100%或自动时却不起作用。

<div id="contentBox" style="overflow: scroll;width: 200px; height: 100px;">
  <div id="actualContent">
    <p>
      <span>2</span>
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris ornare facilisis mollis. Etiam non sem massa, a gravida nunc. Mauris lectus augue, posuere at viverra sed, dignissim sed libero. 
    </p>
    <p>
      <span>3</span>
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris ornare facilisis mollis. Etiam non sem massa, a gravida nunc. Mauris lectus augue, posuere at viverra sed, dignissim sed libero.
    </p>
  </div>
</div>

$("#contentBox").scroll(function(){
  if($("#contentBox").scrollTop() >= ($("#actualContent").height() - $("#contentBox").height())){
       alert("Hi");
     }
 });

Here is the link: https://jsfiddle.net/8qv2ch9u/ 这是链接: https : //jsfiddle.net/8qv2ch9u/

How to make it work without height 100px, but the content filling the size of the browser? 如何使它在没有高度100px的情况下工作,但是内容填充了浏览器的大小?

With height: 100px , the element knows exactly what it needs to do. height: 100px ,元素完全知道它需要做什么。

With height: 100% , the element needs to know: percentage of what? height: 100% ,元素需要知道: 百分比是多少?

For percentage heights, you need to provide a frame of reference. 对于百分比高度,您需要提供参考框架。 This is done by specifying the height of the parent. 这是通过指定父级的高度来完成的。 And if the parent of #contentBox is also a percentage, then the grandparent would need an explicit height, as well. 并且,如果#contentBox的父代也是一个百分比,那么祖父母也将需要一个显式的身高。

Basically, if you're going to use percentage heights, you need to specify the heights for all parent elements up to and including the root element: 基本上,如果要使用百分比高度,则需要为所有父元素(包括根元素)指定高度,包括根元素:

html, body { height: 100%; }

https://jsfiddle.net/8qv2ch9u/3/ https://jsfiddle.net/8qv2ch9u/3/

Here's the relevant section from the spec: 这是规范中的相关部分:

W3C height property definition : W3C height属性定义

percentage 百分比
Specifies a percentage height. 指定百分比高度。 The percentage is calculated with respect to the height of the generated box's containing block. 相对于生成的盒子的包含块的高度计算百分比。 If the height of the containing block is not specified explicitly and this element is not absolutely positioned, the value computes to 'auto'. 如果未明确指定包含块的高度,并且此元素未绝对定位,则该值将计算为“ auto”。

auto 汽车
The height depends on the values of other properties. 高度取决于其他属性的值。

See also: 也可以看看:

As pointed out by michael you can set body height, point is to set height of element in percentage it must have a parent element from whom it will be getting percentage height. 正如michael指出的,您可以设置body高度,重点是设置元素的高度百分比,它必须有一个父元素,该元素将从该元素中获取百分比高度。 Or try the whole code in another div(parent element) and set height in pixels for parent element, then height of child element can be set to percentage of parent element. 或者在另一个div(父元素)中尝试整个代码,并为父元素设置高度(以像素为单位),然后可以将子元素的高度设置为父元素的百分比。 Check this simple snippet. 检查这个简单的代码段。

 .parent{width:50px;height:600px;} .childone{width:100%;height:80%;background-color:red;} .childtwo{width:100%;height:19%;background-color:green;} 
 <div class='parent'> <div class='childone'>I have 80% height of my parent</div> <div class='childtwo'>i have 19% height of my parent</div> </div> 

只需将视图高度的100px更改为100vh ,将视图宽度的更改为vw

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM