Image reflection with jQuery
Sep. 16, 2008 14 Comments Posted under: Mac OS X, Random
NOTE: This isn’t working in the latest releases of browsers. Please let me know if you have updated or know of maintained versions of this code I can link to.
I have often used JavaScript to add reflections to images. Ideally this would be done server side, but often for quick presentations or mock ups it saves a lot of time.
I have previously used Reflection.js, which uses prototype. Since I now normally use jQuery I looked for a jQuery version of reflection, which I found here.
There is a big problem with the code though. If the images haven’t loaded then the reflection fails. Most of the time if you refresh the page it will work (thanks caching), but that’s not idea.
I have written a work around until the plugin is updated. The work around will run and check each image with the reflect class to see if it has finished loading (using the handy .complete). If it hasn’t it sets a flag to retry after a time out, if it has then we call the reflect function on it, and then remove the reflect class, so we don’t check it again next time. Tom improved this by using the .load function which removes the need for the time out, nice work, I have updated the examples. This works really well, especially when you have lots of large images, or users on slow connections. The code will also add the reflection to each image as it loads, rather than waiting for all of them to load.
<script type="text/javascript"> $(document).ready(function() { $("img.reflect").each(function(){ $(this).load( function(){ $(this).reflect({height: 0.3, opacity: 0.6} ); } ); }); }); </script>
Please let me know if you have any advice or improvements!
This entry was posted on Tuesday, September 16th, 2008 at 4:03 pm and is filed under Mac OS X, Random. You can leave a comment and follow any responses to this entry through the RSS 2.0 feed.
Just great!
Thank you very much. I have been suffering with this problem like forever.
hi, nice work!
I used your code on some pages and it’s perfect.
I’ve also tried to use it inside a wordpress istance but with no luck. Everything works only after a refresh. Do you think that it’s because of wordpress template hierarchy? Have you ever tried reflection with it?
kel
Hello, I’ve created a new Reflection plugin for jQuery which is smaller (< 2 KB) and does not suffer from this problem. When you call the reflect() function on a jQuery selector, it automatically delays the reflection for each image until the image is actually loaded. You can also call unreflect() at any time to remove the reflection, or cancel it if the image was not yet loaded.
More info about this plugin on this page:
http://www.digitalia.be/software/reflectionjs-for-jquery
Wonderfull solution!
However I was wondering if it is possible to add a small space fe. 2px between the picture and its reflection. Designwise this would look more natural.
Cheers,
Dick
ehm…let’s answer that myself:
reflection.style.marginTop = ‘2px’;
Cheers,
Dick
I updated my script to version 1.02. Now you can use the following CSS rule to add a small space between the image and reflection, without modifying the javascript code:
img.reflected {
margin-bottom: 2px
}
[...] Samples Image Reflection help | terms of service | privacy | report a bug | flag as objectionable Hosted [...]
Is there a compatibility fix for Safari 4 Beta, the reflections no longer work!
Indeed this no longer works in the Beta. Feel free to update and let me know the changes. It may work once out of Beta though.
Thnks Mr Christophe Beyls for the wonderfull script
Looks like Safari 4 is not working. Any ideas? Great plugin by the way.
Not sure why it doesn’t work in Safari. May be a problem with the reflection plug in as a whole?
doesn’t work correctly in Safari 4.0.3. There’s no opacity. In IE7 the reflection doesn’t even show up.
Indeed it doesn’t Stephan. I have updated to the post to say that. We no longer use the code in our production environment so I can’t justify the time to fix it.
If you would like to update it and post your patch please let me know and I’ll update the post.