Horizontal content scroll with jQuery

March 13, 2012 jQuery 22 comments

In this tutorial we will create a simple smooth horizontal content scrolling effect with just a few lines of jQuery without using any plugin. We will use HTML and CSS for the layout and jQuery for the scrolling.

The Markup:

Content Markup:

Following is markup for the content. We have three content boxes wrapped in the contentbox-wrapper which is wrapped inside the #content div. Each of the content box has a unique id.

<div id="content">

	<div class="contentbox-wrapper">

		<div id="about" class="contentbox">
			<h3>About</h3>
			<p>Lorem ipsum dolor ...</p>
		</div>

		<div id="work" class="contentbox">
			<h3>Work</h3>
			<p>Lorem ipsum dolor ...</p>
		</div>

		<div id="contact" class="contentbox">
			<h3>Contact</h3>
			<p>Lorem ipsum dolor ... </p>
		</div>
	</div>

</div>
Menu Markup:

Now we need to create a navigation menu, so that by clicking on the links we can scroll to the relevant content id.

<div id="nav">
	<ul>
		<li ><a class="active" href="#" onClick="goto('#about', this); return false">About</a></li>
		<li><a href="#" onClick="goto('#work', this); return false">Work</a></li>
		<li><a href="#" onClick="goto('#contact', this); return false">Contact</a></li>
	</ul>
</div>

As you can see, we call a javascript function goto on clicking the link by passing two parameters. First is the id of of the target content div and second is reference of the clicked link. Passing the reference of the link helps us to add some css class to the clicked link with help of jQuery. The return false event cancels the default behavior of browser, so in this case it tells the browser not to follow the hyperlink after the javascript has run, therefore the browser will not attempt to go to the # and add # in the url.

The CSS:

Following is the CSS for the content divs:

#content{
	overflow:hidden;
	-moz-box-shadow: 0 0 2px 2px #ccc;
	-webkit-box-shadow: 0 0 2px 2px #ccc;
	box-shadow: 0 0 2px 2px #ccc;
}

.contentbox-wrapper{
	position:relative;
	left:0;
	width:3000px;
	height:100%;
}

.contentbox{
	width:580px;
	height:100%;
	float:left;
	padding:10px;
	background:#fff;
}

We have set the .contentbox-wrapper width to 3000px so that all content boxes can fit in it. The width of .contentbox is set as 580px. #content has overflow:hidden so that the overflowing content is not visitble.

The CSS for the menu is very simple:

#nav {
	margin-top:10px;
	background: url("navbg.png") repeat-x center bottom;
	border-bottom: 1px solid #DDDDDD;
	padding: 5px 10px;
}

#nav ul li{
	display:inline;
	margin-right:10px;
}

#nav a.active {
	font-weight:bold;
}

The link with the class active will be bold.

The JavaScript:

Here is the function for the horizontal scrolling and adding the CSS class to the link:

function goto(id, t){
	//animate to the div id.
	$(".contentbox-wrapper").animate({"left": -($(id).position().left)}, 600);

	// remove "active" class from all links inside #nav
    $('#nav a').removeClass('active');

	// add active class to the current link
    $(t).addClass('active');
}

The above function animates to the div whose id is passed as a paramenter. It removes the css class from the previous link and adds to the link which is clicked.

  • Mali

    Nice one!

  • Gheuntak

    is it possible to use left – right arrows instead of navigation to scroll the contents?

    • joaoferreira

      Hi, is it possible to use left – right arrows?

      • http://gazpo.com Sami

        yes, it is possible, you can use css to edit the link text and show arrow image etc.

  • Pingback: 45 Highly Useful jQuery And JavaScript Tutorials

  • n9iels

    ingenious, thank you very much!!!

  • asd

    animate to left corner div how i can make this animate to centred div no left corner div?

  • http://www.facebook.com/vanusa.oliveira.90281 Vanusa Oliveira

    Can this scroll start automaticaly? thanks!

  • nicole

    You are officially my hero! Brilliant :))

    • http://gazpo.com Sami

      Thanks nicole :)

  • Dashrath

    Add some revenue generator advertisement on website ….I like to click on adds every time i download something from any website so that website can get benefit of what it gives to us ….

  • Pete

    Doesn’t work on IE7 :(

    • http://twitter.com/Kowagaru Danilo Pires Kaltner

      Add position:relative to the element and that worked.

  • Jime Ximbalo

    HI. Thank you for the excellent and simple demo. I am integrating it into responsive layout project I have right now with 3 side-by-side content panels. As the window (device) screen gets smaller, three visible panels reduces, to and finally to one. Your slider will help me resolve how to easily handle show/hide of non-visible content. One question though… what would you see as the best way to add touch-slide functionality to the content panels ( ie: not just button click-event driven)? For Mobile Phone and Tablet usage. Please advise! Thank you!

  • richie

    not even working on this site

  • Martin

    Love the slider, but have come across an issue where there are more tha 8 images. For some reason, the nav for images 8 and above send the slider back to image 1.

    Takea al ook at this page – http://jan.gardenadventure.co.uk/Climbing-Frames/Swing-Module-1681.php When you click on the green button for “Lodge” you start again ?

    Any help appreciated

    • martin

      Answered my own question – the div.contentbox-wrapper was restricting the width. By increasing this figure allowed the greater number of images to function correctly

  • Bob

    How do you hidden the content and make other boxes show up. By using only the nav bar. Like your example I just can’t seem to get it working

  • Kimmobi

    Your demo site works great in my IE7, 8 & 9, Firefox and Chrome but when I download the files and run it in my IIS sever, Firefox and Chrome works fine but IE have the problem in hiding the 2nd and 3rd page (seems that the “overflow:hidden” doesn’t work), am I missed something?

  • CBX

    Hi, this is a great tutorial. I used your tips and built a website with this jquery and your code inspired me. There is a problem with contentbox-wrapper/contentbox – those are not responsive. Everything else could be resized, but when I apply any percentage width, max-width or anything like this, it does not work. Either the text inside will not be resized – displayed as complete or all of my id divs (i have 4) will be displeyed at once. What do I do wrong?

    Thank you very much for your tips and tutorials.

  • Vinay

    can this event be triggered by mouse scroll

  • http://www.facebook.com/adriesilva Adriana Adrie Silva

    Hi.. this tool is great. Can I ask how to change the background color of the content box? In your example there is a white background with the content.

    thank you.