document.getElementById("first-img").addEventListener("click", function(){
document.getElementById("detail-img").innerHTML = `Detail of first img`
})
document.getElementById("second-img").addEventListener("click", function(){
document.getElementById("detail-img").innerHTML = `Detail of second img`
})
.container{
display: flex;
height: 30px;
gap: 1rem;
}
.img{
background-color: red;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="container">
<div class="img" id="first-img">This is image-1</div>
<div class="img" id="second-img">This is image-2</div>
</div>
<p id="detail-img">Click image for detail</p>
</body>
</html>
So i want to #detail-img be animated when click the image. the animation is like opacity to 0 to opacity 1, not just instant. I have try with animation css property but i was failed, nothing change. any clue to do that?
3
Answers
You can surround your detail-img with a detail-container, then apply a class to it as described in this answer, removing that class with a timeout.
A problem with animations/transitions in CSS can be that once the change has been made the system doesn’t do it again.
This snippet gets round that by changing the opacity and transition then requesting an animation frame (so the new styles are taken up) then sets the opacity and transition to the final values.
Note: the snippet has introduced a function to cut down on repetitive code. More could be done e.g. by using querySelectorAll.
112233445566778899kkll