I am completely new to javascript, but I want to learn it rather than jQuery to get some fundamental knowledge. The function I want to create should animate an arrow to turn upwards when it passes 50% of the page. In addition to this, the function that it calls when upside down should change.
So: upArrow onClick(scrollUp)
downArrow onClick(scrollDown)
I’m sure I sound stupid, but maybe someone can understand what I’m trying to explain.
You can either write the
imageelement.srcproperty to point to a different arrow, or, for extra smoothness, use a CSS sprite containing both arrows and writeelement.style.backgroundPositionto change the displayed image without having to load a new image.You can assign a new function to
imageelement.onclickto change what happens when you click on it, but usually it is simpler to have one click-function that decides what to do based on state.eg:
Browser compatibility notes:
window.onscroll()is because IE doesn’t fire the scroll event whenscrollTo()is used. Bothhtmlandbody‘s scroll offsets have to be added together inisHalfwayDown()because the browsers unfortunately don’t agree on which element represents the viewport.