What is the role of pageXOffset property in JavaScript?

HTMLProgramming ScriptsJavascript

If you want to get the pixels the document scrolled to from the upper left corner of the window, then use the pageXoffset and pageYoffset property. Use pageXoffset for horizontal pixels.


You can try to run the following code to learn how to work with pageXOffset property in JavaScript −

Live Demo

<!DOCTYPE html>
         div {
            background-color: green;
            height: 1500px;
            width: 1500px;
         function scrollFunc() {
            window.scrollBy(200, 200);
            alert("Horizonal: " + window.pageXOffset);
      <button onclick="scrollFunc()" style="position:fixed;">Scroll</button>
Updated on 23-Jun-2020 07:56:50