1
0
mirror of https://github.com/hakimel/reveal.js.git synced 2025-08-13 01:54:21 +02:00

refactoring, fix preload distance

This commit is contained in:
Hakim El Hattab
2023-09-14 15:17:07 +02:00
parent eaf5f61318
commit 4da6f6b30f

View File

@@ -199,23 +199,43 @@ export default class Reader {
onScroll() { onScroll() {
const viewportElement = this.Reveal.getViewportElement(); const viewportElement = this.Reveal.getViewportElement();
const viewportHeight = viewportElement.offsetHeight;
const scrollTop = viewportElement.scrollTop; const scrollTop = viewportElement.scrollTop;
const scale = this.Reveal.getScale(); const scale = this.Reveal.getScale();
this.pageMap.forEach( ( page, i ) => { this.pageMap.forEach( ( page, i ) => {
const isPartiallyVisible = scrollTop >= page.top && scrollTop < page.top + page.bottom; const isWithinPreloadRange = scrollTop + viewportHeight >= page.top - viewportHeight && scrollTop < page.top + page.bottom + viewportHeight;
const isPartiallyVisible = scrollTop + viewportHeight >= page.top && scrollTop < page.top + page.bottom;
if( isPartiallyVisible ) { // Preload content when it appears within range
// Load the slide content when it becomes visible if( isWithinPreloadRange ) {
if( !page.visible ) { if( !page.preloaded ) {
page.visible = true; page.preloaded = true;
this.Reveal.slideContent.load( page.slideElement ); this.Reveal.slideContent.load( page.slideElement );
this.Reveal.slideContent.startEmbeddedContent( page.slideElement ); }
}
else if( page.preloaded ) {
page.preloaded = false;
this.Reveal.slideContent.unload( page.slideElement );
} }
if( page.totalHeight > page.pageHeight ) { // Play slide content when the slide becomes visible
const scrollProgress = Math.min( ( scrollTop - page.top ) / page.scrollHeight, 1 ); if( isPartiallyVisible ) {
if( !page.playing ) {
page.playing = true;
this.Reveal.slideContent.startEmbeddedContent( page.slideElement );
}
}
else if( page.playing ) {
page.playing = false;
this.Reveal.slideContent.stopEmbeddedContent( page.slideElement );
}
// Handle scroll freezing and triggers for slides in view
if( isPartiallyVisible && page.totalHeight > page.pageHeight ) {
let scrollProgress = ( scrollTop - page.top ) / page.scrollHeight;
scrollProgress = Math.max( Math.min( scrollProgress, 1 ), 0 );
// Fix the slide to the top of the viewport while we're in its // Fix the slide to the top of the viewport while we're in its
// scrollable region // scrollable region
@@ -233,12 +253,6 @@ export default class Reader {
} }
} ); } );
} }
}
else if( page.visible ) {
page.visible = false;
this.Reveal.slideContent.unload( page.slideElement );
this.Reveal.slideContent.stopEmbeddedContent( page.slideElement );
}
} ); } );
} }