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

Written by Kevin Liew on 14 Jul 2009
709,837 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
hanna 14 years ago
Hello I am just wondering if it is possible to see more than one div .
when you looking at demo file, we can see only one div on your screen. is it possible to change the distance between divs
Reply
Kevin Liew Admin 14 years ago
hmm, I see what you're trying to do. See the calculation below, #mask will be the 5* of your screen size, lets keep it that way, for item, instead of 100% divided by total of items, try 90% or 80% instead. I have not try it but that will make 2 items appeared with one slightly shown.

#mask : width=100%, height = 100% * total of items
.item : width=100%, height = 100% / total of items
Reply
hanna 14 years ago
Yes i tried that. and I got 3 items div in one screen. but the selected item is floating left rather than being in the centre.
Do you think i could have the selected item in the middle having the other item in both sides.
Reply
sruchan 14 years ago
Hello Kevin,

First, i want to appreciate you for this tutorial. I followed all steps, but i cant see scrolling effects in my website.
http://narinder.co.uk/?page_id=455

Can you pls help?



Thanks

Reply
Kevin Liew Admin 14 years ago
I think you haven't included scrollto plugin.
Reply
Kevin 14 years ago
Hello thank you for this tutorial. I have used this in my thesis. Puy-anan.com
It was fantastic. I'm planning to use it again in my portfolio. My question:
Can I integrate this with scrollTo's queue function? If yes, can you show me how? A reply would be greatly aprreciated! Thanks!
Reply
Kevin Liew Admin 14 years ago
Hi, you should able to do it, just refer it from scrollto plugin doc.
http://flesler.blogspot.com/2007/10/jqueryscrollto.html

You can change the first part of the jvascript, line 14:
$('#wrapper').scrollTo($(this).attr('href'), 800);

to

$('#wrapper').scrollTo($(this).attr('href'), 800, {queue:true});
Reply
Eno 14 years ago
Is there a way to add keyboard support? To use the directional keys and move up and down, right and left?

Thank you so much!
Reply
Kevin Liew Admin 14 years ago
Robbie 14 years ago
Hello, I'm having a weird problem with the implementation of this plugin. I'm working with a horizontal slider layout, yet if the window is resized the scroller changes to vertical scrolling. Also, IE renders it as a vertical scroller, period. Do you know what could be causing this odd behaviour? Thanks very much in advance!
Reply
Kevin Liew Admin 14 years ago
I think you forgot to put the resizePanel function. It in the last section of this tutorial.
Reply
Roc 14 years ago
How I can get it in normal content page? It so difficult...
Reply
Marty 14 years ago
Hey - Great code. I've really enjoyed designing with it.

I'm running into trouble after adding the "Jump to a page" line into the script. The code seems to break the window re-size protections and my pages bleed into one another on re-size. If I remove the "jump to a page" code, the window re-size behaves properly.

I am placing the problem code in the correct spot, ahead of the rest of the code. I've tried to move it elsewhere but the results are the same. Any suggestions would be greatly appreciated!

-M
Reply
Kevin Liew Admin 14 years ago
The jump to section script should put on top of everything else, inside the $document.ready.
Reply
Seema Morajkar 14 years ago
hi,
I am using vertical scrolling for my website, it's very useful. But can you please tell me how to use for different different height of every page, If you help me out for this matter really appreciate.
Thanks a lot.
Reply
Kevin Liew Admin 14 years ago
This is the calculation:

#mask : width=100%, height = 100% * total of items
.item : width=100%, height = 100% / total of items

basically each item will be the same size as your browser width and height.
Reply
gmoss 14 years ago
Thanks for this scrolling! Works great! However, I have noticed an "issue" when you have a full screen content. You can see this issue also with one of the commenters' website (http://seegermattijs.be/!/) and I have the same issue as well.

When you drag the browser window's side (top/left/bottom/right) just 1 pixel INWARDS (try to move it very slowly) you will see a bar with a background color appear at the right and bottom and staying on the screen. Drag the side of the window slowly inward just 1 pixels, the bars appear. Continue dragging inwards again 1 pixels, now they disappear. Drag more again, they will appear, etc, etc. It seems that those bars and the right and bottom would be the scroll bars (which are not visible). When the bars appear, and you let go of the mouse, the bars will stay on the screen (which is annoying).

Oddly, it's ok in IE 7/8 and Chrome 16, but very noticable in Firefox 10 and Opera 11.61. I wonder is this is a browser app implementation issue, of could this be fixed within the script/css? Thank you!

Reply
bill beck 14 years ago
question, how can i change the direction of the horizontal slider,
so that it begins to slide in from the left ? thank you for your time, -bill
Reply
Kevin Liew Admin 14 years ago
Well, there is a trick for it, on page load, load the last slide on the right. Then you get to slide from the left. :)
Reply
bill beck 14 years ago
kevin, can you please post some code to show how to acomplish left to right slide in, thank you, -bill
Reply
gmoss 14 years ago
Ok, I solved it. For full screen content (eg, content that covers the whole viewing area of the window) you need to add {overflow: hidden;} to the body style.
Reply
gmoss 14 years ago
I am getting a script error (displayed in lower left side of the browser) in IE 7/8 when I resize the window. It's coming from the scrollTo.js script. However, no other browsers report errors (neither mac or pc).
Reply
Kevin Liew Admin 14 years ago
What's the error message?
Reply
gmoss 14 years ago
you can reproduce this in your demo sample if you run it in IE7/8. just keep an eye in the lower left corner of the browser where the script errors will be shown.
Reply
Simon 14 years ago
Great work, I have used scrolling before but this is incredibly simple to implement.

One Question:

Id like to add 2 navigation buttons as fixed divs. One will be fixed to the left and one fixed to the right of the design. I would like these to be scroll left and scroll right functions ? so clicking the right button would scroll the content to item 2, then another click would scroll to item 3 etc....

Would it be possible to implement these buttons with the current javascript code ?

Any advice would be greatly received!

thanks again
Reply
Kevin Liew Admin 14 years ago
Hi Simon, it actually possible. For the left and right buttons, you need to style them with position fixed, so it will always display on screen.

For the scrolling, I have not implemented that feature but I can give you some idea. For the main nav, the selected item will get a class called "selected".

So, you need to write a javascript to find the two id that next to the current selected a tag. Eg, Assume you have 5 items, if item number 3 was selected, so the prev and next would be 2 and 4. So, when you click on the previous button, it will get 2, the prev item of current selected A tag.

I hope it makes sense.. :p
Reply
dd666 14 years ago
I'm trying to find the answer to this as well. I don't know how to write Javascript, but I would like a fixed navigation with next and back arrows. Could you possibly expand on this?
Reply
Simon 14 years ago
Thanks for your speedy reply.

Well I have the navigation elements fixed in place. My current Jquery skill level is being able to adapt things to meet my needs as opposed to writing from scratch. I will give it a try as Im always looking to learn. If I get it right I'll post the method.

And if i struggle expect to hear back from me.. :P

thanks again
Reply
Kevin Liew Admin 14 years ago
That's the spirit :) I started this blog to learn jQuery, and the fastest way to learn is to share. People will ask questions, critiques, bugs etc... that speed up the learning process. If I ever forget to reply, (I always do that!) reach me via contact form.

Good luck.
Reply