I have a simple css hover function. I hover over tree-original.jpg and it changes to tree-2.jpg. It works fine.
Now I want to make things a bit complicated. I want to have multiple “hover” state images. So let’s say I hover over an image, and the image changes every 2 seconds to a different image.
For example, I have 4 images:
tree-original.jpg
tree-2.jpg
tree-3.jpg
tree-4.jpg
When I hover over tree-original.jpg I want tree-2.jpg to appear for 2 seconds, then tree-3.jpg for 2 seconds, then tree-4.jpg for 2 seconds (as long as the mouse is hovering over the image), and then back to tree-original.jpg. So as long as I have the pointer hovering over the image, the 4 images would be alternating every 2 seconds, forever (or until I move the mouse cursor).
Is that possible to do?
<div>
<a href="home.html"></a>
</div>
div a {
width: 200px;
height: 200px;
display: block;
background-image: url('tree-original.jpg');
}
div a:hover {
background-image: url('tree-2.jpg);
}
You could use @keyframes to do this by stacking the images horizontally (in an inner div) inside the 200px width div (hide the overflow) and then use translateX to move the inner container by 200px to reveal the next image.
Keyframes will allow you to do this continuously as you hover. You’d set a duration of 8 seconds and then move the container at 25% intervals.
Is there a way to make that work with :focus for keyboard access, @PaulOB? (I played briefly with your codepen, but I haven’t had my second cup of coffee yet. )