binpress

Creating a Full-Page Scrolling Website with fullPage.js

Fullscreen websites are everywhere, and it’s highly likely that you’ll wind up making at least one if you’re a web designer. I was building one myself not long ago, and I realized there weren’t any jQuery plugins to make this easier. So, I made my own (fullPage.js) and open sourced it. In this tutorial I’ll show you how to create a basic full-screen scrolling website using fullPage. Live demo

Create the basic structure of the page

Before doing anything else, we have to create our basic HTML structure for the page. This is the same for all sites except for the DOCTYPE tag, which might change.

  1. <!DOCTYPE html>
  2. <html>
  3.  
  4. <head>
  5. </head>
  6.  
  7. <body>
  8.  
  9. </body>
  10. </html>

Be careful, don’t forget to add the DOCTYPE or you might have some problems.

Include the needed files

We’ll need to add four files: – jQuery – jQuery UI (Or its animation effects, at least.) – fullPage.js – fullPage’s css file

You can include them between the <head> tags of the page, although some people prefer to add the JavaScript files just before the closing </body> tag to improve content loading times. I wouldn’t recommend it in this case as this plugin sets the structure of the page and, unless it’s applied the content, it won’t look as expected.

Let’s add the files I named in the order detailed in the official documentation:

  1. <DOCTYPE html>
  2. <html>
  3. <head>
  4.     <link rel="stylesheet" type="text/css" href="jquery.fullPage.css" />
  5.     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  6.     <script src="vendors/jquery.easings.min.js"></script>
  7.     <script type="text/javascript" src="jquery.fullPage.min.js"></script>
  8. </head>
  9.  
  10. <body>
  11.  
  12. </body>
  13. </html>

Be careful, the order in which you include them is important. fullPage.js plugin requires the jQuery library to work and unless you include it before the plugin, it won’t be able to work.

Required HTML structure

Once we’ve included our files we can start adding our content in the <body> section of our site. fullPage works with vertical sections and horizontal slides. To use them, we’ll have to use the classes section and slide, respectively. (Although they’re configurable if you need.)

The plugin’s structure needs to be wrapped in another element which will be used later to initialize the plugin. For our example, we will use a wrapper with id="fullpage".

A site with 3 vertical sections would have this structure:

  1. <div id="fullpage">
  2.     <div class="section">Section 1</div>
  3.     <div class="section">Section 2</div>
  4.     <div class="section">Section 3</div>
  5. </div>

But to make it a bit more interesting, we are going to add also some horizontal slides in the 2nd section. It’s as simple as this:

  1. <div id="fullpage">
  2.     <div class="section">Section 1</div>
  3.     <div class="section">
  4.         <div class="slide">Section 2 Slide 1</div>
  5.         <div class="slide">Section 2 Slide 2</div>
  6.         <div class="slide">Section 2 Slide 3</div>
  7.     </div>
  8.     <div class="section">Section 3</div>
  9. </div>

Our file looks like this now:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <link rel="stylesheet" type="text/css" href="jquery.fullPage.css" />
  5.     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  6.     <script src="vendors/jquery.easings.min.js"></script>
  7.     <script type="text/javascript" src="jquery.fullPage.min.js"></script>
  8. </head>
  9.  
  10. <body>
  11.     <div id="fullpage">
  12.         <div class="section">Section 1</div>
  13.         <div class="section">
  14.             <div class="slide">Section 2 Slide 1</div>
  15.             <div class="slide">Section 2 Slide 2</div>
  16.             <div class="slide">Section 2 Slide 3</div>
  17.         </div>
  18.         <div class="section">Section 3</div>
  19.     </div>            
  20. </body>
  21. </html>

Initialization

Now that we have all we need, lets initialize the plugin to make the magic happen. To do so, we will need to add a <script> element in the header of our site. Just after the inclusion of all the needed files:

  1. <script type="text/javascript">
  2.     $(document).ready(function() {
  3.         $('#fullpage').fullpage();
  4.     });
  5. </script>

We’ve used the most simple initialization, but the plugin provides plenty of options you can configure as you can see in the official documentation.

As you can see, we’ve added the plugin initialization inside the ready function of jQuery. This is where most of the plugins get initialized as they need the structure of the page to be completely generated to start working with it.

Here’s the result of our current file:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <link rel="stylesheet" type="text/css" href="jquery.fullPage.css" />
  5.     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  6.     <script src="vendors/jquery.easings.min.js"></script>
  7.     <script type="text/javascript" src="jquery.fullPage.js"></script>
  8.  
  9.     <script type="text/javascript">
  10.         $(document).ready(function() {
  11.             $('#fullpage').fullpage();
  12.         });
  13.     </script>
  14. </head>
  15.  
  16. <body>
  17.     <div id="fullpage">
  18.         <div class="section">Section 1</div>
  19.         <div class="section">
  20.             <div class="slide">Section 2 Slide 1</div>
  21.             <div class="slide">Section 2 Slide 2</div>
  22.             <div class="slide">Section 2 Slide 3</div>
  23.         </div>
  24.         <div class="section">Section 3</div>
  25.     </div>            
  26. </body>
  27. </html>

Ready to work

And that’s it! We have our full-screen page working. You can save the changes in an .html file and open it in any browser to see the result.

You might notice the text isn’t horizontally centered and it’s quite small, but you can solve this by applying some of your CSS knowledge.

  1. .section{
  2.     font-size: 6em;
  3.     text-align: center;
  4. }

Also, you may want to add some background color to each section. To do so, we can use the option sectionsColor provided in the plugin. It’s as simple as modifying our initialization to look like this:

  1. $('#fullpage').fullpage({
  2.     sectionsColor: ['#f2f2f2', '#4BBFC3', '#7BAABE', 'whitesmoke']
  3. });

Now it is going to look amazingly beautiful! Here’s our final .html file:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <link rel="stylesheet" type="text/css" href="jquery.fullPage.css" />
  5.     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  6.     <script src="vendors/jquery.easings.min.js"></script>
  7.     <script type="text/javascript" src="jquery.fullPage.js"></script>
  8.  
  9.     <script type="text/javascript">
  10.         $(document).ready(function() {
  11.             $('#fullpage').fullpage({
  12.                 sectionsColor: ['#f2f2f2', '#4BBFC3', '#7BAABE', 'whitesmoke', '#000'],
  13.             });
  14.         });
  15.     </script>
  16.  
  17.     <style>
  18.         .section{
  19.             font-size: 6em;
  20.             text-align: center;
  21.         }
  22.     </style>
  23. </head>
  24.  
  25. <body>
  26.     <div id="fullpage">
  27.         <div class="section">Section 1</div>
  28.         <div class="section">
  29.             <div class="slide">Section 2 Slide 1</div>
  30.             <div class="slide">Section 2 Slide 2</div>
  31.             <div class="slide">Section 2 Slide 3</div>
  32.         </div>
  33.         <div class="section">Section 3</div>
  34.     </div>            
  35. </body>
  36. </html>

Additional information

Linking sections and slides

It is as simple as using the URL when using the anchors option: For example: http://alvarotrigo.com/fullPage/#secondPage/2

You can do it by using the index of the slide (starting by 0), or if you prefer, you can create custom anchor links for them by using the attribute data-anchor in each slide. For example:

  1. <div class="section">
  2.     <div class="slide" data-anchor="slide1"> Slide 1 </div>
  3.     <div class="slide" data-anchor="slide2"> Slide 2 </div>
  4.     <div class="slide" data-anchor="slide3"> Slide 3 </div>
  5.     <div class="slide" data-anchor="slide4"> Slide 4 </div>
  6. </div>

Be careful! data-anchor tags can not have the same value as any ID element on the site (or NAME element for IE).

Methods

  1. $.fn.fullpage.moveSectionUp();
  2. $.fn.fullpage.moveSectionDown();
  3. $.fn.fullpage.moveTo(section, slide);
  4. $.fn.fullpage.moveSlideRight();
  5. $.fn.fullpage.moveSlideLeft();
  6. $.fn.fullpage.setAutoScrolling(boolean);
  7. $.fn.fullpage.setAllowScrolling(boolean);
  8. $.fn.fullpage.setKeyboardScrolling(boolean);
  9. $.fn.fullpage.setScrollingSpeed(700);
  10. $.fn.fullpage.destroy(type); (type = all or empty)
  11. $.fn.fullpage.reBuild();

Callbacks

  1. onLeave: function(index, nextIndex, direction){},
  2. afterLoad: function(anchorLink, index){},
  3. afterRender: function(){},
  4. afterResize: function(){},
  5. afterSlideLoad: function(anchorLink, index, slideAnchor, slideIndex){},
  6. onSlideLeave: function(anchorLink, index, slideIndex, direction){}

More information

You can find more information about it in the official documentation or in the author’s blog.

Author: Alvaro

Scroll to Top