1
I have two images, I want them to change when:
- User hovers over it, implemented with CSS, works well.
- If the user does not mouse the img, when scrolling the page the img changes, I implemented a jquery and I am having difficulties because the code changes all the img of the page, and I want you to change a specific img.
In html:
<a href="#">
<img src="assets/images/sujo.svg" class="defaultPic" alt="Carro Sujo">
<img src="assets/images/limpo.svg" class="altPic" alt="Carro Limpo">
</a>
Scritpt I’m trying to use:
$(window).scroll(function () {
//console.log($(document).scrollTop());
if ($(document).scrollTop() >= 10) {
$('img').attr('src', 'assets/images/limpo.svg');
} else {
$('img').attr('src', 'assets/images/sujo.svg');
}
});
Know where you can find your error? in the attr selector, create an id for each <img> and add the id of the images in the selector, example: $('#imagem1'). attr('src', 'Assets/images/limpo.svg');
– Renan