I'm trying to make a sticky header + first column table. Works fine on desktop browsers.
However, when I scroll the table's x-axis on a mobile device, the position update is dragging i.e. not fast enough.
I've read various SO-threads that suggest iScroll. I'm not really sure how to use it appropriately in this case. Should intercept the tbody
scroll event, perevent the default behaviour and update the position based on iScroll's event values? Please point me in the right direction here :)
$(function() {
var $tbody = $('tbody');
$tbody.on('scroll', function(e) {
var left = $tbody.scrollLeft();
$('thead').css('left', -left);
$('tbody td:nth-child(1), thead th:nth-child(1)').css('left', left);
});
var iScroll = new IScroll($tbody[0], { probeType: 3 });
iScroll.on('scroll', function(){
console.log('not fired?');
});
});
https://jsfiddle.net/97r799gr/
To reproduce the problem, it's probably easiest for you to visit https://jsfiddle.net/97r799gr/show on your mobile. I'm using an SGS7 edge so I think this will be reproducible on pretty much any mobile device.
td:nth-child(1)
withtd:first-child
. Then, addvar $thead= $('thead');
under thevar $tbody
.$('thead').css('left', -left);
can be replaced with$thead.css('left', -left);
. Then, replace$('tbody td:nth-child(1), thead th:nth-child(1)')
with$tbody.find('td:first-child').css('left', left); $thead.find('th:first-child').css('left', left);
. This should work and speed up things a bit. You could go a bit further and store those results into a var – Throckmorton