With jquery
$(window).scroll(function () {
//console.log($(document).scrollTop());
if ($(document).scrollTop() >= 10) {
$('img').attr('src', 'imagem_2.png');
} else {
$('img').attr('src', 'imagem_1.png');
}
});
With JS pure
var onScrollHandler = function() {
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
if (scrollTop >= 10) {
seuElemento.src = "imagem2.png"
}else{
seuElemento.src = "imagem1.png"
}
};
object.addEventListener ("scroll", onScrollHandler);
*Note that with javascript the seuElemento
has to be replaced by the element you want to add the image, in case I suggest you to put an id in your img
and use the method getElementById()
passing the id that was assigned.
Thank you very much!
– Lucas de Carvalho