我有一个带有水平滚动条和垂直滚动条的固定页眉滚动表,目前我可以对每个列进行排序,但是在加载页面时无法正确地保留表的水平位置。
使用下面的内联css,我从一个隐藏的输入中获取scrollAmount,该输入的值来自jquery代码,并将其输入到内联css中。
style="transform:translateX(<negative offset value of scrollAmount>)"我的问题是如何正确地维护使用CSS和jQuery的水平滚动条溢出的表的滚动位置,而无需等待页面完全加载/完成呈现?
内联风格
transform:translateX(-<?=scrollAmount()?>px)jQuery滚动定位函数
function setScroll(id_header, id_table)
{
$('div.'+id_table).on("scroll", function(){ //activate when #center scrolls
var left = $('div.'+ id_table).scrollLeft(); //save #center position to var left
$('div.'+id_header).scrollLeft(left); //set #top to var left
$('#scrollamount').val(left);
});隐藏输入代码
<input type="hidden" id="scrollamount" name="scrollamount" value=<?=scrollAmount()?>"/>PHP代码
function scrollAmount()
{
if (isset($_POST['scrollamount']))
return $_POST['scrollamount'];
return "";
}发布于 2016-06-18 22:22:11
使用下面的代码,我能够生成一个视觉上可以接受的解决方案,该解决方案使表水平偏移于用户的滚动量。
JS代码-在表完成获取/加载时在document.ready()上执行
//Translate 0px, undoing the original translation
$('table#'+ id_table).attr("style", "transform:translateX(0px)");
$('table#'+ id_header).attr("style", "transform:translateX(0px)");
//Scroll left to the desired amount as defined in the hidden input `#scrollamount`
$('div.'+ id_table).scrollLeft($('#scrollamount').val());
$('div.'+ id_header).scrollLeft($('#scrollamount').val());HTML / PHP固定的页眉滚动表(标头和表体)
<div class="summary_header">
<table id="summary_header" border=1 style="transform:translateX(-<?=scrollAmount()?>px)">
... <-----Table Header Definition
</table>
</div>
<div class="summary_table" style="overflow-x:scroll">
<table id="summary_table" style="transform:translateX(-<?=scrollAmount()?>px)">
... <--Table Body Definition
</table>
</div>发布于 2016-06-17 21:59:59
问题是,每次滚动时,.scrollLeft()都会改变,所以您只需将其设置为滚动到的位置。您可能会找到表用jQuery的位置,在开始时将其设置为一个通用变量,然后使用$('div.'+id_header).scrollLeft(globalLeftPosition)来设置它。希望这能有所帮助。
https://stackoverflow.com/questions/37890473
复制相似问题