Horizontal Scroll Indicator

By adamlubek

This recipe demonstrates the creation of a horizontal scroll indicator.

Example Code

( StackBlitz )

// RxJS v6+
import { fromEvent } from 'rxjs';
import { throttleTime, tap } from 'rxjs/operators';

const scrollIndication = document.getElementById('indication');
const getScrollWidth = () => {
  const doc = document.documentElement;
  // https://www.w3schools.com/howto/howto_js_scroll_indicator.asp
  const winScroll = doc.scrollTop;
  const height = doc.scrollHeight - doc.clientHeight;

  return (winScroll / height) * 100;
};
const setScroll = _ => (scrollIndication.style.width = getScrollWidth() + '%');

fromEvent(document, 'scroll')
  .pipe(throttleTime(20), tap(setScroll))
  .subscribe();

html

Operators Used

Last updated