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

Written by Kevin Liew on 14 Jul 2009
685,000 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
Astrid / Germany 12 years ago
Hi, this is great! Thanks a lot!!! :) I just have one question: how can I make the change from page to page more slowly? Thank you for any advice in advance!! :) astrid
Reply
Dru 12 years ago
Hey, great script! I'm wondering if there is a way to add a class ('current') to the currently shown .item. It could be helpful (in horizontal scrolling pages) in resizing the vertical height of each .item if they have different lengths of content.

Thanks for all your hard work.
Reply
fazzo 12 years ago
hi,
1000 thx for that. its realy awesome!
here is the result: fazzo.ch
Reply
darren case 12 years ago
is there a way to make it when it loads it starts out at a different page then page one? Like page 2, then you can scroll left to page 1 and then right for pages 2 and 3. thanks for your help!
Reply
Kevin Liew Admin 12 years ago
Please read - Jump to Certain Item on Page Load in this tutorial
Reply
Shreyas 12 years ago
Awesome tutorial Kevin. I started to develop a website using horizontal scrolling demo code. I noticed when I have slides next to each other everything works fine but when I re-size the browser window slides move away from each other and I can see only one. Any idea How I can solve this?
Reply
Kevin Liew Admin 12 years ago
Please refer to the window resize section.
Reply
Daniel Ramirez-Escudero 12 years ago
Thank you very much!!! Very usefull!!!!!!!!!!!!

I only have one strange bug and maybe its my fault. I tried to insert the code regarding "jump to page onload" and it does work. In this case I wanted to load this in item3, it loads item3 but after reloading the page a lot of times you can observe that 1 out of 5 times it loads in item 1. I really don;t understand why... Maybe its my actual code structure:


[IN THIS CASE #home REPRESENTS item3]

$(document).ready(function() {

$('#wrapper').scrollTo('#home', 0);

$('a.panel').click(function () {

$('a.panel').removeClass('selected');
$(this).addClass('selected');

current = $(this);

$('#wrapper').scrollTo($(this).attr('href'), 800); /*Number is the speed, the higher the slower and viceversa*/

return false;
});

$(window).resize(function () {
resizePanel();
});



});

function resizePanel() {

width = $(window).width();
height = $(window).height();

mask_width = width * $('.item').length;

$('#debug').html(width + ' ' + height + ' ' + mask_width);

$('#wrapper, .item').css({width: width, height: height});
$('#mask').css({width: mask_width, height: height});
$('#wrapper').scrollTo($('a.selected').attr('href'), 0);

}

If important this is my link structure from my html if important:

[ONLOAD IS THE JAVASCRIPT FILE WHERE ALL MY JAVASCRIPT CODE IS PLACED]

<script type="text/javascript" src="_js/jquery-1.3.1.min.js"></script>
<script type="text/javascript" src="_js/jquery.scrollTo.js"></script>
<script type="text/javascript" src="_js/scroll_resize.js"></script>
<script type="text/javascript" src="_js/onload.js"></script>
<link type="text/css" rel="stylesheet" href="_css/style.css" media="screen"/>

Thanks for he help!!!
Reply
Daniel Ramirez-Escudero 12 years ago
This is a really cool tool!!!! It works fine!!!!

There is only one detail which is bugging me... Once I apply the "jump to certain item on load" it does work but the resize function only works when you refresh the window after resizing it manually. Is there a bug or something we should take into consideration?

Thanxs!
Reply
Kevin Liew Admin 12 years ago
Did you add this to your code?
//resize all the items according to the new browser size
$(window).resize(function () {

//call the resizePanel function
resizePanel();
});
Reply
Jon 11 years ago
I am having the same problem. I can't initially get the resize function to apply to the first 'jumped' item. The resize function seems to only start taking place after any manual slide click or page refresh. Do we have to add the resize function twice, in 2 different parts of the script? Is there a special order? Perhaps new code is needed to fix the bug? Thanks in advance.
Reply
Bernard 11 years ago
Same problem here... jumping to a item on load is good, but any window size changes on first load crash the layout :(
Reply
Bernard 11 years ago
Just find the problem and solve it with a condition statement for $('a.selected').attr('href') (undefined until first clic)... like this:
function resizePanel() {

width = $(window).width();
height = $(window).height();

mask_width = width * $('.item').length;

$('#debug').html(width + ' ' + height + ' ' + mask_width);

$('#wrapper, .item').css({width: width, height: height});
$('#mask').css({width: mask_width, height: height});
if (typeof($('a.selected').attr('href'))=='undefined')
{
$('#wrapper').scrollTo('#item1', 0);
}
else
{
$('#wrapper').scrollTo($('a.selected').attr('href'), 0);
}

}

... and replace '#item1' with the one you jump to!

May be is there a better solution... I'm NOT a web scripter :)
Reply
George 12 years ago
Hello great article Kevin. I just installed it on my site, but can I ask you for a favor. I have some problems and I don't know how to fix them.
1. First can you tell me is there a way for the div's to move from top to bottom, not from bottom to top?

2. Is there a way to go to specific div without moving the other div's - for example when you are on page 1 if you press 5 you are gonna see all other div's sliding is there a way not to slide every div, just to go to page 5 with one slide from page 1 to page 5.

3. I have problem with the #wrapper and my footer. I can't make #wrapper to resize. I have it with width: 1360px and my inner div is with width: 1200px, but my other inner div is 600px width and the footer is still in the bottom of the page and there is big hole between my inner div and the footer. How can I make #wrapper to resize with different size when you press different number - example when you push 5 the width will be 500px, when you press 3 the width is gonna be 700px; P.S. I have problem with the resize function here.

4. And last, but not least I have styled the number pages. So for example the 4 here in my page is "contacts". I have problem to style them the exact way that I want, because I have 3 parts that making my navigation button. two divs and one link tag. My div's have position: absolute and they are located top left and bottom left. And right to them is my link tag. We have here style that is called selected,. but there I can put only the link tag, so my other two parts of the button will not show up. So can you tell my how can I make my button to show up correctly.

Thank you very much in advance Kevin. And again great article here :)
Reply
Kevin Liew Admin 12 years ago
Hi George,

1. What do you mean? It should be moving from top first because that's how the page load.

2. Sorry, unfortunately it has to go through all pages. Unless you set the main screen to center, and scroll left and right for other pages.

3. I can't visualize it, if you have an online version, that could be helpful.
Reply
Georgi 12 years ago
I will upload the site soon, but now I can only upload screenshots from it. I found my answers for my questions with your help and a friend too :). But now I have a different question and I hope you can help me again. Can I put to the pages different URL. For example when I press 3 - the URL to be www.mysite.com/photos. Or when I'm pressing 2 the URL to be www.mysite.com/contacts ?
Reply
Yogue 12 years ago
Hi, How to add more pages in horizontally version?
thx
Reply
Kevin Liew Admin 12 years ago
You can add more pages by duplicating the item, and rename it accordingly to 6, 7, 8....
Then, please refer to CSS section, and calculate the correct width.
Reply
Rajesh 11 years ago
It works for more than 5 items.
Reply
giampietro 12 years ago
hello great plugin,
you can use it with jquery easing?
Reply
Erick Rivadeneira 12 years ago
Hi, thanks for the puglin is very useful. Let me now if I can add mousewheel feature on this plugin.. Thanks!!!!
Reply
Sayyeda 12 years ago
Hi. Im doing a uni project and I wanted to know , how can i combine the horizontal and vertical scrolling effect in one project from the above tutorial? Combining horizontal vertical and diagonal would be even better, but i m not sure how to go about it:/?
Reply
Neetu Saroj 12 years ago
Its awesome plugin and helped me alot.......
Thanks alot.... :-)
Reply