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 seuElemento
has to be replaced with the element you want to add the image, in case I suggest you put an id in your img
and use the getElementById()
method passing the id that has been assigned.