jquery - 表格的粘性按钮宽度,位于页脚上方
问题描述
我在表格中有一个按钮,我想贴在屏幕底部,当页脚出现在页脚上方时滚动。按钮也必须与表格的宽度相匹配
我有按钮可以粘在页面底部并在它出现时位于页脚上方但无法获得与表格匹配的宽度,只有当它位于页脚时
https://codepen.io/ryannewell/pen/mdbparX
<body>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<style>
.sidebar {
height: 300px;
background-color: black;
}
.table {
height: 600px;
width: 600px!important;
background-color: grey;
margin-bottom: 0px;
}
.footer {
background-color:#26272b;
padding:45px 0 20px;
height: 40px;
width: 100%;
}
.sub-btn-fixed {
position: fixed;
width: 100%!important;
margin:auto;
left: 0;
right: 0;
bottom:0;
height: 45px;
background: red center top repeat-x scroll;
box-shadow: 0 0 10px rgba(0,0,0,0.2);
z-index: 1030;
border: none;
}
</style>
<div class="container">
<div class="row">
<div class="col-md-3 col-sm-6 sidebar">
<p>Sidebar</p>
</div>
<div class="col-md-9 col-sm-6 table">
<table>
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr>
<td>Centro comercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
<tr>
<td><button class="sub-btn-fixed">Submit</button></td>
</tr>
</table>
</div>
</div>
</div><!-- /container -->
<footer class="footer"></footer>
<script>
$(".sub-btn-fixed").css({'width':($(".table").width()+'px')});
function checkOffset() {
if ($('.sub-btn-fixed').offset().top + $('.sub-btn-fixed').height() >=
$('.footer').offset().top - 10) $('.sub-btn-fixed').css('position', 'absolute');
if ($(document).scrollTop() + window.innerHeight < $('.footer').offset().top) $('.sub-btn-fixed').css('position', 'fixed'); // restore when you scroll up
}
$(document).scroll(function() {
checkOffset();
});
</script>
</body>
按钮应该是表格的宽度,但应该是屏幕的 100% 宽度,除非它位于页脚上方
这个问题的 JSFiddle:https ://jsfiddle.net/4fm1rx7v/
解决方案
position: fixed;
从.sub-btn-fixed
CSS 中删除,如下所示:
.sidebar {
height: 300px;
background-color: black;
}
.table {
height: 600px;
width: 600px!important;
background-color: grey;
margin-bottom: 0px;
}
.footer {
background-color:#26272b;
padding:45px 0 20px;
height: 40px;
width: 100%;
}
.sub-btn-fixed {
width: 100%!important;
margin:auto;
left: 0;
right: 0;
bottom:0;
height: 45px;
background: red center top repeat-x scroll;
box-shadow: 0 0 10px rgba(0,0,0,0.2);
z-index: 1030;
border: none;
}
此外,将colspan="3"
属性添加到 HTML 中,如下所示:
<div class="container">
<div class="row">
<div class="col-md-3 col-sm-6 sidebar">
<p>Sidebar</p>
</div>
<div class="col-md-9 col-sm-6 table">
<table>
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr>
<td>Centro comercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
<tr >
<td colspan="3"><button class="sub-btn-fixed">Submit</button></td>
</tr>
</table>
</div>
</div>
</div><!-- /container -->
<footer class="footer"></footer>
</body>
推荐阅读
- c# - 进程停止时如何取消虚拟主机
- linux - Flutter 文档/教程中为 Linux 编写插件(尝试编写桌面相机插件)
- c# - 需要帮助使用 C# 代码执行 SCCM 任务序列
- python - 使用 Scrapy 将多个页面的数字相加
- graphdb - 是否可以在 graphdb 工作台可视化图中显示文字?
- c# - 无法在控制台应用程序中使用位图
- flutter - 用不同的 ListView 项目分离主屏幕
- php - 在 laravel Blade foreach 循环期间格式化 DATETIME 以仅显示时间
- html - 我的 jquery execCommand 没有按我的意愿工作
- python - Webscraping 点击按钮 Selenium