Subscribe Via RSS

0 Subscribers

Subscribe Via RSS
Subscribe Via Twitter

Followers

Subscribe Via Twitter
Learn How To Create A Scrollable Website

May 27th, 2010 in JQuery Tutorials by Richard Carpenter

Learn How To Create A Scrollable Website

1 Star2 Stars3 Stars4 Stars5 Stars11 Votes, Rating: 5.00
Loading ... Loading ...

Good evening all, today were going to be diving into some jquery. What we’ll be creating is a small low key website which scrolls down to each section when a navigation item is clicked.

The Live Demo

Click the image to see the full working demo.

jQuery Scroll Live Demo

Resources Needed In This Tutorial

Lets get started…

Slicing The Images From Pre-Made PSD File

Before we start create a new folder on your desktop called template, inside the template folder create a further 3 folders. Label the 3 folders “Images”, “Js” and “Stylesheets”.

Next download the pre-made PSD file then open it up in photoshop. The PSD should look like this.

jQuery Scroll

We need to slice 3 items from the PSD file which will make up our demo, the rest can be done in CSS. The items we need to slice are “The Background”, “The Title” and “The Graphic Top”.

Using the “Rectangular Marquee Tool” make a selection around each item then save them inside the images folder. Be sure to put each image on a transparent background and save the files as “bg.png”, “title.png” and “top.png”.

jQuery Scroll

Creating The HTML Markup

Inside your template folder create a blank HTML file then inside your stylesheets folder create a blank CSS file. Once you’ve created the files open up both files inside your favorite code editor.

Inside your HTML file start the mark-up, which looks like this.

<div id="top">

<div id="title">
</div><!--title ends-->

<div id="navigation">
</div><!--navigations ends-->

<div id="homepage">
</div><!--homepage ends-->

<div id="about">
</div><!--about ends-->

<div class="top">
</div><!--top ends-->

<div id="gallery">
</div><!--gallery ends-->

<div class="top">
</div><!--top ends-->

<div id="portfolio"">
</div><!--portfolio ends-->

<div class="top">
</div><!--top ends-->

<div id="contact"">
</div><!--contact ends-->

<div class="top">
</div><!--top ends-->

</div><!--container ends-->

Lets look at the mark-up abit more closer.

  • DIV Top – The div top is our container div, this div will be the div which holds all the elements to our layout. You’ll be pleased to know that there is also a reason why its labeled as “top”.

    When we click “Back To Top” which will be a link underneath each section the animation will scroll to the top of this div called “top”.

  • DIV Title – The div title will simply be a div which will house our title image.
  • DIV Navigation – This div will be the div in which our navigation will sit.
  • DIV’s Homepage, About, Gallery, Portfolio and Contact – These div’s are the sections in which the animation will scroll down to when the desired item is click in the navigation.
  • Class Top – The classes top in between each section will house our fancy separators and contain our “Back To Top” link.

jQuery Scroll

Adding Our Elements

Inside the “Title” div insert your title.png image.

<div id="title">
<img src="images/title.png" alt="jQuery Animated Scroll" />
</div><!--title ends-->

Inside the “Navigation” div create a simple unordered list for your navigation items. Each link should contain the class “Scroll”, this class is needed for the scroll animation to work. Also take note of the hyperlinks in each navigation item, the hyperlinks are actually the names of the DIV’s in which are used in the HTML code.

<div id="navigation">
<ul>
<li><a href="#about" class="scroll">About Me</a></li>
<li><a href="#gallery" class="scroll">Gallery</a></li>
<li><a href="#portfolio" class="scroll">Portfolio</a></li>
<li><a href="#contact" class="scroll">Contact Me</a></li>
</ul>
</div><!--navigations ends-->

Inside each of the sections “Homepage, About, Gallery, Portfolio and Contact” insert a H1 tag which will contain the section title. Also inside the H1 tag add a description of that section wrapped in a span class.

<div id="homepage">
<h1>Homepage <br />
<span class="description">This is the Homepage</span></h1>
</div><!--homepage ends-->

<div id="about">
<h1>About <br />
<span class="description">This is the About Page</span></h1>
</div><!--about ends-->

<div class="top">
</div><!--top ends-->

<div id="gallery">
<h1>Gallery <br />
<span class="description">This is the Gallery Page</span></h1>
</div><!--gallery ends-->

<div class="top">
</div><!--top ends-->

<div id="portfolio">
<h1>Portfolio <br />
<span class="description">This is the Portfolio Page</span></h1>
</div><!--portfolio ends-->

<div class="top">
</div><!--top ends-->

<div id="contact">
<h1>Contact <br />
<span class="description">This is the Contact Me Page</span></h1>
</div><!--contact ends-->

<div class="top">
</div><!--top ends-->

Finally inside the classes “Top” add a simple P tag containing the words “Back To Top”, add a link to the words which should link to our top div. Don’t forget to add the class “Scroll” to the link, without this jquery wont work.

<div id="homepage">
<h1>Homepage <br />
<span class="description">This is the Homepage</span></h1>
</div><!--homepage ends-->

<div id="about">
<h1>About <br />
<span class="description">This is the About Page</span></h1>
</div><!--about ends-->

<div class="top">
<p><a href="#top" class="scroll">Back To Top</a></p>
</div><!--top ends-->

<div id="gallery">
<h1>Gallery <br />
<span class="description">This is the Gallery Page</span></h1>
</div><!--gallery ends-->

<div class="top">
<p><a href="#top" class="scroll">Back To Top</a></p>
</div><!--top ends-->

<div id="portfolio">
<h1>Portfolio <br />
<span class="description">This is the Portfolio Page</span></h1>
</div><!--portfolio ends-->

<div class="top">
<p><a href="#top" class="scroll">Back To Top</a></p>
</div><!--top ends-->

<div id="contact">
<h1>Contact <br />
<span class="description">This is the Contact Me Page</span></h1>
</div><!--contact ends-->

<div class="top">
<p><a href="#top" class="scroll">Back To Top</a></p>
</div><!--top ends-->

Adding The CSS

The CSS for all our elements look like this.

body {
background-image: url(../images/bg.gif);
background-repeat: repeat-x;
background-color: #FFFFFF;
margin: 0px;
padding: 0px;
font-family: Verdana, Arial, Helvetica, sans-serif;
}

#top {
width: 950px;
margin-top: 50px;
margin-right: auto;
margin-bottom: auto;
margin-left: auto;
}

#title {
margin: auto;
width: 495px;
clear: both;
}

#navigation {
float:left;
width:100%;
overflow:hidden;
position:relative;
margin-top: 20px;
margin-bottom: 20px;
}

#navigation ul {
clear:left;
float:left;
list-style:none;
margin:0;
padding:0;
position:relative;
left:50%;
text-align:center;
}

#navigation ul li {
display:block;
float:left;
list-style:none;
margin:0;
padding:0;
position:relative;
right:50%;
}

#navigation ul li a {
display:block;
margin:0 0 0 1px;
padding:3px 10px;
color:#666666;
text-decoration:none;
line-height:1.3em;
}

#navigation ul li a:hover {
color:#000000;
}

#navigation ul li a.active, #navigation ul li a.active:hover {
color:#000000;
font-weight:bold;
}

#homepage {
float: left;
width: 950px;
height: 800px;
}

#about, #gallery, #portfolio, #contact {
float: left;
width: 950px;
height: 600px;
}

h1 {
color: #5a5a5a;
font-size: 24px;
font-weight: normal;
margin: 0px;
padding: 0px;
}

span.description {
color: #9f9f9f;
font-size: 12px;
}

.top {
background-image: url(../images/top.png);
background-repeat: no-repeat;
float: left;
height: 48px;
width: 950px;
padding-bottom: 35px;
background-position: bottom;
margin-bottom: 20px;
}

.top p {
text-align: center;
color: #999999;
font-size: 10px;
}

a:link {
color: #666666;
}
a:visited {
color: #666666;
}
a:hover {
color: #000000;
}
a:active {
color: #666666;
}

Adding The jQuery

Download the latest jQuery library from the jQuery website, place the library file inside your “JS” folder.

Next, open up notepad then go to “File > Save As” save the blank notepad file as “Scroll.js” inside your “JS” folder.

Open up your “Scroll.JS” file inside your code editor then paste the following snippet inside and save.

$(document).ready(function(){
$(".scroll").click(function(event){
//prevent the default action for the click event
event.preventDefault();

//get the full url - like mysitecom/index.htm#home
var full_url = this.href;

//split the url by # and get the anchor target name - home in mysitecom/index.htm#home
var parts = full_url.split("#");
var trgt = parts[1];

//get the top offset of the target anchor
var target_offset = $("#"+trgt).offset();
var target_top = target_offset.top;

//goto that anchor by setting the body scroll top to anchor top
$('html, body').animate({scrollTop:target_top}, 500);
});
});

Test your HTML file inside your browser and see how it works.

That’s it all done hope you enjoyed this tutorial.

About The Author

About The Author: Richard Carpenter

Hi im Richard Carpenter and im a freelance web and graphics designer from England. I am also a regular Blogger, Tutorial Writer, and owner of Photoshop Plus. You can follow me on twitter HERE. You may also view my portfolio HERE.

 

Richard Carpenter has written 364 posts.

Be Part Of The Community!

Become part of the hv-designs community.

Subscribe Via RSS or Follow Us On Twitter.

Subscribe Via RSS Follow Us On Twitter

40 Responses to “Learn How To Create A Scrollable Website”

  1. Ec says:

    Omg no way! That is so sick!
    Thanks for this tutorial!

  2. Andrea says:

    Nice, tut dud! I have an different idea for this, could we make that is goes from left to right instead of top to down??

  3. most says:

    very nice …

  4. Matias says:

    Nice, Very Nice Tut.

  5. Such an elegant smooth effect. Thanks for sharing. A horizontal version would also be interesting.

  6. Nice tutorial. If you like scrolling websites check out these two: http://centratissimo.musings.it (a little jQuery experimental navigation here) and http://www.festeggiano.it (more classical approach) :) Have fun!

  7. Hannes says:

    @Andrea I think the javascript is quite easy, just replace every top with left… or something along those lines.

    Only Problem I often have with this kind of solutions is that the result vary very much depending on the screen resolution.

  8. Ec says:

    I would also want to know if you can make it go from left to right.

  9. cesare casadonte says:

    compliment for the style

  10. @hannes :

    regarding having a different result on different screen resolutions. You could actually make each section 100% of the browser windoe and remove the scroll bars.

    thanks for the comments, much appreciated

  11. Avinash says:

    I Can do better than this :)

    Anyway thanks for sharing !!

  12. jzigbe says:

    Hi Richard,
    Please explain your comment to @hannes
    How do I make each section 100% of the browser window and remove the scroll bars?
    Sample code please!

  13. digof says:

    Really amazing any body gimme a link for full download??

  14. @DIGOF – the download is available in the “freebies pack #4″

  15. Jeroen says:

    Nice tutorial. :)

    Doesn’t this get “slow” if you have a lot of stuff going on on your site though?
    Since you’re basically working with only 1 page, that means everything will have to be loaded at the same time, right?

    (images, videos, all that)

    Good job nevertheless.

  16. FOR PEOPLE intrested in horizontally and diagonally scrolling websites – i found this tutorial here

    http://www.queness.com/post/356/create-a-vertical-horizontal-and-diagonal-sliding-content-website-with-jquery

  17. Angel Andraca says:

    Wow!! I really love? your tutorials!
    Keep up the good work!

  18. Andrea says:

    Thanks dud!

  19. Nenad says:

    Nice and easy. Thanks .

  20. Marcipa says:

    Thanks for sharing.
    Realy useful.
    M

  21. jzigbe says:

    I have changed a few things and implemented this here:
    http://www.school-furnitures.com/
    Comments welcome.

  22. azem says:

    awesome…rich…as always you rock again….

  23. Agon says:

    awesome…rich…as always, you rock again….thanks for sharing…keep it up…

  24. Ec says:

    Could I make it so it only scrolls the content but the navigation stays?

  25. thanks for this tutorial…

  26. Thats wonderful stuff you’ve got on this blog. Had been hunting for details on this all over. Nice work

  27. Mizafir says:

    Thank you.. Nice and easy.

  28. gnoa says:

    Hi, guys is really cool technique, thanks for sharing this whith us Richard, but what about IE, it doesn’t work, or maby i should do anything speacial for it, filter or ?….does any body else have same problem?

  29. Scrapsforever says:

    Its really useful. I am always searching Jquery tutorials its really nice one

  30. Anne says:

    Veryy beautiful..
    Thanks..

  31. nice tut Richard. thanks for sharing. i have always wanted to do this the easy way, and jquery its definitely the way. and pretty slick!

    thanks again.

    Luis S.

  32. RuleZ-DM says:

    Your blog I liked, I place your articles in my blog

  33. kaya says:

    Wow!! I really love? your tutorials!
    Keep up the good work!

  34. Wow, awesome weblog structure! How long have you ever been blogging for? you made blogging glance easy. The whole glance of your site is fantastic, as neatly as the content!

  35. Jovie (dianNe) Gayz says:

    Wow111, awesome….
    it’s beautiful and useful….
    thankz….

Leave a Reply