That is normal since you invoke the method when the window scrolls and not when you click the button as it will invoke the color method each time you scroll. Each time you click you want to know the updated scroll position not when you scroll the page without clicking the button (hope I understood that well...). And on page load you initially set the background-color to red.
var link = $('link');
var box = $('color-box');
link.addEvent('click', function(event){
event.preventDefault();
changeBgColorBox();
});
changeBgColorBox();
function changeBgColorBox () {
var scroll = window.getScroll().y;
if (scroll > 0) {
box.setStyle('background','black');
} else {
box.setStyle('background','red');
}
}
Here is a jsfiddle: http://jsfiddle.net/Bwv3S/40/
To invoke the method only when you scroll the page you would not need to invoke the method with the link being clicked at all. You would invoke the method onload or ondomready and onscroll. http://jsfiddle.net/jLPpa/9/