10 examples of 'js get scroll position' in JavaScript

Every line of 'js get scroll position' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
465public getScrollPosition(): IScrollPosition {
466 return this._scrollable.getCurrentScrollPosition();
467}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

34function getCurrentScroll() {
35 return window.pageYOffset || document.documentElement.scrollTop;
36}
43function getScrollPosition(win) {
44 var x = 0, y = 0;
45 if (typeof win.pageXOffset == NUMBER && typeof win.pageYOffset == NUMBER) {
46 x = win.pageXOffset;
47 y = win.pageYOffset;
48 } else {
49 var doc = win.document;
50 var docEl = doc.documentElement;
51 var compatMode = doc.compatMode;
52 var scrollEl = (typeof compatMode == "string" && compatMode.indexOf("CSS") >= 0 && docEl)
53 ? docEl : dom.getBody(doc);
54
55 if (scrollEl && typeof scrollEl.scrollLeft == NUMBER && typeof scrollEl.scrollTop == NUMBER) {
56 try {
57 x = scrollEl.scrollLeft;
58 y = scrollEl.scrollTop;
59 } catch (ex) {}
60 }
61 }
62 return { x: x, y: y };
63}
101setScrollPosition (scrollPos) {
102 if (platform.inBrowser) {
103 document.body.scrollTop = scrollPos
104 }
105}
492function findScrollTop() {
493 if (!glbl.$scrollTopNode) {
494 if (glbl.$html.scrollTop() != 0) {
495 glbl.$scrollTopNode = glbl.$html
496 } else if (glbl.$body.scrollTop() != 0) {
497 glbl.$scrollTopNode = glbl.$body
498 }
499 }
500 return (glbl.$scrollTopNode) ? glbl.$scrollTopNode.scrollTop() : 0
501}
33function getScrollPos() {
34 // Handle scroll position in case of IE differently
35 if (isIE) {
36 return document.documentElement.scrollTop;
37 } else {
38 return window.pageYOffset;
39 }
40}
280getScrollTop() {
281 return (
282 window.pageYOffset ||
283 document.documentElement.scrollTop ||
284 document.body.scrollTop ||
285 0
286 )
287}
22function getScrollPosition(wnd) {
23 var data = getScrollData(wnd);
24 return data.scrollY / (data.totalHeight - data.viewportHeight);
25}
7export function getScrollTop() {
8 return window.pageYOffset !== undefined
9 ? window.pageYOffset
10 : (document.documentElement || document.body.parentNode || document.body)
11 .scrollTop
12}
176function getScroll(element, side = 'top') {
177 const upperSide = side === 'top' ? 'scrollTop' : 'scrollLeft';
178 const nodeName = element.nodeName;
179
180 if (nodeName === 'BODY' || nodeName === 'HTML') {
181 const html = window.document.documentElement;
182 const scrollingElement = window.document.scrollingElement || html;
183 return scrollingElement[upperSide];
184 }
185
186 return element[upperSide];
187}

Related snippets