miércoles, 28 de noviembre de 2012

Code a Dynamic Questions & Answers FAQ Page with jQuery

Code a Dynamic Questions & Answers FAQ Page with jQuery:

You can perform many various effects using the jQuery JavaScript library. It’s an open source project which has been gaining followers for a couple years now. Aside from the many jQuery plugins you can build a lot of custom web functionality right from scratch.
I want to use this tutorial to showcase how we can build a custom FAQ webpage layout. I’ll be using a small bit of JavaScript to show and hide the answers. We could include any type of data like static text, images, or videos. Additionally you could port this code into your own layout and custom CSS codes. So without further ado let’s get started!
screenshot preview jQuery CSS3 questions and answers
Live DemoDownload Source Code

Document Header Scripts

To build the demo we need to create a new file named index.html which will serve as the FAQ page. I’ll be using a typical HTML5 doctype with some external header includes. Among some of these is a custom stylesheet named styles.css and a remote font style from Google Web Fonts.

<!doctype html>
<html lang="en-US">
<head>
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
  <title>Dynamic Question/Answer FAQ Webpage Layout</title>
  <meta name="author" content="Jake Rocheleau">
  <link rel="shortcut icon" href="http://spyrestudios.com/favicon.ico">
  <link rel="icon" href="http://spyrestudios.com/favicon.ico">
  <link rel="stylesheet" type="text/css" href="styles.css">
  <link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Paprika">
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
</head>

I have also included a reference to the Google-hosted jQuery library. There are some alternate CDNs with jQuery and Microsoft, but any of them will work fine. If you wanted to jazz up the fading animations include a copy of the jQuery UI Library too. You can setup custom easing functions which the animations will mimic.
Inside the body area we also have a very basic code setup. I am using a whole outer wrapper div which contains all our main body content. Then I’ve got questions divided up into divs with the class .q and all internal elements labeled as well.

Body Classes

If you notice on the demo page I’ve split up the questions into three different sections. The script runs on unique IDs so it doesn’t matter where the questions are contained. Therefore you can have a lot of flexibility switching between headers and sub-headers to contain further questions.

<h2>User Accounts</h2>
<div class="q">
  <h3 class="qhead"><a href="#q05">Can I have more than one user account?</a></h3>
  <div class="answer" id="q05"><p>Nope, sorry.</p></div>
</div>

<div class="q">
  <h3 class="qhead"><a href="#q06">How Do I change my user profile photo?</a></h3>
  <div class="answer" id="q06"><p>Access your user Settings panel from the profile screen. Then click on your default profile photo to upload a new image.</p></div>
</div>

Each of the question links are contained inside an h3 tag to promote exclusivity. The anchor link value is a hash related to the answer div’s ID value. This is probably the quickest way to retrieve a value from jQuery, but you may customize this to anything you need.
Inside the .answer div you can place any type of content related to the answer. The whole div is given a CSS display: none; rule to hide everything when the page loads. This shouldn’t interfere with smaller media objects, such as embedded videos or image slideshows.

Simple CSS Styles

Aside from the typical CSS resets I have included some other base properties. This mostly has to do with typography and the page layout styles. But you will notice that the code is very elegant and easy to read. Start off by creating a new file styles.css and include some of the base codes.

/* typography */
h1 { font-family: 'Paprika', Tahoma, Arial, sans-serif; font-size: 2.65em; line-height: 1.55em; margin-bottom: 10px; font-weight: normal; color: #6c635e; }
h2 { font-size: 2.1em; line-height: 1.0em; font-weight: normal; font-style: italic; letter-spacing: 0.05em; color: #778657; margin-bottom: 2px; }
h3 { font-size: 1.45em; }

p { display: block; font-size: 1.4em; line-height: 1.4em; color: #656565; margin-bottom: 15px; }

a { color: #1565ba; text-decoration: none; }
a:hover { text-decoration: underline; } 

First I am setting up our major content for paragraphs, links, and headers. These are the only elements I am positioning inside the FAQ page. But it would be good practice to keep similar typographic styles in the same location within your own stylesheet.

/* wrapper div */
#w { 
display: block; 
margin: 0 auto; 
padding: 18px 25px;
min-width: 400px; 
max-width: 900px; 
background: #fff; 
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px; 
-webkit-box-shadow: 1px 1px 2px rgba(0,0,0,0.4);
-moz-box-shadow: 1px 1px 2px rgba(0,0,0,0.4);
box-shadow: 1px 1px 2px rgba(0,0,0,0.4);
}

/* question containers */
.q { display: block; max-width: 550px; margin-bottom: 16px; }

.qhead { margin-bottom: 3px; }
.qhead a:hover { text-decoration: none; color: #428ddc; border-bottom: 4px solid #c4d3ee; }

.answer { display: none; }

And then finally we have the last section of codes for our FAQ display. I am using some nice CSS3 effects on the container window. You can notice they really stand out on the page and against the background. Also I have the question containers limited at 550px width to save space. In any other ideal website layout you would probably have a sidebar or two which would limit the body width.

Creating the Fade Effect

This last part is accomplished with just a few lines of jQuery and shouldn’t be difficult to follow. Instead of creating a new document I have simply embedded this code into a script tag right before closing the page body.

$(document).ready(function(){
  $(".qhead a").on("click", function(e){
    e.preventDefault();
    var href = $(this).attr("href");
    
    $(href).fadeToggle(450);
  });
});

After the document finishes loading then I am placing a click event listener on the .qhead links. This means whenever the user clicks on any of our header links we call the internal function code. It starts by targeting the click event variable e and using the preventDefault() method. This is how we stop the page from loading each HREF value into the URL bar.
The other code is targeting the href attribute value on the clicked link and using this as a new jQuery selector. This means we can quickly target the proper answer div and use .fadeToggle() to hide or show content as needed. Obviously a much easier solution than trying to determine if the paragraph is already displaying to hide or show accordingly.
screenshot preview jQuery CSS3 questions and answers
Live DemoDownload Source Code

Final Thoughts

This tutorial is a bit self-explanatory if you are familiar with JavaScript development. But even some intermediate web developers could find this handy for duplicating the code on future projects. Building an FAQ page is a great way to offer solutions to your visitors and explain the purpose of your website.
I hope this tutorial can offer a great resource to some web designers & developers. You can download a copy of my project source code from the links above. Feel free to play around and see if you can build any additional features.

No hay comentarios:

Publicar un comentario