Skip to content

Instantly share code, notes, and snippets.

@blackakula
Created April 4, 2018 00:24
Show Gist options
  • Save blackakula/c5a8fbd0c23ac2e9631ba086b24b2f08 to your computer and use it in GitHub Desktop.
Save blackakula/c5a8fbd0c23ac2e9631ba086b24b2f08 to your computer and use it in GitHub Desktop.
Browser window document width and height (very little piece of code, that saved my life)
const maxElementProperty = (element, ...properties) => Math.max(
0,
...properties.map(property => element ? element[property] : 0)
);
const realSize = (...documentElementProps) => (...bodyProps) => Math.max(
0,
maxElementProperty(document.documentElement, ...documentElementProps),
maxElementProperty(document.body, ...bodyProps)
);
export const width = realSize('clientWidth', 'offsetWidth', 'scrollWidth')('scrollWidth', 'offsetWidth');
export const height = realSize('clientHeight', 'offsetHeight', 'scrollHeight')('scrollHeight', 'offsetHeight');
export default {width, height};
@blackakula
Copy link
Author

blackakula commented Apr 4, 2018

Than in code:

import {width, height} from 'browser';

console.log(`Browser width: ${width}px`);
console.log(`Browser height: ${height}px`);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment