I have an img tag that I want to change the src when hover and it all works but i would like to add some transition so it doesn't look so rough but since it's an img src i cant target it with css.
html
<img id="bg" src="img/img1.jpg">
<div onmouseover="imgChange('img/img2.jpg'); "onmouseout="imgChange('img/img1.jpg');">
js
function imgChange(im){
document.getElementById('bg').src=(im);
}
i usually use jquery for this.
You want a crossfade. Basically you need to position both images on top of each other, and set one's
opacity
to 0 so that it will be hidden:CSS:
With a
transition
set foropacity
on the images, all we need to do is trigger it with this script:http://jsfiddle.net/Ne5zw/12/
Here is a pure css solution using css
transition
. You can use adiv
as the container and set thebackground-image
on hover.Just in case someone is curious how to actually create a transition-like effect when you are actually changing the source attribute of an image, this was the solution I came up with.
Javascript:
CSS:
This will give the illusion of 'fading to black and back' between images - even if you're using something like parallax.js where you have a data-attribute driven component that renders out into a dynamic image. Hope it helps someone.