固定 VS 滚动的绝对定位
fixed VS absolute positioning for scrolling
我只是在玩弄一些 CSS 绝对和固定属性,发现 CSS 中绝对定位和相对定位之间存在不寻常的差异。
基本上,当我绝对定位某些东西并且内容超过 window 或包含元素的高度时,滚动条出现,但是当我将位置更改为固定时,即使内容是与 window 相比高度更高,不会出现滚动条。
我为此创建了一个测试用例:
HTML:
<div class="page-container">
<div class="helper">
<div class="marker red"></div>
<div class="marker green"></div>
<div class="marker yellow"></div>
<div class="marker blue"></div>
</div>
</div>
CSS:
#slides-container {
height: 100%;
width: 100%;
overflow: hidden;
}
.helper {
height: 400%;
width: 20px;
position: fixed; /* change this to absolute and the scrollbars appear*/
top: 0;
left: 0;
}
.helper .marker {
height: 25%;
width: 100%;
}
.marker.red {
background: red;
}
.marker.green {
background: green;
}
.marker.yellow {
background: yellow;
}
.marker.blue {
background: blue;
}
这里是 fiddle:fiddle。 (查看 CSS 中的评论)
希望得到有关此问题的解释。
固定定位意味着元素在视口中是固定的——它不受滚动的影响。您可以在这里阅读更多内容:https://css-tricks.com/absolute-relative-fixed-positioining-how-do-they-differ/
由于没有其他任何东西可以为您的页面提供高度,因此在这种情况下您所看到的就是固定元素适合您的视口的任何部分。
看看在这个版本中为容器设置高度时会发生什么:
http://jsfiddle.net/93ubza81/3/
.page-content{
height: 3000px;
}
您有滚动,但固定元素不受影响。
我只是在玩弄一些 CSS 绝对和固定属性,发现 CSS 中绝对定位和相对定位之间存在不寻常的差异。
基本上,当我绝对定位某些东西并且内容超过 window 或包含元素的高度时,滚动条出现,但是当我将位置更改为固定时,即使内容是与 window 相比高度更高,不会出现滚动条。
我为此创建了一个测试用例:
HTML:
<div class="page-container">
<div class="helper">
<div class="marker red"></div>
<div class="marker green"></div>
<div class="marker yellow"></div>
<div class="marker blue"></div>
</div>
</div>
CSS:
#slides-container {
height: 100%;
width: 100%;
overflow: hidden;
}
.helper {
height: 400%;
width: 20px;
position: fixed; /* change this to absolute and the scrollbars appear*/
top: 0;
left: 0;
}
.helper .marker {
height: 25%;
width: 100%;
}
.marker.red {
background: red;
}
.marker.green {
background: green;
}
.marker.yellow {
background: yellow;
}
.marker.blue {
background: blue;
}
这里是 fiddle:fiddle。 (查看 CSS 中的评论)
希望得到有关此问题的解释。
固定定位意味着元素在视口中是固定的——它不受滚动的影响。您可以在这里阅读更多内容:https://css-tricks.com/absolute-relative-fixed-positioining-how-do-they-differ/
由于没有其他任何东西可以为您的页面提供高度,因此在这种情况下您所看到的就是固定元素适合您的视口的任何部分。
看看在这个版本中为容器设置高度时会发生什么: http://jsfiddle.net/93ubza81/3/
.page-content{
height: 3000px;
}
您有滚动,但固定元素不受影响。