I have come across the following code which I suspect is over-complicated in order to support legacy browsers. I imagine it can be simplified for modern browsers but I’m not sure how. I understand the ternary operator, but not the double one used here.
var thisTop =
(el.style.top || el.style.pixelTop || el.offsetTop || 0) -
(window.pageXOffset
? window.pageYOffset
: document.documentElement.scrollTop
? document.documentElement.scrollTop
: document.body.scrollTop);
Can anyone help? Thanks
They’re listed in order of preference, so you can just use window.pageYOffset, which is good for everything beyond IE8.
1 Like
Thanks, Paul. And do I need all of
(el.style.top || el.style.pixelTop || el.offsetTop || 0)
?
Nope, you can just use:
var thisTop = window.pageYOffset;
1 Like
Thanks, but that doesn’t quite work. My full code is in this pen based on one by @PaulOB with JS by Fellgall
Oh wait - there’s a subtraction in there. I didn’t see that before.
You’ll be wanting:
var thisTop = el.offsetTop - window.pageYOffset;
2 Likes
Just the ticket - thanks!
Gandalf
Closed
July 18, 2020, 6:56pm
8
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.