scrollToBottom.js 463 B

123456789101112131415
  1. export default async (timeout, viewportN) => {
  2. await new Promise((resolve) => {
  3. let totalHeight = 0, distance = 200, duration = 0, maxHeight = window.innerHeight * viewportN;
  4. const timer = setInterval(() => {
  5. duration += 200;
  6. window.scrollBy(0, distance);
  7. totalHeight += distance;
  8. if (totalHeight >= document.body.scrollHeight || duration >= timeout || totalHeight >= maxHeight) {
  9. clearInterval(timer);
  10. resolve();
  11. }
  12. }, 200);
  13. });
  14. };