html - 如何使左右列粘在底部的 3 列布局?
问题描述
我有一个 3 列布局,在中心列中,当滚动到最后时,我正在添加内容,就像无限滚动一样。现在我需要在底部获得粘性左右列,但放置 position: sticky, bottom: 0px 对我没有任何作用。我将父高度更改为 100hv 以始终保持视口较高,而不是每次在中间列中添加一些内容时都在增长的视口,但仍然无法正常工作。我可以得到一些想法或基本示例吗?
我想在这个例子中完成这个。
.App {
text-align: center;
}
.topheader {
background: red;
width: 100%;
height: 50px;
}
.box {
width: 100%;
height: 200px;
background: gray;
}
.content {
width: 100%;
height: auto;
display: flex;
}
.left {
background: blue;
display: flex;
flex-direction: column;
width: 220px;
height: 100%;
}
.center {
background: yellow;
width: 540px;
height: auto;
}
.right {
background: green;
display: flex;
width: 220px;
height: 100%;
flex-direction: column;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root">
<div class="App">
<div class="topheader"></div>
<div class="content">
<div class="left">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">2</div>
<div class="box">2</div>
<div class="box">2</div>
<div class="box">stick here at bottom</div>
</div>
<div class="center">
<div class="box">6</div>
<div class="box">6</div>
<div class="box">6</div>
<div class="box">6</div>
<div class="box">6</div>
<div class="box">6</div>
<div class="box">6</div>
<div class="box">6</div>
<div class="box">6</div>
<div class="box">6</div>
<div class="box">6</div>
<div class="box">6</div>
</div>
<div class="right">
<div class="box">3</div>
<div class="box">4</div>
<div class="box">4</div>
<div class="box">4</div>
<div class="box">stick here at bottom</div>
</div>
</div>
</div>
</div>
</html>
期望的行为是,当我向下滚动并且视口的底部到达左右列的底部(粘在底部)时,左列和右列被粘在那里并继续滚动以查看中心内容。
解决方案
使用position: sticky
<div class="container">
<div class="item shark-1">
<img src="/images/punk.png" width="100" alt="Sammy the Shark with a pun theme.">
</div>
<div class="item shark-2">
<img src="/images/pony.png" width="100" alt="Sammy the Shark with a magical pony theme.">
</div>
<div class="item shark-3">
<img src="/images/dino.png" width="100" alt="Sammy the Shark with a dinosaur theme.">
</div>
<div class="item shark-4">
<img src="/images/steampunk.png" width="100" alt="Sammy the Shark with a steampunk theme.">
</div>
</div>
CSS:
.container {
display: flex;
justify-content: space-around;
align-items: flex-start;
border: 2px dashed rgba(114, 186, 94, 0.35);
height: 400px;
background: rgba(114, 186, 94, 0.05);
}
.item { position: sticky;}
.shark-1 { top: 0;}
.shark-2 {top: 4rem;
.shark-3 { bottom: 1rem; align-self: flex-end;}
推荐阅读
- swift - 调试 - 降低 Swift 4.2 中的音频麦克风输入电平 - 它太敏感了
- rspec - 当我在 rspec 测试中使用 Watir.default_timeout 时,它似乎不起作用
- c# - 为什么 https 请求资源时缺少“X-Forwarded-For”和“Via”标头?
- sql-server - 在 SSIS 中显示 foreach 循环迭代次数
- java - 我可以用方括号声明一个变量吗?
- r - 在R中没有for循环的情况下获取重叠数据帧值的计数
- modelica - 将模型实例化为参数时的固定属性
- css - 更改属性样式,删除项目符号列表
- c++ - 一个数组,其中每个元素都是数字的除数和下一个数字的除数
- mysql - 编写 SQL 查询,列出出现在 cd 表中所有 cd 上的所有录音的录音 id 和录音日期