txt.js: improved scrolling

This commit is contained in:
rlx 2016-01-10 12:56:06 +05:30
parent 1259c8974b
commit f5dfeb2ce0

View file

@ -36,24 +36,22 @@ txtjs.open = function(url) {
.on({ .on({
mousedown: function(e) { mousedown: function(e) {
var offset = 'offsetY' in e ? e.offsetY : e.layerY; var offset = 'offsetY' in e ? e.offsetY : e.layerY;
document.body.scrollTop = offset / factor; document.body.scrollTop = offset / factor + margin - 16;
//Ox.print('!', offset)
} }
}) })
.appendTo($scroll), .appendTo($scroll),
textHeight, scrollTextHeight, factor; factor, margin;
text = Ox.encodeHTMLEntities(text) text = Ox.encodeHTMLEntities(text)
.replace(/\r\n/g, '\n') .replace(/\r\n/g, '\n')
.replace(/[\r\n]/g, '<br>'); .replace(/[\r\n]/g, '<br>');
$text.html(text); $text.html(text);
$scrollText.html(text); $scrollText.html(text);
window.onresize = function() { window.onresize = function() {
textHeight = $text[0].clientHeight - window.innerHeight, margin = $text.width() * 0.1;
scrollTextHeight = $scrollText[0].clientHeight - (window.innerHeight - 32), factor = $scrollText[0].clientHeight / $text[0].clientHeight;
factor = scrollTextHeight / textHeight;
}; };
window.onscroll = function() { window.onscroll = function() {
$scroll[0].scrollTop = window.pageYOffset * factor; $scroll[0].scrollTop = (window.pageYOffset - margin + 16) * factor;
}; };
window.onresize(); window.onresize();
}); });