Register now or login here to start promoting your blog and your favourite articles.
Create a Stunning Sliding Door Effect with jQuery
31 Aug 2009 - 28 Comments
Learn how to make a stunning four corners sliding effect easily with jQuery. It's elegant, unique and really cool solution for thumbnail gallery. This tutorial includes detailed explanation that will guide you through the whole process.
Author: kevin | Source: queness
Demonstration Download

Introduction

Continue from our previous thumbnail effect series. This is the third one. If you missed the previous two, you can visit the following links:

This tutorial is going to be awesome! I got inspired by my friend's Image Splitting Effect from Tutsvalley. I decided to take one step further, create a four corners sliding door effect.

Basically, this is what it does:

  • Grab all the div with qitem class, find the image and replace it with four DIVs (four corners)
  • Each of the corner will have the same background image (grabbed from qitem class's child image element) but with different background position: top left, top right, bottom left and bottom right. It will still look like a whole image, however, the image is actually replaced by DIVd and divided into four portions.
  • Mouse over and mouse out event will move the corners diagonally out and in according to the mouse event.
  • Caption will be displayed after the corners have moved out.
  • If user clicked on it, it will execute the link assign to the item.

The following image illustrate how it works:

jQuery Slicing Structure

1. HTML

It's a good practise to keep HTML as simple as possible. HTML always can be simplified using CSS and Javascript. As a result from the simplification, This is the html for a single item. We can duplicate it into more than one.

In the demo, we can see there are total of 9 items. Please be aware that, in this tutorial, we are using float:left and clear:both in CSS to create the multi column and rows. Make sure the floating is cleared to ensure it doesnt mess up your existing website layout.

<div class="qitem">
	<a href="http://www.google.com"><img src="1.gif" alt="Test 1" title="" width="126" height="126"/></a>
	<span class="caption"><h4>Night Club</h4><p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</p></span>
</div>

2. CSS

In CSS section, it's quite simple. We have a class called qitem. qitem must be overflow:hidden to hide those four corners, float:left to create the milti rows and columns and cursor:hand/ cursor:pointer to let uses know they can click on it.

For caption, position:absolute is a must to se the z-index. Caption should be layered under the four corners

We need to set the generic setting for all the corners. We can do this in javascript, but it would be more efficient and simpler to do it with CSS. You see, the good thing in web development is, one problem can have different type of solutions.

body {
	font-family:arial;	
}

.qitem {
	/* width and height must be even number */
	width:126px;
	height:126px;	
	
	/* some styling for the item */
	border:4px solid #222;	
	margin:5px 5px 5px 0;
	background: url('bg.gif') no-repeat;
	
	/* make sure the four divs are hidden after changing the position */
	overflow:hidden;
	
	/* absolute position enabled for children elements*/
	position:relative;
	
	/* display item in single row */
	float:left;
	
	/* hand symbol for ie and other browser */
	cursor:hand; cursor:pointer;
}

	.qitem img {
		border:0;
	}

	/* styling for caption, position absolute is a must to set the z-index */
	.qitem .caption {
		position:absolute;
		z-index:0;	
		color:#ccc;
		display:block;
	}

		.qitem .caption h4 {
			font-size:12px;
			padding:10px 5px 0 8px;
			margin:0;
			color:#369ead;
		}

		.qitem .caption p {
			font-size:10px;	
			padding:3px 5px 0 8px;
			margin:0;
		}



/* Generic setting for corners */
.topLeft, .topRight, .bottomLeft, .bottomRight {
	/* allow javascript to move the corners */
	position:absolute;
	background-repeat: no-repeat; 
	z-index:200;
}

/* set the background position for different corners */
.topLeft {
	background-position: top left; 	
} 

.topRight {
	background-position: top right; 
} 

.bottomLeft {
	background-position: bottom left; 
} 

.bottomRight {
	background-position: bottom right; 
}

.clear {
	clear:both;	
}

3. Javascript

Javascript is abit messy this time. But I have isolated all the configurable setting on the top to make it easier to change. Basically, this is what it does:

  1. When the document is ready, it counts all the value for all corners
  2. Using each() to loop through the the item, in the loop:
    • Grab all the details: url, image path
    • Remove the img element
    • Append the four corners
    • Set the background image (image path grabbed from img element) to all the corners
    • Set the position to all the corners
    • * Due to IE7, you need to make sure the image/qitem size is even number, so that the divided value is whole number. IE7 doesn't render fraction value properly.
  3. On mouse over event: Set correct values to all the corners and animate them so that the corners are moving away from the qitem.
  4. On mouse out event: reset all the corners and animate them so that the corners are moving back to where there were.
  5. On click: execute the link (grabbed from anchor element).

The following image illustrate the calculation:

jQuery Slicing Structure

In the javascript below, we have a section for the calculation. If you look at the image above carefully, assume the item width is 126px, and values that we need to generate is 0px, 63px, -63px and 126px

Hence, (the item size we used in this tutorial is 126px)

  • var neg = Math.round($('.qitem').width() / 2) * (-1);
    (126 / 2) * (-1), neg = -63px
  • var pos = neg * (-1);
    neg = -63px, pos = (-63) * -(1), pos = 63px
  • var out = pos * 2;
    pos = 63px, pos = 63 * 2, pos = 126px

Now, we know all the mathematic calculations, how it works. It's time to combine everything into one piece!

	
$(document).ready(function() {

	//Custom settings
	var style_in = 'easeOutBounce';
	var style_out = 'jswing';
	var speed_in = 1000;
	var speed_out = 300;	

	//Calculation for corners
	var neg = Math.round($('.qitem').width() / 2) * (-1);	
	var pos = neg * (-1);	
	var out = pos * 2;
	
	$('.qitem').each(function () {
	
		//grab the anchor and image path
		url = $(this).find('a').attr('href');
		img = $(this).find('img').attr('src');
		
		//remove the image
		$('img', this).remove();
		
		//append four corners/divs into it
		$(this).append('
'); //set the background image to all the corners $(this).children('div').css('background-image','url('+ img + ')'); //set the position of corners $(this).find('div.topLeft').css({top:0, left:0, width:pos , height:pos}); $(this).find('div.topRight').css({top:0, left:pos, width:pos , height:pos}); $(this).find('div.bottomLeft').css({bottom:0, left:0, width:pos , height:pos}); $(this).find('div.bottomRight').css({bottom:0, left:pos, width:pos , height:pos}); }).hover(function () { //animate the position $(this).find('div.topLeft').stop(false, true).animate({top:neg, left:neg}, {duration:speed_out, easing:style_out}); $(this).find('div.topRight').stop(false, true).animate({top:neg, left:out}, {duration:speed_out, easing:style_out}); $(this).find('div.bottomLeft').stop(false, true).animate({bottom:neg, left:neg}, {duration:speed_out, easing:style_out}); $(this).find('div.bottomRight').stop(false, true).animate({bottom:neg, left:out}, {duration:speed_out, easing:style_out}); }, function () { //put corners back to the original position $(this).find('div.topLeft').stop(false, true).animate({top:0, left:0}, {duration:speed_in, easing:style_in}); $(this).find('div.topRight').stop(false, true).animate({top:0, left:pos}, {duration:speed_in, easing:style_in}); $(this).find('div.bottomLeft').stop(false, true).animate({bottom:0, left:0}, {duration:speed_in, easing:style_in}); $(this).find('div.bottomRight').stop(false, true).animate({bottom:0, left:pos}, {duration:speed_in, easing:style_in}); }).click (function () { //go to the url window.location = $(this).find('a').attr('href'); }); });

Conclusion

Like this tutorials? You can express your gratitude by visiting my sponsors on the sidebar, buy me a drink in the bottom of the page or, just bookmark it and help me to spread this tutorial to the rest of the people who you think they are interested! :) Thanks!

Demonstration Download

Copyright & Usage

The effects and techniques demonstrated in tutorials on Queness can be used in whatever manner you wish without attribution. You cannot copy whole tutorials (unless permission is given), either in English or translated to another language.

Share This Post to Support Me! :)


Comments

Will H. on 31 Aug 2009 says:
We have something like this installed on our custom board books Web site, but we are using it differently. Take a look, http://www.hullabaloostories.com
Boba (tutsvalley.com) on 31 Aug 2009 says:
@Kevin - You took it few steps further, not one :)

@Will H. - You got a basic image slider (also known as carousel)
Dave Sparks on 1 Sep 2009 says:
Nice effect, well executed, thanks.
Sam Benson on 1 Sep 2009 says:
Kudos on this Kevin, really nice effect.

One question though - why set [gotoEnd] to true within the stop() effect?

Wouldn't setting it to false stop the "jumpy" feeling when you hover off then on again?

Sam
Marco on 1 Sep 2009 says:
Awesome! I'll use it for our ranks page!
kevin on 1 Sep 2009 says:
Hi Sam, maybe it's an intensive animation, it's kinda laggy after set it to false. I argued with my friend regarding queue:false as well. But I guess this is the best result. However, different ppl have different ideas and sense of design, you are free to mod it :)
Kelkirpi on 4 Sep 2009 says:
Nice effect, thanks for sharing..
But i want to use this with target=_blank ..

How i do it?
kevin on 4 Sep 2009 says:
Hi Kelkirpi, you can change this line in click event:

window.location = $(this).find('a').attr('href');

to

window.open($(this).find('a').attr('href'), '_blank');

it should work :)
Kelkirpi on 4 Sep 2009 says:
Thanks Kevin, this is excellent.. It's works!
dlv on 4 Sep 2009 says:
awesome!
i've never imagine implementing something like that with jquery! no way, You rock!

1/
Will H. on 11 Sep 2009 says:
Thanks Boba. I really like what jQuery can do.
Janko on 11 Sep 2009 says:
Kudos for the idea :)
bobv on 11 Sep 2009 says:
Wow!! I love it! It's cutie pie!
Michael Willy on 13 Sep 2009 says:
Very cool
christophe on 14 Sep 2009 says:
Wow, nice stuff!
g1lb0 on 2 Oct 2009 says:
I tried this awsome effect with joomla! but there is some problem between mootools.js and the script of this effect.
How can i fix the problem?
Boba on 4 Oct 2009 says:
g1lb0, this is how - http://tutsvalley.com/daily_tip/using-multiple-versions-of-jquery-on-the-same-page/

:)
Martin on 2 Nov 2009 says:
A great idea! Loved the easing on mouse out. Would look great in a portfolio.
Oliver Mezquita on 3 Nov 2009 says:
Very impressive effect! Taking note!
Davetiye on 6 Nov 2009 says:
a great application. I would really like to congratulate you. More waiting. Thank you.
bali website design on 14 Nov 2009 says:
awsome effect, very creative
Raj on 15 Nov 2009 says:
Fantastic! Well done effect!
mrak911 on 22 Nov 2009 says:
Merci pour linformation, je la cherchais longtemps!
Max on 22 Nov 2009 says:
And what would that do to the x-axis was one division, and the y-axis the other? For example I have a rectangle 400px to 200px.
N-Design on 30 Dec 2009 says:
great effect
Cheap Apple iPod Touch on 11 Jan 2010 says:
Nice effect, thanks for sharing..
But i want to use this with target=_blank ..

How i do it?
Chris Allen on 1 Feb 2010 says:
Many, thanks, I was wondering if you could help ?

I want to use this effect ...

http://www.chrisallen.eu/jquery/portfolio1.html

with the above effect ?

http://www.chrisallen.eu/jquery/portfolio2.html

Seems to stop working ? multiple JQuery problems possibly ?!?

Leave a comment

http://
Subscribe RSS Subscribe RSS, Keep yourself updated with queness latest posts!
Pixel Crayons Buy and Sell Flash Buy and Sell Flash

Buy wholesale computers directly from China at DHgate.com

Discover the tools to build your e-Commerce Site with Netfirms

  •  
  •  
  •  
  •  
  •  

Community News

Recent Comments

Random Posts


View all posts and news Back to top

About the Author

A web designer and developer who is passionate and keen on contributing to the web development industry. Feel free to say hi to me, or follow me on twitter.

Kevin Liew

Partner

  • Web and Designers
  • CSS Style
  • PV.M Garage Blogzine - (Italian)
  • TutsValley
  • Designrfix
  • CoolVibe
  • Web Developer Juice
  • Denbagus
  • Web Hosting Secret Revealed
  • PSD to HTML Conversion
  • BlueHost