Gildas 7 лет назад
Родитель
Сommit
11a1b3b60b
2 измененных файлов с 15 добавлено и 9 удалено
  1. 2 3
      lib/lazy/web-lazy-loader-after.js
  2. 13 6
      lib/lazy/web-lazy-loader-before.js

+ 2 - 3
lib/lazy/web-lazy-loader-after.js

@@ -18,15 +18,14 @@
  *   along with SingleFile.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-/* global window, requestAnimationFrame, scrollBy, Element, document, dispatchEvent, UIEvent, clearTimeout */
+/* global window, Element, document, dispatchEvent, UIEvent, clearTimeout */
 
 (() => {
 
 	if (window._singleFile_getBoundingClientRect) {
 		Element.prototype.getBoundingClientRect = window._singleFile_getBoundingClientRect;
 		delete window._singleFile_getBoundingClientRect;
-		scrollBy(0, 1);
-		requestAnimationFrame(() => scrollBy(0, -1));
+		dispatchEvent(new UIEvent("scroll"));
 	}
 	if (window._singleFile_timeoutScroll) {
 		clearTimeout(window._singleFile_timeoutScroll);

+ 13 - 6
lib/lazy/web-lazy-loader-before.js

@@ -18,7 +18,7 @@
  *   along with SingleFile.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-/* global window, scrollBy, requestAnimationFrame, Element, document, dispatchEvent, UIEvent, setTimeout */
+/* global window, Element, document, dispatchEvent, UIEvent, setTimeout */
 
 (() => {
 
@@ -36,17 +36,24 @@
 		const right = left + boundingRect.width;
 		return { x: boundingRect.x, y: boundingRect.y, top, bottom, left, right, width: boundingRect.width, height: boundingRect.height };
 	};
-	scrollBy(0, 1);
-	requestAnimationFrame(() => scrollBy(0, -1));
+	dispatchEvent(new UIEvent("scroll"));
+	const scrollTop = document.documentElement.scrollTop;
 	scroll(SCROLL_LENGTH, document.documentElement.scrollHeight - window.innerHeight);
 
 	function scroll(offsetY, maxOffsetY) {
 		document.documentElement.__defineGetter__("scrollTop", () => offsetY);
 		document.documentElement.__defineGetter__("scrollY", () => offsetY);
 		window.__defineGetter__("pageYOffset", () => offsetY);
-		dispatchEvent(new UIEvent("scroll"));
-		if (offsetY < maxOffsetY) {
-			window._singleFile_timeoutScroll = setTimeout(() => scroll(offsetY + SCROLL_LENGTH, maxOffsetY), SCROLL_DELAY);
+		processScroll();
+
+		function processScroll() {
+			if (offsetY < maxOffsetY) {
+				offsetY += SCROLL_LENGTH;
+			} else {
+				offsetY = scrollTop;
+			}
+			dispatchEvent(new UIEvent("scroll"));
+			window._singleFile_timeoutScroll = setTimeout(processScroll, SCROLL_DELAY);
 		}
 	}