Pixelbox

Welcome to Pixelbox. Thursday the 20 of November 2008

Skip to content >>

Image reflection with jQuery

JavaScript reflection example

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

Meta tags: Mac OS X, Random

Delighters

At D.Construct 08, Matt Biddulph and Matt Jones talked about Delighters. A delighter is a term coined by the Hotel industry for an item that delights a customer and increases the chance of them staying again (more info on delighters here). Items such as Plush Towels, an arrival gift or free movies could all be [...]
Meta tags: Random

Google Chrome

So yes, this is yet another post on Chrome the new browser from Google. However I am going to leave the UI and performance etc to the other million posts and focus on the strategy behind it! Firstly Google clearly have a brand advantage of creating a browser, by using Google to view the web you [...]
Meta tags: Random