Create a Vertical, Horizontal and Diagonal Sliding Content Website with jQuery

Written by Kevin Liew on 14 Jul 2009
708,128 Views • Tutorials

Introduction

Content Sliding website is one of the famous and creative design techniques for portfolio website. One thing, please make sure you put the menu on every single page to avoid confusion/dizziness.

  • You should put the navigation menu in every single page
  • Or, fix the position of the menu
  • Let your visitors know exactly what section they're reading, like highlight the selected menu item and have a clear title

In this tutorial, we will learn to scroll your web content vertically, horizontally and plus diagonally! Don't worry, we won't rely on javascript too much, we will use css/html for the layout and javascript only do the scrolling. And hey, it will still work even if your browser doesn't support javascript.

And, thanks to Ariel Flesler, his scrollTo plugin is simply amazing!

Advertisement

1. HTML

Depend on which direction you want to scroll your website, each of them have slightly different layout. For the horizontal and vertical, they use the same layout, whereas for diagonal, you have to add extra div to create the "diagonal". It sounds like a dirty trick, but hey, it works! :)

I put <a name="name"></a>, just in case some of the browsers out there don't support javascript.

Structure for jQuery share it toolbox

Scroll Horizontally & Vertically

<div id="wrapper">
	<div id="mask">

		<div id="item1" class="item">
			<a name="item1"></a>
			<div class="content">item1 
				<a href="#item1" class="panel">1</a> | 
				<a href="#item2" class="panel">2</a> | 
				<a href="#item3" class="panel">3</a> | 
				<a href="#item4" class="panel">4</a> | 
				<a href="#item5" class="panel">5</a>
			</div>
		</div>
		
		<div id="item2" class="item">
			<a name="item2"></a>
			<div class="content">item2 <a href="#item1" class="panel">back</a></div>
		</div>
		
		<div id="item3" class="item">
			<a name="item3"></a>
			<div class="content">item3 <a href="#item1" class="panel">back</a></div>
		</div>

		<div id="item4" class="item">
			<a name="item4"></a>
			<div class="content">item4 <a href="#item1" class="panel">back</a></div>
		</div>
		
		<div id="item5" class="item">
			<a name="item5"></a>
			<div class="content">item5 <a href="#item1" class="panel">back</a></div>
		</div>

	</div>
</div>

<div id="wrapper">
	<div id="mask">

		<!-- first row -->

		<div id="item1" class="item">
			<a name="item1"></a>
			<div class="content">item1 
				<a href="#item1" class="panel">1</a> | 
				<a href="#item2" class="panel">2</a> | 
				<a href="#item3" class="panel">3</a>
			</div>
		</div>
		
		<div class="item"></div>
		<div class="item"></div>
		<div class="clear"></div>

		<!-- second row -->		
		
		<div class="item"></div>

		<div id="item2" class="item">
			<a name="item2"></a>
			<div class="content">item2 <a href="#item1" class="panel">back</a></div>
		</div>

		<div class="item"></div>
		<div class="clear"></div>
		
		<!-- third row -->

		<div class="item"></div>
		<div class="item"></div>

		<div id="item3" class="item">
			<a name="item3"></a>
			<div class="content">item3 <a href="#item1" class="panel">back</a></div>
		</div>
		
		<div class="clear"></div>

	</div>
</div>
<div id="wrapper">
	<div id="mask">

		<div id="item1" class="item">
			<a name="item1"></a>
			<div class="content">item1 
				<a href="#item1" class="panel">1</a> | 
				<a href="#item2" class="panel">2</a> | 
				<a href="#item3" class="panel">3</a> | 
				<a href="#item4" class="panel">4</a> | 
				<a href="#item5" class="panel">5</a>
			</div>
		</div>
		
		<div id="item2" class="item">
			<a name="item2"></a>
			<div class="content">item2 <a href="#item1" class="panel">back</a></div>
		</div>
		
		<div id="item3" class="item">
			<a name="item3"></a>
			<div class="content">item3 <a href="#item1" class="panel">back</a></div>
		</div>

		<div id="item4" class="item">
			<a name="item4"></a>
			<div class="content">item4 <a href="#item1" class="panel">back</a></div>
		</div>
		
		<div id="item5" class="item">
			<a name="item5"></a>
			<div class="content">item5 <a href="#item1" class="panel">back</a></div>
		</div>

	</div>
</div>

<div id="wrapper">
	<div id="mask">

		<!-- first row -->

		<div id="item1" class="item">
			<a name="item1"></a>
			<div class="content">item1 
				<a href="#item1" class="panel">1</a> | 
				<a href="#item2" class="panel">2</a> | 
				<a href="#item3" class="panel">3</a>
			</div>
		</div>
		
		<div class="item"></div>
		<div class="item"></div>
		<div class="clear"></div>

		<!-- second row -->		
		
		<div class="item"></div>

		<div id="item2" class="item">
			<a name="item2"></a>
			<div class="content">item2 <a href="#item1" class="panel">back</a></div>
		</div>

		<div class="item"></div>
		<div class="clear"></div>
		
		<!-- third row -->

		<div class="item"></div>
		<div class="item"></div>

		<div id="item3" class="item">
			<a name="item3"></a>
			<div class="content">item3 <a href="#item1" class="panel">back</a></div>
		</div>
		
		<div class="clear"></div>

	</div>
</div>

Scroll Diagonally

<div id="wrapper">
	<div id="mask">

		<div id="item1" class="item">
			<a name="item1"></a>
			<div class="content">item1 
				<a href="#item1" class="panel">1</a> | 
				<a href="#item2" class="panel">2</a> | 
				<a href="#item3" class="panel">3</a> | 
				<a href="#item4" class="panel">4</a> | 
				<a href="#item5" class="panel">5</a>
			</div>
		</div>
		
		<div id="item2" class="item">
			<a name="item2"></a>
			<div class="content">item2 <a href="#item1" class="panel">back</a></div>
		</div>
		
		<div id="item3" class="item">
			<a name="item3"></a>
			<div class="content">item3 <a href="#item1" class="panel">back</a></div>
		</div>

		<div id="item4" class="item">
			<a name="item4"></a>
			<div class="content">item4 <a href="#item1" class="panel">back</a></div>
		</div>
		
		<div id="item5" class="item">
			<a name="item5"></a>
			<div class="content">item5 <a href="#item1" class="panel">back</a></div>
		</div>

	</div>
</div>

<div id="wrapper">
	<div id="mask">

		<!-- first row -->

		<div id="item1" class="item">
			<a name="item1"></a>
			<div class="content">item1 
				<a href="#item1" class="panel">1</a> | 
				<a href="#item2" class="panel">2</a> | 
				<a href="#item3" class="panel">3</a>
			</div>
		</div>
		
		<div class="item"></div>
		<div class="item"></div>
		<div class="clear"></div>

		<!-- second row -->		
		
		<div class="item"></div>

		<div id="item2" class="item">
			<a name="item2"></a>
			<div class="content">item2 <a href="#item1" class="panel">back</a></div>
		</div>

		<div class="item"></div>
		<div class="clear"></div>
		
		<!-- third row -->

		<div class="item"></div>
		<div class="item"></div>

		<div id="item3" class="item">
			<a name="item3"></a>
			<div class="content">item3 <a href="#item1" class="panel">back</a></div>
		</div>
		
		<div class="clear"></div>

	</div>
</div>
<div id="wrapper">
	<div id="mask">

		<div id="item1" class="item">
			<a name="item1"></a>
			<div class="content">item1 
				<a href="#item1" class="panel">1</a> | 
				<a href="#item2" class="panel">2</a> | 
				<a href="#item3" class="panel">3</a> | 
				<a href="#item4" class="panel">4</a> | 
				<a href="#item5" class="panel">5</a>
			</div>
		</div>
		
		<div id="item2" class="item">
			<a name="item2"></a>
			<div class="content">item2 <a href="#item1" class="panel">back</a></div>
		</div>
		
		<div id="item3" class="item">
			<a name="item3"></a>
			<div class="content">item3 <a href="#item1" class="panel">back</a></div>
		</div>

		<div id="item4" class="item">
			<a name="item4"></a>
			<div class="content">item4 <a href="#item1" class="panel">back</a></div>
		</div>
		
		<div id="item5" class="item">
			<a name="item5"></a>
			<div class="content">item5 <a href="#item1" class="panel">back</a></div>
		</div>

	</div>
</div>

<div id="wrapper">
	<div id="mask">

		<!-- first row -->

		<div id="item1" class="item">
			<a name="item1"></a>
			<div class="content">item1 
				<a href="#item1" class="panel">1</a> | 
				<a href="#item2" class="panel">2</a> | 
				<a href="#item3" class="panel">3</a>
			</div>
		</div>
		
		<div class="item"></div>
		<div class="item"></div>
		<div class="clear"></div>

		<!-- second row -->		
		
		<div class="item"></div>

		<div id="item2" class="item">
			<a name="item2"></a>
			<div class="content">item2 <a href="#item1" class="panel">back</a></div>
		</div>

		<div class="item"></div>
		<div class="clear"></div>
		
		<!-- third row -->

		<div class="item"></div>
		<div class="item"></div>

		<div id="item3" class="item">
			<a name="item3"></a>
			<div class="content">item3 <a href="#item1" class="panel">back</a></div>
		</div>
		
		<div class="clear"></div>

	</div>
</div>

2. CSS

The CSS is basically almost the same with the tabbed based content slider tutorial. I have illustrated how it works in that tutorial, so you might wanna check that out as well.

Scroll Horizontally & Diagonally

 

  • #mask : height=100%, width = 100% * total of items
  • .item : height=100%, width = 100% / total of items
body {
	height:100%;
	width:100%;
	margin:0;padding:0;
}

#wrapper {
	width:100%;
	height:100%;
	position:absolute;
	top:0;left:0;
	background-color:#ccc;
	overflow:hidden;
}

	#mask {
		width:500%;
		height:100%;

		background-color:#eee;
	}

	.item {
		width:20%;
		height:100%;
		float:left;
		background-color:#ddd;
	}
	
	
	.content {
		width:400px;
		height:300px;
		top:20%;
		margin:0 auto;
		background-color:#aaa;
		position:relative;
	}
	
	.selected {
		background:#fff;
		font-weight:700;
	}

	.clear {
		clear:both;
	}

Scroll Vertically

The width and height are opposite from scrolling horizontally. Calculations are:

  • #mask : width=100%, height = 100% * total of items
  • .item : width=100%, height = 100% / total of items
	#mask {
		width:100%;
		height:500%;
		background-color:#eee;
	}

	.item {
		width:100%;
		height:20%;
		float:left;
		background-color:#ddd;
	}

3. Javascript

So, you must be thinking how complicated the javascript would be. You'll be surprise! its only a line of code to make the scrolling effect. Thanks to jquery.scrollTo Plugin. What we have to do is solve the layout using css/html, and the plugin will do the rest.

$(document).ready(function() {

	//get all link with class panel
	$('a.panel').click(function () {

                //reset and highlight the clicked link
		$('a.panel').removeClass('selected');
		$(this).addClass('selected');
		
		//grab the current item, to be used in resize function
		current = $(this);
		
                //scroll it to the destination
		$('#wrapper').scrollTo($(this).attr('href'), 800);		
		
                //cancel the link default behavior
		return false;
	});


	//resize all the items according to the new browser size
	$(window).resize(function () {
		
		//call the resizePanel function
		resizePanel();
	});
	
});

This resize function have 2 versions. One for vertical and another for both horizontal and diagonal. The only difference is the mask_height and mask_width.

Vertical
function resizePanel() {

	//get the browser width and height
	width = $(window).width();
	height = $(window).height();

	//get the mask height: height * total of items
	mask_height = height * $('.item').length;
		
	//set the dimension		
	$('#wrapper, .item').css({width: width, height: height});
	$('#mask').css({width: width, height: mask_height});

	//if the item is displayed incorrectly, set it to the corrent pos
	$('#wrapper').scrollTo($('a.selected').attr('href'), 0);
		
}
Horizontal & Diagonal
function resizePanel() {

	//get the browser width and height
	width = $(window).width();
	height = $(window).height();

	//get the mask width: width * total of items
	mask_width = width * $('.item').length;
		
	//set the dimension	
	$('#wrapper, .item').css({width: width, height: height});
	$('#mask').css({width: mask_width, height: height});
	
	//if the item is displayed incorrectly, set it to the corrent pos
	$('#wrapper').scrollTo($('a.selected').attr('href'), 0);
		
}

Conclusion

That's it, simple and easy! I hope you all will enjoy it.

Last but not least, I need your support :) If you like this article, please help me to promote it by adding this post into your bookmark. You can subscribe to my RSS for more jQuery tutorial posts! Or go to my website footer to follow me on twitter, and buy me a drink! Thanks!

Jump to Certain Item on Page Load

This is one of the most popular request. A lot of people want this content slider to display certain item on page load. It's simple, you need to use this:

$(document).ready(function () {

   $('#wrapper').scrollTo('ITEM-ID-HERE', 0); 

   ......
   ......
   ...... 
}

With that line of code, the slider will jump to the item on page load.

Update

31 Aug 2009 : Due to popular demand, and something I should have created. I have added window resize function.
Demo 1Demo 2Demo 3Download
Join the discussion

Comments will be moderated and rel="nofollow" will be added to all links. You can wrap your coding with [code][/code] to make use of built-in syntax highlighter.

792 comments
Alex Warren 14 years ago
Hi Kevin,

Great tutorial, really useful. The only thing I can't get to work is using more than 5 horizontal scrolling divs. As soon as I add a 6th it starts scrolling vertically.

Any ideas?

Thank You
Reply
Kevin Liew Admin 14 years ago
You need to follow the horizontal math calculation. From your description, I think the width of mask doesn't fit the total items you have.

#mask : height=100%, width = 100% * total of items
.item : height=100%, width = 100% / total of items
Reply
katak 14 years ago
yes! working for me.

#mask : width = 600%
.item : width = 17% (calculation to be exact is 16.66666666666667)

tq!
Reply
katak 14 years ago
Decay 14 years ago
Thanks so much for your tutorial! Clear and efficient, I was researching a good way to make my website slide since a few weeks!

Then I have one question, it's about the menu, and links.

My menu is not in my #wrapper, he is in a div above, #menu, and it works but without any sliding effects.... whereas when links are in the #wrapper #mask .item .content, it works very well.

Is there a way to make it works in the javascript? I need to keep them in two different parents...
Thanks you!
Reply
Kevin Liew Admin 14 years ago
It doesn't matter where you put your menu as long as the link has a class called "panel" and linked to a correct panel.
Reply
Fortune 14 years ago
Hi Kevin! Thank you for the tutorial! I have a question about the menu too. How do I include it in the resize function? When I resize the window, everything except the #menu fits in the browser.
Reply
ras 14 years ago
how about different height in every div??
Reply
Kevin Liew Admin 14 years ago
I think it will still working, but doesn't look good.
Reply
Andy G 14 years ago
Hello Kevin,

First of all i want to thank you for sharing the jquery horizontal code. It is going well for my limited knowledge of code.

I do have some reservations. Recently i put the your code through w3 validator and. The validator didnt like this as it says NAME is now deprecated.

eg as 4.01 strict

<div id="item1" class="item"><a name="item1" id="item1"></a>



An "id" is a unique identifier. Each time this attribute is used in a document it must have a different value. If you are using this attribute as a hook for style sheets it may be more appropriate to use classes (which group elements) than id (which are used to identify exactly one element).



when i changed it to HTML 5 it says that NAME is now deprecated

eg HTML 5

Line 109, Column 56: The name attribute is obsolete. Consider putting an id attribute on the nearest container instead.

<div id="item2" class="item"><a name="item2" id="item2"></a>


Would you considering reworking the code to appear more user friendly for all browsers?

Thank you for your time Kevin.

Andy G

Reply
Kevin Liew Admin 14 years ago
Yes, you shouldn't have multiple ID that are same. I don't have any plan for the rework for now, the reason I use name is for those who have no javascript support, they will still able to show the page.
Reply
Andy G 14 years ago
Kevin,

Your prompt & concise response is greatly appreciated. Thank you.

Reply
Andy G 14 years ago
This comment is not for publishing please.

I was going to pose my next question to the stackoverflow people Kevin. But i was not sure if your work also included helping people with code.

ok here is a link to your modified code for my idea for my site.

http://www.sendspace.com/file/jesvuc

I am using WAMP on Firefox to be able to view the test site. Like i said i have very limited code knowledge & surprised that i have got it this far. I have only sent you the bare bones. My problem is that i want to be able to keep the top banner and nav bar fixed and also the left, right and bottom banners fixed. I am able to keep the left banner & bottom banner fixed using css but the rest doesnt take nor does it take to IE very well also.

I also need to add my the logo in the header section & naively added a few <br> to fix the layout problems
i was having. I know this is such a crude attempt of interpreting your code Kevin but it was more helpful than trying to use the jquery one over at tutsplus.

I knwo you are busy & therefore hope to hear from you at your convenience Kevin.

Thank you again for your time & patience.

Andrew



Reply
galerikredit 14 years ago
I think its a great tutorials ....
Reply
Jannie 14 years ago
Hi Kevin

This is working perfectly for me, I'm just having an issue when I try to add a slideshow gallery by the name of Galleria, the moment I add the script tags pointing to the .js files required for the slideshow - both the slide effect of the page stops working and the slideshow doesn't even display at all (not that it ever did).

This is what I've done so far: http://www.quicktoast.co.za/revive/#item1

Any type of feedback from you if I'm doing something wrong or if this would even work would be GREATLY appreciated.

Best
Jannie
Reply
Kevin Liew Admin 14 years ago
Maybe you can try a newer version of jQuery. This tutorial has been here for a while, and it's using a pretty old jQuery.
Reply
deepak 14 years ago
Hi Kevin,

Your tutorial is great. Thank you very much for this.
I need one help from you.

I want "item2" or "item3" div will display default.
I don't want "item1" div is display first.

Thanx for your help kevin
Reply
Kevin Liew Admin 14 years ago
You can check out this section in the tutorial:
Jump to Certain Item on Page Load
Reply
Deepak 14 years ago
You are superb kevin,
Thanx for your help.
Reply
Nate 14 years ago
First off I have to say thanks for the awesome tutorial! It was a HUGE help achieved exactly what I was looking for... BUT There is ONE issue that I'm surprised hasn't come up yet that I'm running into that I need help addressing...

I need to a MINWIDTH so that you can scroll to see the content if the browser is resized too small...
But when you scroll horizontally to see the content in the current div, the Other divs are visible when scrolled =/

I think the easiest way to Fix this issue would be to set the Z-INDEX of the currently selected div to a higher value than the others, But how can this functionality be added to the onClick event?

This is the ONLY thing holding me back at this point...
Reply
sunny 14 years ago
This is a great tutorial!! I noticed that there are two sepreate source folders - (one which is for macosx) if i create a website will i need to edit both the scroll folder and macosx folder seperatly, and then upload them to a server?
Reply
s_bas 14 years ago
Hi, thanks for the great tutorial. I was wondering if there is a way to create a link which only scrolls the page down a certain height when using the vertical scroll. E.G. On the last page i would like to create a "footer", when a user preses a link the page will only scroll about 200px to show the footer. thanks!!!!
Reply
Ian 14 years ago
Hey there! Thank you very much for your tutorial, it is really nice!
I think I've just found a "bug" and I don't know how to solve it... Here it is :
On every page you did (to slide horizontally, vertically or diagonnaly), when you click on an item (for example item2) and then you refresh the page (strike F5) and finally you try to resize the page it says that "targ" is undefined in the scrollTo script and the page isn't resizing very well... This bug appears only in FF and IE (I've tried with FF, Opera, Chrome and IE)
Do you know how to solve that?

Thank you again for your article, it was exactly what I was looking for :)
Reply
Jc@rcc 14 years ago
Look great. two questions before I try and make it work for my site -

1. Can I scroll just one div within a page or does it have to be the whole page (as in all the divs on the page) that scrolls?

2. do the pages all have to be the same height or can they vary in height?
Reply
Kevin Liew Admin 14 years ago
Yes and yes.
Reply
jc@rcc 14 years ago
Is that a 'yes' to the div that scrolls having to be the same height?
Reply
Kevin Liew Admin 14 years ago
The script auto resizes each item and makes all of them having the same width and height.

Based on your request, It's possible, but you will need to mod it. You can build a custom one easily by referring to jquery.scrollTo.js plugin website mentioned above.
Reply
jc@rcc 14 years ago
I have almost finished revamping my website and am almost ready to relaunch it. When I'm done I'll take some time to look at scrolling it in the future.
Reply
Lisa 14 years ago
Great tutorial but the issue I'm having is the other links (a href) on my page not associated with this no longer work.
Reply