<script type="text/javascript">
jQuery(document).ready(function(){
setTimeout(function(){
jQuery('.my-image').each(function(){
jQuery(this).greyScale({
fadeTime: 200,
reverse: false
});
$(this).animate({ 'opacity' : 1 }, 1000);
$(this).load(function(){
jQuery(this).greyScale({
fadeTime: 200,
reverse: false
});
$(this).animate({ 'opacity' : 1 }, 1000);
});
});
}, 200);
});
</script>
In the above example I use greyScale() function that duplicates image into canvas and keeps both versions (grey = default, color = hover).
It works fine 99% of the time BUT when browser first runs it sometimes fails to load 1 image, 2 images or something like that. It’s like both “load” and “normal event” failed to work.
Can someone confirm if I’m doing it right? I attempt to load that event if image is already there or if it’s not there then there’s alternative “load()” to ensure it will execute once it’s loads. Logically it seems like good solution.
A good way to test whether an image has loaded or not is to try and access its dimensions, if you can get the height or width of an image, you can assume it has loaded and greyscale it. Therefore you could modify your code to do this:
In this situation, since you want the image to be greyscale first I’d recommend inserting images programmatically:
Where your
<img>tags would be, replace them with<div>tags where you add adata-srcattribute, this will contain the image’s URL.When you’re document has loaded, use a script that goes through all the
<div>tags and insert the<img>tags within the<div>tags, for example: