Skip to content

Commit 9a34df4

Browse files
committed
Fix: Error on scrolling when reading
1 parent 72813f7 commit 9a34df4

File tree

2 files changed

+60
-61
lines changed

2 files changed

+60
-61
lines changed

scripts/dom.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1491,7 +1491,7 @@ module.exports = {
14911491
addSepToEnd: addSepToEnd,
14921492
indexPathControlUpdateLastComic: indexPathControlUpdateLastComic,
14931493
indexMainPathA: function(){return indexMainPathA},
1494-
dom: function(_this, string = false, querySelectorAll = false) {
1494+
this: function(_this, string = false, querySelectorAll = false) {
14951495
return new _dom(_this, string, querySelectorAll);
14961496
},
14971497
query: function(_this) {

scripts/reading.js

+59-60
Original file line numberDiff line numberDiff line change
@@ -543,28 +543,39 @@ function disposeImages(data = false)
543543
}
544544
}
545545

546+
var rightSize = {}; // Right content size
547+
546548
function calculateView()
547549
{
548550
let contentRight = template.contentRight().get(0);
549551

550-
let content = template.contentRight().children('div');
551-
let contentWidth = template.contentRight().width();
552+
let content = contentRight.firstElementChild;
553+
let rect = content.getBoundingClientRect();
554+
555+
rightSize = {
556+
height: rect.height,
557+
width: rect.width,
558+
top: rect.top,
559+
left: rect.left,
560+
// readingRect: content.querySelector('.reading-body').getBoundingClientRect(),
561+
scrollHeight: content.scrollHeight,
562+
};
552563

553564
if(readingViewIs('slide'))
554565
{
555-
template.contentRight('.reading-body > div, .reading-lens > div > div').css({
556-
'width': (contentWidth * indexNum)+'px',
557-
'height': content.height(),
566+
dom.this(contentRight).find('.reading-body > div, .reading-lens > div > div').css({
567+
width: (rect.width * indexNum)+'px',
568+
height: rect.height,
558569
});
559570
}
560571
else if(readingViewIs('scroll'))
561572
{
562-
template.contentRight('.reading-body > div').css({
563-
'width': '100%',
573+
dom.this(contentRight).find('.reading-body > div').css({
574+
width: '100%',
564575
});
565576

566-
template.contentRight('.reading-lens > div > div').css({
567-
'width': ($('.content-right').width())+'px',
577+
dom.this(contentRight).find('.reading-lens > div > div').css({
578+
width: rect.width+'px',
568579
});
569580
}
570581

@@ -573,7 +584,7 @@ function calculateView()
573584
imagesPosition = [];
574585
imagesFullPosition = [];
575586

576-
let scrollTop = content.scrollTop() - content.offset().top;
587+
let scrollTop = content.scrollTop - rect.top;
577588

578589
for(let key1 in imagesDistribution)
579590
{
@@ -859,13 +870,13 @@ function goToIndex(index, animation = true, nextPrevious = false, end = false)
859870

860871
clearTimeout(disableOnScrollST);
861872

862-
disableOnScroll(1);
873+
disableOnScroll(true);
863874

864875
disableOnScrollST = setTimeout(function(){
865876

866-
reading.disableOnScroll(2);
877+
reading.disableOnScroll(false);
867878

868-
}, animationDurationMS);
879+
}, animationDurationMS + 200); // Add 200 of margin to avoid errors
869880

870881
content.stop(true).animate({scrollTop: (scrollTop + scrollSum)+'px'}, animationDurationMS);
871882
}
@@ -1250,7 +1261,7 @@ function applyScale(animation = true, scale = 1, center = false, zoomOut = false
12501261
if(config.readingGlobalZoom && readingViewIs('scroll'))
12511262
{
12521263
zoomingIn = true;
1253-
disableOnScroll(1);
1264+
disableOnScroll(true);
12541265

12551266
if(originalRect === false)
12561267
{
@@ -1324,7 +1335,7 @@ function applyScale(animation = true, scale = 1, center = false, zoomOut = false
13241335
scalePrevData.tranY = translateY;
13251336

13261337
calculateView();
1327-
disableOnScroll(2);
1338+
disableOnScroll(false);
13281339
zoomingIn = false;
13291340

13301341
if(scale == 1)
@@ -1780,12 +1791,9 @@ function readingViewIs(value)
17801791

17811792
var activeOnScroll = true;
17821793

1783-
function disableOnScroll(mode)
1794+
function disableOnScroll(disable = true)
17841795
{
1785-
if(mode == 1)
1786-
activeOnScroll = false;
1787-
else
1788-
activeOnScroll = true;
1796+
activeOnScroll = !disable;
17891797
}
17901798

17911799
function setReadingDragScroll(dragScroll)
@@ -3290,81 +3298,69 @@ async function read(path, index = 1, end = false, isCanvas = false)
32903298
$(window).on('mousewheel touchstart keydown', function(e) {
32913299

32923300
if(!zoomingIn && (e.type != 'keydown' || (e.type == 'keydown' && (e.keyCode == 38 || e.keyCode == 40))))
3293-
disableOnScroll(2);
3301+
disableOnScroll(false);
32943302

32953303
});
32963304

32973305
template.contentRight().children('div').on('scroll', function(e) {
32983306

32993307
if(activeOnScroll && readingViewIs('scroll'))
33003308
{
3301-
previousScrollTop = $(this).scrollTop();
3302-
let contentHeight = template.contentRight().children('div').height();
3303-
let contentPosition = (previousScrollTop + (contentHeight / 2)),
3304-
contentPositionTop = previousScrollTop,
3305-
contentPositionBottom = previousScrollTop + contentHeight;
3309+
let scrollTop = this.scrollTop;
3310+
3311+
let center = 0;
33063312

3307-
let selIndex = false, selPosition = false;
3313+
let availableScroll = rightSize.scrollHeight - rightSize.height;
3314+
let centerOffset = (availableScroll < rightSize.height ? availableScroll : rightSize.height) / 2;
33083315

3309-
let lastKey = imagesFullPosition.length - 1;
3316+
if(scrollTop < centerOffset)
3317+
center = scrollTop + (centerOffset * (scrollTop / centerOffset));
3318+
else if(scrollTop + centerOffset > availableScroll)
3319+
center = scrollTop + centerOffset + (centerOffset * (1 - (availableScroll - scrollTop) / centerOffset));
3320+
else
3321+
center = scrollTop + centerOffset;
3322+
3323+
let selIndex = false;
3324+
let closest = false;
33103325

33113326
toBreak:
33123327
for(let key1 in imagesFullPosition)
33133328
{
33143329
for(let key2 in imagesFullPosition[key1])
33153330
{
3316-
if(key1 == 0 && imagesFullPosition[key1][key2].center - contentPositionTop > 0)
3317-
{
3318-
selIndex = key1;
3331+
let position = imagesFullPosition[key1][key2];
33193332

3320-
break toBreak;
3321-
}
3322-
else if(key1 == lastKey && imagesFullPosition[key1][key2].center - contentPositionBottom < 0)
3333+
if(position.top < center && position.bottom > center)
33233334
{
3324-
selIndex = key1;
3325-
3335+
selIndex = +key1;
33263336
break toBreak;
33273337
}
33283338
else
33293339
{
3330-
var position = 0;
3331-
3332-
if(Math.abs(contentPosition - imagesFullPosition[key1][key2].top) < Math.abs(contentPosition - imagesFullPosition[key1][key2].bottom))
3333-
position = imagesFullPosition[key1][key2].top// + 16;
3334-
else
3335-
position = imagesFullPosition[key1][key2].bottom// - 16;
3336-
3337-
position = Math.abs(contentPosition - position);
3340+
let diff = Math.abs(position.center - center);
33383341

3339-
if(!selIndex || position < selPosition)
3342+
if(closest === false || diff < closest.diff)
33403343
{
3341-
selIndex = key1;
3342-
selPosition = position;
3343-
}
3344-
else if(selIndex && position > selPosition)
3345-
{
3346-
break toBreak;
3344+
selIndex = +key1;
3345+
closest = {center: position.center, diff: diff};
33473346
}
33483347
}
33493348
}
33503349
}
33513350

3352-
var imgHeight = imagesFullPosition[selIndex][0].bottom - imagesFullPosition[selIndex][0].top + (_config.readingMargin.top * 2);
3351+
let imgHeight = imagesFullPosition[selIndex][0].bottom - imagesFullPosition[selIndex][0].top + (_config.readingMargin.top * 2);
33533352

3354-
var pageVisibility = Math.floor(imgHeight / contentHeight);
3353+
let pageVisibility = Math.floor(imgHeight / rightSize.height);
33553354

33563355
maxPageVisibility = pageVisibility;
33573356

3358-
if(pageVisibility > 0)
3359-
var contentHeightRes = ((contentHeight * pageVisibility) - imgHeight) / pageVisibility;
3360-
else
3361-
var contentHeightRes = 0;
3357+
let contentHeightRes = pageVisibility > 0 ? ((rightSize.height * pageVisibility) - imgHeight) / pageVisibility : 0;
33623358

3363-
scrollPart = ((contentHeight - contentHeightRes) - contentHeight / pageVisibility);
3359+
scrollPart = ((rightSize.height - contentHeightRes) - rightSize.height / pageVisibility);
33643360

33653361
currentPageVisibility = Math.round((previousScrollTop - (imagesFullPosition[selIndex][0].top - _config.readingMargin.top)) / scrollPart);
33663362

3367-
if(currentIndex != (parseInt(selIndex) + 1))
3363+
if(currentIndex != selIndex + 1)
33683364
{
33693365
if(currentScale != 1 && !(config.readingGlobalZoom && readingViewIs('scroll')))
33703366
reading.resetZoom();
@@ -3394,8 +3390,10 @@ async function read(path, index = 1, end = false, isCanvas = false)
33943390
goToImageCL(imageIndex, true);
33953391
}
33963392

3397-
currentIndex = parseInt(selIndex) + 1;
3393+
currentIndex = selIndex + 1;
33983394
}
3395+
3396+
previousScrollTop = scrollTop;
33993397
}
34003398

34013399
});
@@ -3586,4 +3584,5 @@ module.exports = {
35863584
setShownBarHeader: function(value){shownBarHeader = value},
35873585
loadReadingMoreOptions: loadReadingMoreOptions,
35883586
currentScale: function(){return currentScale},
3587+
rightSize: function(){return rightSize},
35893588
};

0 commit comments

Comments
 (0)