How can i change image source on hover with only CSS
i tried but was able to find only answers with javascript.
this is my code:
<img src="">
i want to change image to 1.gif when user mouse hover it.
you can target the image first. and then from there when user mouser over the image you can change src attribute
const img = document.getElementById('imgId');
img.addEventListener('mouserover', callback);
function callback(event) {
if (img.src=== '1.gif') {
img.src = '2.gif';
}
}
img.addEventListener("mouseout", anotherCallback);
function anotherCallback(event) {
if (img.src !== "1.gif") {
img.src = '1.gif';
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.