Skip to main content

Get the browser window's current scroll position in JavaScript.

"use strict";

/**
 * Get scroll position
 *
 * Use pageXOffset and pageYOffset if they are defined, otherwise scrollLeft and
 * scrollTop. You can omit el to use a default value of window.
 *
 * @example
 * getScrollPos() -> {x: 0, y: 200}
 *
 * @return {Object} An object containing the scroll position
 */
var getScrollPos = function (el) {
    if (el === void 0) { el = window; }
    return ({
        x: el.pageXOffset !== undefined ? el.pageXOffset : el.scrollLeft,
        y: el.pageYOffset !== undefined ? el.pageYOffset : el.scrollTop
    });
};

//
// es6 version
const getScrollPos = (el = window) => ({
    x: el.pageXOffset !== undefined ? el.pageXOffset : el.scrollLeft,
    y: el.pageYOffset !== undefined ? el.pageYOffset : el.scrollTop
});