admin管理员组

文章数量:1334821

I have searched high and low for an answer for this question and I FINALLY found it. I am writing this to hopefully save a future programmer a lot of time and frustration.

Please ment below if you would like to see something added or have any questions and I will add it. This will bee a more plete answer as people bring forth issues.

I have searched high and low for an answer for this question and I FINALLY found it. I am writing this to hopefully save a future programmer a lot of time and frustration.

Please ment below if you would like to see something added or have any questions and I will add it. This will bee a more plete answer as people bring forth issues.

Share Improve this question edited Apr 17, 2015 at 19:58 Noah Gary asked Apr 17, 2015 at 19:49 Noah GaryNoah Gary 96012 silver badges26 bronze badges 5
  • 6 if you really think that this can help anyone and for this reason you have wrote it, then you should paste the solution as an answer,, not in question – A.B Commented Apr 17, 2015 at 19:51
  • 1 Your heart is in the right place :) - it's only your solution that is not... – LcSalazar Commented Apr 17, 2015 at 19:53
  • 1 Furthermore , you will need to mark it accepted in order to get the (answered) effect... – A.B Commented Apr 17, 2015 at 19:56
  • I have moved the solution to the answer section. Thanks. I cannot accept my own answer for two days so... until then. – Noah Gary Commented Apr 17, 2015 at 19:56
  • please vote me back up if I have corrected the problem you down voted me for. – Noah Gary Commented Sep 25, 2015 at 21:24
Add a ment  | 

1 Answer 1

Reset to default 9

This is a solution to find/detect the scroll bar position on the three major browsers(IE, Chrome, Firefox). Let me know if you find a version of these browsers that will not work and I will edit this answer to include that info.

Older versions of chrome:
var scrollTop = document.body.scrollTop; //Chrome
var scrollLeft = document.body.scrollLeft; //Chrome

Chrome > v.78.0.3904.97 (That I know of)
var scrollTop = window.scrollY; //Chrome
var scrollLeft = window.scrollX; //Chrome

Other Browsers:
var otherScrollTop = document.documentElement.scrollTop; //IE & Firefox
var otherScrollLeft = document.documentElement.scrollLeft; //IE & Firefox

These variables will detect the scroll position for the whole page.

As @Eloy Pineda Mentioned: You can write this more concisely with:

var scrollTop = window.scrollY || document.body.scrollTop || document.documentElement.scrollTop;
var scrollLeft = window.scrollX || document.body.scrollLeft || document.documentElement.scrollLeft;

本文标签: javascriptCrossbrowser Detecting scroll position JS onlyStack Overflow