javascript - 如何在特定的 div 中定位修复按钮?
问题描述
按钮显示position: fixed
仅在第二个 div 内具有属性。当我滚动到第一个或最后一个 div 时,按钮不应该固定在它们上,这意味着按钮应该只在第二个 div 内可见,我该怎么做?
.one{
height:600px;
width: 100%;
background-color: red;
}
.two{
height:600px;
width: 100%;
background-color: yellow;
}
.three{
height:600px;
width: 100%;
background-color: pink;
}
.mybutton{
width:80px;
height: 20px;
position: fixed;
right:10px;
top:100px;
}
<html>
<head>
<body>
<div class="one">
<button class="mybutton">Click</button>
</div>
<div class="two"></div>
<div class="three"></div>
</body>
</head>
</html>
解决方案
这是一种方法,尽管我不知道它是否是您要寻找的方法。position:sticky
在按钮和position:relative
容器上使用( .two
)
.one {
height: 600px;
width: 100%;
background-color: red;
}
.two {
height: 600px;
width: 100%;
background-color: yellow;
position: relative;
}
.three {
height: 600px;
width: 100%;
background-color: pink;
}
.mybutton {
width: 80px;
height: 20px;
position: sticky;
right: 10px;
top: 100px;
}
<div class="one"></div>
<div class="two"><button class="mybutton">Click</button></div>
<div class="three"></div>
推荐阅读
- javascript - 使用扩展运算符从 API 操作/过滤对象的道具
- lua - 如何防止 renderBlocks() 函数进入无限循环?
- c# - 执行 .net 方法登录某些服务器而不是其他服务器
- google-data-studio - 如何在 Data Studio 中使用嵌入式报表创建 RLS 过滤器
- flutter - 如何在 shared_preferences flutter 中保存 bool 类型的数据
- python-3.x - 如何以多平台方式(win/Unix)处理文件路径?
- java - 通过 prunsrv 安装的 Windows 服务抛出错误代码 1
- django - Django Rest,如何更新视图集中对象的单个字段?我得到必填字段的 IntegrityError
- azure - Outlook 添加重定向到浏览器
- c++ - 地图查找错误:无法将对转换为枚举值