In html
I have a button which when clicked animates the document's scrollTop
to a certain position.
The issue I'm facing is that I have to make this support down to ie9
.
So, I have the following function which returns the correct scrollTopPosition
depending on what the browser can support. This is working correctly:
function posTop(){
return typeof window.pageYOffset != 'undefined' ?
window.pageYOffset : document.documentElement.scrollTop ?
document.documentElement.scrollTop : document.body.scrollTop ?
document.body.scrollTop : 0;
}
I am using the greensock library to manage the animation, and it is implemented like this:
TweenLite.to(document.body, 1, { scrollTop: targetPos });
The issue is that even though posTop()
returns the correct value, I have to manually ensure with an if statement
that I can indeed use document.body.scrollTop
in the animation call... For example...
if (document.documentElement.scrollTop) { // for ie11 and below
var tween = TweenLite.to(document.documentElement, 1, { scrollTop: targetPos });
}
else {
var tween = TweenLite.to(document.body, 1, { scrollTop: targetPos });
}
Is there a way that I can modify posTop()
so that it returns a reference to the element property rather than its value? Or a better way to implement this without the need for the if statement everytime I want to animate scrollTop
?
Clarification:
I would like to change the if statement block
to something like:
var correctScrollTopProperty = pointer to document.body // psuedo
var correctPropertyToBeAnimated = pointer to document.body.scrollTop // pseudo
TweenLite.to(correctScrollTopProperty , 1, { correctPropertyToBeAnimated : targetPos });