Bootstrap tutorial / parallax scrolling tutorial. It is no secret that the bootstrap is used on many websites now. This allows to save time and create excellent results in a short period of time. In particular it concerns the markup and styles. Last time, when we wrote about the bootstrap, we received many positive comments, so I decided to prepare another lesson on using this framework today. In order to make it more interesting, we’ll create the demonstration with one of the interesting effects – Parallax. We will see the use of different backgrounds when scrolling the page. In order to understand what we intend to do – please have a look at the demo.
Step 1. HTML
For a start – prepare a skeleton markup:
index.html
04 |
< meta charset = "utf-8" /> |
05 |
< meta name = "author" content = "Script Tutorials" /> |
06 |
< title >Bootstrap one-page template with Parallax effect | Script Tutorials</ title > |
07 |
< meta name = "viewport" content = "width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" > |
09 |
< link href = "css/bootstrap.min.css" rel = "stylesheet" > |
10 |
< link href = "css/style.css" rel = "stylesheet" /> |
15 |
< script src = "js/jquery-1.10.2.js" ></ script > |
16 |
< script src = "js/bootstrap.min.js" ></ script > |
17 |
< script src = "//maps.google.com/maps/api/js?sensor=true" ></ script > |
18 |
< script src = "js/main.js" ></ script > |
This is the minimal markup which allows us to use the responsive nature of the Bootstrap. Now we can begin with adding new elements to the page.
Top navigation menu
This is the fixed navigation bar. Here is the markup:
02 |
< nav class = "navbar navbar-default navbar-fixed-top" role = "navigation" > |
03 |
< div class = "container-fluid" > |
05 |
< div class = "navbar-header" > |
06 |
< button type = "button" class = "navbar-toggle" data-toggle = "collapse" data-target = "#navbar-collapse-main" > |
07 |
< span class = "sr-only" >Toggle navigation</ span > |
08 |
< span class = "icon-bar" ></ span > |
09 |
< span class = "icon-bar" ></ span > |
10 |
< span class = "icon-bar" ></ span > |
12 |
< a class = "navbar-brand" href = "#" >Your Brand Name</ a > |
14 |
< div class = "collapse navbar-collapse" id = "navbar-collapse-main" > |
15 |
< ul class = "nav navbar-nav navbar-right" > |
16 |
< li >< a href = "#home" >Home</ a ></ li > |
17 |
< li >< a href = "#about" >About</ a ></ li > |
18 |
< li >< a href = "#services" >Services</ a ></ li > |
19 |
< li >< a href = "#information" >Information</ a ></ li > |
20 |
< li >< a href = "#google_map" >Contact</ a ></ li > |
Basically, this is UL-LI-based menu (as usual). There are links to different sections of the page
Section 1 – Home
The first section is full-sized greeting page:
2 |
< div id = "home" class = "home" > |
3 |
< div class = "text-vcenter" > |
5 |
< h3 >This is bootstrap-based layout</ h3 > |
6 |
< a href = "#about" class = "btn btn-default btn-lg" >Continue</ a > |
The greeting text is aligned vertically by center.
Section 2 – About
There are two columns, the image is in the first columnm and centered text is in the right column:
02 |
< div id = "about" class = "pad-section" > |
03 |
< div class = "container" > |
05 |
< div class = "col-sm-6" > |
06 |
< img src = "images/logo.png" alt = "" /> |
08 |
< div class = "col-sm-6 text-center" > |
09 |
< h2 >Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec in sem cras amet.</ h2 > |
10 |
< p class = "lead" >Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed interdum metus et ligula venenatis, at rhoncus nisi molestie. Pellentesque porttitor elit suscipit massa laoreet metus.</ p > |
Section 3 – Services
This blue section consists of four elements with glyph icons provided by the bootstrap:
02 |
< div id = "services" class = "pad-section" > |
03 |
< div class = "container" > |
04 |
< h2 class = "text-center" >Our Services</ h2 > < hr /> |
05 |
< div class = "row text-center" > |
06 |
< div class = "col-sm-3 col-xs-6" > |
07 |
< i class = "glyphicon glyphicon-cloud" > </ i > |
09 |
< p >Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec in sem cras amet. Donec in sem cras amet.</ p > |
11 |
< div class = "col-sm-3 col-xs-6" > |
12 |
< i class = "glyphicon glyphicon-leaf" > </ i > |
14 |
< p >Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec in sem cras amet. Donec in sem cras amet.</ p > |
16 |
< div class = "col-sm-3 col-xs-6" > |
17 |
< i class = "glyphicon glyphicon-phone-alt" > </ i > |
19 |
< p >Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec in sem cras amet. Donec in sem cras amet.</ p > |
21 |
< div class = "col-sm-3 col-xs-6" > |
22 |
< i class = "glyphicon glyphicon-bullhorn" > </ i > |
24 |
< p >Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec in sem cras amet. Donec in sem cras amet.</ p > |
Section 4 – Information
This is the second wide section with full-size background, plus, there are two additional blocks (panels):
02 |
< div id = "information" class = "pad-section" > |
03 |
< div class = "container" > |
05 |
< div class = "col-sm-6" > |
06 |
< div class = "panel panel-default" > |
07 |
< div class = "panel-heading" > |
08 |
< h2 class = "panel-title" >Additional information</ h2 > |
10 |
< div class = "panel-body lead" > |
11 |
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed hendrerit adipiscing blandit. Aliquam placerat, velit a fermentum fermentum, mi felis vehicula justo, a dapibus quam augue non massa. Duis euismod, augue et tempus consequat, lorem mauris porttitor quam, consequat ultricies mauris mi a metus. Phasellus congue, leo sed ultricies tristique, nunc libero tempor ligula, at varius mi nibh in nisi. Aliquam erat volutpat. Maecenas rhoncus, neque facilisis rhoncus tempus, elit ligula varius dui, quis amet. |
15 |
< div class = "col-sm-6" > |
16 |
< div class = "panel panel-default" > |
17 |
< div class = "panel-heading" > |
18 |
< h2 class = "panel-title" >Additional information</ h2 > |
20 |
< div class = "panel-body lead" > |
21 |
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed hendrerit adipiscing blandit. Aliquam placerat, velit a fermentum fermentum, mi felis vehicula justo, a dapibus quam augue non massa. Duis euismod, augue et tempus consequat, lorem mauris porttitor quam, consequat ultricies mauris mi a metus. Phasellus congue, leo sed ultricies tristique, nunc libero tempor ligula, at varius mi nibh in nisi. Aliquam erat volutpat. Maecenas rhoncus, neque facilisis rhoncus tempus, elit ligula varius dui, quis amet. |
Section 5 – Contacts and Footer
In fact, there are three elements – the narrow blue service line (to put some extra information), the google map, where you can put your company address (as a pin on the map):
02 |
< div id = "services" class = "pad-section" > |
03 |
< div class = "container" > |
05 |
< div class = "col-sm-12 text-center" > |
06 |
< h3 >Parallax scrolling effect is in action</ h3 > |
07 |
< h4 >The next is the address on Google maps</ h4 > |
14 |
< div id = "google_map" ></ div > |
19 |
< div class = "container" > |
20 |
< p class = "text-right" >Copyright © Your Company 2014</ p > |
Step 2. CSS
You may have noticed that most elements already received all necessary styles at once. However, we still have to define styles for all new non-standard elements:
css/style.css
17 |
vertical-align : middle ; |
27 |
background : url (../images/home.jpg) no-repeat center center fixed ; |
32 |
-webkit-background- size : cover; |
33 |
-moz-background- size : cover; |
34 |
-o-background- size : cover; |
35 |
background- size : cover; |
40 |
background-color : #306d9f ; |
43 |
#services .glyphicon { |
44 |
border : 2px solid #FFFFFF ; |
46 |
display : inline- block ; |
51 |
vertical-align : middle ; |
55 |
background : url (../images/estate.jpg) no-repeat center center fixed ; |
60 |
-webkit-background- size : cover; |
61 |
-moz-background- size : cover; |
62 |
-o-background- size : cover; |
63 |
background- size : cover; |
78 |
footer .glyphicon:hover { |
Step 3. JavaScript
In the end, there is the google map, in order to initialize it (with a predefined address) we have to add the following code:
js/main.js
01 |
$(document).ready( function (){ |
03 |
var latlng = new google.maps.LatLng(-33.86455, 151.209); |
08 |
mapTypeId: google.maps.MapTypeId.ROADMAP, |
09 |
navigationControl: true , |
10 |
mapTypeControl: false , |
12 |
disableDoubleClickZoom: true |
15 |
var map = new google.maps.Map(document.getElementById( 'google_map' ), options); |
17 |
var marker1 = new google.maps.Marker({ |
18 |
position: latlng, map: map |
21 |
google.maps.event.addListener(marker1, 'click' , function () { |
22 |
infowindow.open(map, marker1); |
25 |
var infowindow = new google.maps.InfoWindow({ |
26 |
content: '<div class="info"><strong>This is my company</strong><br><br>My company address is here<br> 32846 Sydney</div>' |
[sociallocker]
[/sociallocker]
Conclusion
That is all for now, we just finished the layout of our page. I hope that you like the result. Another advantage is the fact that this template is responsive by default, and it means that it looks good at all possible devices (including mobile phones).