gpt4 book ai didi

javascript - 带有固定标题的嵌入式表格垂直滚动

转载 作者:行者123 更新时间:2023-11-28 05:07:06 26 4
gpt4 key购买 nike

我有一个嵌入另一个div的表格,当然尺寸比外面的div大。我目前正在使用 ScrollToFixed在滚动时将标题固定在顶部,并在表格内容超出视口(viewport)时再次保留它。这行得通,但真正丑陋的是,虽然标题行是固定的,但它大于内容,因此它在左/右溢出。

请参阅the Fiddle它确实按描述工作的地方。我能想象的是以某种方式设置表格的固定高度和宽度,并且它应该可以垂直和水平滚动,因为有溢出并且第一行应该固定在滚动时。

$(document).ready(function() {
var scrollLeft = 0;
var leftRoundingRect = $('.table-body')[0].getBoundingClientRect().left;

$('.sticky').scrollToFixed({
limit: $('.user.last').offset().top,
removeOffsets: true,
zIndex: 0,
unfixed: function() {
$(this).offset({
left: leftRoundingRect - scrollLeft
});
}
});

$(".table-body").scroll(function() {
scrollLeft = this.scrollLeft;
$(".user.sticky").offset({
left: leftRoundingRect - scrollLeft
});
});
});
.tab {
padding: 20px;
border: 1px solid lightgray;
}

.inner-content {
overflow: hidden;
padding: 0 10px 0 10px;
}

.table-body {
overflow: auto;
}

.users {
white-space: nowrap;
font-size: 0;
}

.user {
font-size: 0;
border-bottom: 1px solid #d3d3d3;
border-left: 1px solid #d3d3d3;
color: #555;
width: 750px;
}

.user.odd {
background: #f4f5f7;
}

.sticky {
background: grey;
}

.bold {
color: #fff;
font-weight: 600;
}

.company,
.firstName,
.lastName,
.roles {
font-size: 18px;
border-right: 1px solid lightgray;
display: inline-block;
width: 150px;
padding: 10px;
overflow: scroll;
white-space: nowrap;
}
<div class="tab">
<div class="inner-content">
<div class="users">
<div class="user sticky">
<div class="firstName bold">First</div>
<div class="lastName bold">Last</div>
<div class="roles bold">Role</div>
<div class="company bold">Company</div>
</div>
<div class="table-body">
<div class="user">
<div class="firstName">Max</div>
<div class="lastName">Mustermann</div>
<div class="roles">admin</div>
<div class="company">HP</div>
</div>
<div class="user odd">
<div class="firstName">Melanie</div>
<div class="lastName">Musterfrau</div>
<div class="roles">readonly</div>
<div class="company">IBM</div>
</div>
<div class="user">
<div class="firstName">Max</div>
<div class="lastName">Mustermann</div>
<div class="roles">admin</div>
<div class="company">HP</div>
</div>
<div class="user odd">
<div class="firstName">Melanie</div>
<div class="lastName">Musterfrau</div>
<div class="roles">readonly</div>
<div class="company">IBM</div>
</div>
<div class="user">
<div class="firstName">Max</div>
<div class="lastName">Mustermann</div>
<div class="roles">admin</div>
<div class="company">HP</div>
</div>
<div class="user odd">
<div class="firstName">Melanie</div>
<div class="lastName">Musterfrau</div>
<div class="roles">readonly</div>
<div class="company">IBM</div>
</div>
<div class="user last">
<div class="firstName">Test</div>
<div class="lastName">Tester</div>
<div class="roles">contributor</div>
<div class="company">SAP</div>
</div>
</div>
</div>
</div>
</div>
<div style="display:inline-block; height: 1000px; padding-top:50px;">Let's Scroll Baby
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://bigspotteddog.github.io/ScrollToFixed/jquery-scrolltofixed-min.js"></script>

最佳答案

下面的代码确实像我描述的那样工作。

$(document).ready(function() {
var scrollLeft = 0;
var leftRoundingRect = $('.table-body')[0].getBoundingClientRect().left;

$(".table-body").scroll(function() {
scrollLeft = this.scrollLeft;
$(".user.sticky").offset({
left: leftRoundingRect - scrollLeft
});
});

$(".user").width($(".table-body").get(0).scrollWidth - 1);

});
.tab {
padding: 20px;
border: 1px solid lightgray;
width:220px;
}
.inner-content {
padding: 0 10px 0 10px;
width:200px;
}
.header {
overflow: hidden;
}
.table-body {
overflow: auto;
height: 100px;
}
.users {
white-space: nowrap;
font-size: 0;
}
.user {
font-size: 0;
border-bottom: 1px solid #d3d3d3;
border-left: 1px solid #d3d3d3;
color: #555;
width: 750px;
}
.user.odd {
background: #f4f5f7;
}
.sticky {
background: grey;
}
.bold {
color: #fff;
font-weight: 600;
}
.company,
.firstName,
.lastName,
.roles {
font-size: 18px;
border-right: 1px solid lightgray;
display: inline-block;
width: 150px;
padding: 10px;
overflow: scroll;
white-space: nowrap;
}
<div style="display:inline-block; height: 200px; padding-top:50px;">Other Content
</div>
<div class="tab">
<div class="inner-content">
<div class="header">
<div class="user sticky">
<div class="firstName bold">First</div>
<div class="lastName bold">Last</div>
<div class="roles bold">Role</div>
<div class="company bold">Company</div>
</div>
</div>
<div class="table-body">
<div class="user">
<div class="firstName">Max</div>
<div class="lastName">Mustermann</div>
<div class="roles">admin</div>
<div class="company">HP</div>
</div>
<div class="user odd">
<div class="firstName">Melanie</div>
<div class="lastName">Musterfrau</div>
<div class="roles">readonly</div>
<div class="company">IBM</div>
</div>
<div class="user">
<div class="firstName">Max</div>
<div class="lastName">Mustermann</div>
<div class="roles">admin</div>
<div class="company">HP</div>
</div>
<div class="user odd">
<div class="firstName">Melanie</div>
<div class="lastName">Musterfrau</div>
<div class="roles">readonly</div>
<div class="company">IBM</div>
</div>
<div class="user">
<div class="firstName">Max</div>
<div class="lastName">Mustermann</div>
<div class="roles">admin</div>
<div class="company">HP</div>
</div>
<div class="user odd">
<div class="firstName">Melanie</div>
<div class="lastName">Musterfrau</div>
<div class="roles">readonly</div>
<div class="company">IBM</div>
</div>
<div class="user last">
<div class="firstName">Test</div>
<div class="lastName">Tester</div>
<div class="roles">contributor</div>
<div class="company">SAP</div>
</div>
</div>
</div>
</div>
<div style="display:inline-block; height: 200px; padding-top:50px;">Other Content
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

关于javascript - 带有固定标题的嵌入式表格垂直滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39780280/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com