scrollbar.ts 757 B

12345678910111213141516171819202122232425262728293031
  1. import { isMobile } from '../is_mobile';
  2. let cachedScrollbarWidth: number | null = null;
  3. const getActualScrollbarWidth = () => {
  4. const outer = document.createElement('div');
  5. outer.style.visibility = 'hidden';
  6. outer.style.overflow = 'scroll';
  7. document.body.appendChild(outer);
  8. const inner = document.createElement('div');
  9. outer.appendChild(inner);
  10. const scrollbarWidth = outer.offsetWidth - inner.offsetWidth;
  11. outer.remove();
  12. return scrollbarWidth;
  13. };
  14. export const getScrollbarWidth = () => {
  15. if (cachedScrollbarWidth !== null) {
  16. return cachedScrollbarWidth;
  17. }
  18. const scrollbarWidth = isMobile(window.innerWidth)
  19. ? 0
  20. : getActualScrollbarWidth();
  21. cachedScrollbarWidth = scrollbarWidth;
  22. return scrollbarWidth;
  23. };