JavaScript reflection example

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!

You can see a jQuery Reflection work around example here

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.

14 Comments Leave a comment

  1. Florian 2 December 2008 at 1:44 pm #

    Just great!
    Thank you very much. I have been suffering with this problem like forever.

  2. kel 12 December 2008 at 5:32 pm #

    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

  3. Christophe Beyls 16 December 2008 at 5:26 am #

    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

  4. Dick Dekker 16 December 2008 at 10:29 pm #

    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

  5. Dick Dekker 16 December 2008 at 10:53 pm #

    ehm…let’s answer that myself:

    reflection.style.marginTop = ‘2px’;

    Cheers,

    Dick

  6. Christophe Beyls 17 December 2008 at 2:47 am #

    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
    }

  7. [...] Samples Image Reflection help | terms of service | privacy | report a bug | flag as objectionable Hosted [...]

  8. Edward Williams 25 February 2009 at 2:11 pm #

    Is there a compatibility fix for Safari 4 Beta, the reflections no longer work!

  9. Rob 25 February 2009 at 3:21 pm #

    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.

  10. Cuba Mukundan 17 August 2009 at 11:50 am #

    Thnks Mr Christophe Beyls for the wonderfull script

  11. Adam 14 October 2009 at 4:23 am #

    Looks like Safari 4 is not working. Any ideas? Great plugin by the way.

  12. Rob 14 October 2009 at 9:43 am #

    Not sure why it doesn’t work in Safari. May be a problem with the reflection plug in as a whole?

  13. Stefan 3 November 2009 at 4:54 pm #

    doesn’t work correctly in Safari 4.0.3. There’s no opacity. In IE7 the reflection doesn’t even show up.

  14. Rob 3 November 2009 at 6:20 pm #

    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.

Leave a Reply