if I have the following simple js/knockout code:
.js (viewModel):
var image = ko.observable('http://placehold.it/300x150');
view:
<img data-bind={attr: {src: image}} />
How do I create a ko binding handler that will perform a simple cross-fade when the image property changes?
I think the best solution is to create another image and fade it in.
Here is a simple binding handler that will create a new image (or a div with a background image in this case) within the bound element every time the source observable is updated and fade to it with a CSS3 animation:
It requires some css in place for the opacity to get animated:
To use, bind it to a div container:
The advantage with this approach is that it is quite easy to substitute in more complex animations like sliding by just playing with the css.
Here is a fiddle for it!