html - 如何在特定元素内放置固定按钮
问题描述
我需要一个按钮story
在单击时滚动到顶部。
但我需要它在里面story
,而不是外面。
此外,它不能滚动,所以定位固定。
但如果是fixed
- 如何将它放在里面story
,即距离右边界 14 px story
?
.story{
position:relative;
width:50%;
margin:0 auto;
height:100vh;
background:#ddd;
overflow-y:scroll;
}
.gotop{
position:fixed;
right:14px;
bottom:0;
padding:14px 9px;
background:gold;
border-radius:45% 45% 0 0;
cursor:pointer;
}
<div class='story'>
<div class='gotop'>TOP</div>
</div>
解决方案
position: sticky和对你的标记的小改动可能会起作用。
如果您设置.gotop
为粘性并将其加载到故事的最底部。然后,您可以使用强制将其卡在底部bottom: 0
我用float
这里把它推到右边,这很丑。如果你使用 flex,你可以摆脱它,但我不知道你的其余标记。
.story {
position: relative;
width: 50%;
margin: 0 auto;
height: 70vh;
background: #ddd;
overflow-y: auto;
}
.gotop {
position: -webkit-sticky;
position: sticky;
float: right;
margin-right: 10px;
bottom: 0;
padding: 14px 9px;
background: gold;
border-radius: 45% 45% 0 0;
cursor: pointer;
}
<div class='story'>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<p>Example</p>
<div class='gotop'>TOP</div>
</div>
推荐阅读
- android - 在 Asynctask android 中使用 socket.io
- android - 如何传递数组列表
从一个片段到另一个片段 - c# - WCF - 创建自定义属性来修改属性
- c# - 如何使用 Framework 4.6 在 C# 上解析 IPAddress
- vba - 当每周按一次按钮从另一张表导入日期时,如何在列中自动填充日期?
- jinja2 - 如何使用 jinja 模板为盐栈创建宏
- java - spring-security-oauth2中的OAuth2 refresh_token逻辑实现
- java - 如何创建允许非整数值的 JSlider?
- java - Not able to generate access token from LH public api
- java - JavaFX 排序文本文件输出