There are a lot of free scripts to create a content slider. One of the example is SmoothGallery by JonDesign. But, if you are using jQuery and don’t want to install another JavaScript library, then you may need to consider jQuery slider plugin.

Today, i am going to provide a tutorial on using jFlow, a minimalist jQuery plugin to create a content slider. Readers are required to have basic XHTML/CSS and JavaScript skills. I will provide a download link after this tutorial. This is the first tutorial post on WebDesignBooth, and i will write more tutorials in future.

content-slider

So, let’s start to create our content-slider now. Download both jQuery and jFlowbefore you proceed.

  1. Create a style.css file, and insert the following code into the file:
    view plaincopy to clipboardprint?
    1. #jFlowSlide{ background:#DBF3FDfont-family: Georgia; }
    2. #myController { font-family: Georgia; padding:2px 0;  width:610pxbackground:#3AB7FF; }
    3. #myController span.jFlowSelected { background:#43A0D5;margin-right:0px; }
    4. .slide-wrapper { padding5px; }
    5. .slide-thumbnail { width:300pxfloat:left; }
    6. .slide-thumbnail img {max-width:300px; }
    7. .slide-details { width:290pxfloat:rightrightmargin-left:10px;}
    8. .slide-details h2 { font-size:1.5em; font-styleitalicfont-weight:normalline-height: 1; margin:0; }
    9. .slide-details .description { margin-top:10px; }
    10. .jFlowControl, .jFlowPrev, .jFlowNext { color:#FFFcursor:pointerpadding-left:5pxpadding-right:5pxpadding-top:2pxpadding-bottom:2px; }
    11. .jFlowControl:hover, .jFlowPrev:hover, .jFlowNext:hover { background#43A0D5; }
  2. Open your main document and include the style.css, jQuery, and jFlow in the <head> tag.
    view plaincopy to clipboardprint?
    1. <link rel=“stylesheet” href=“style.css” type=“text/css” media=“screen” />
    2. <script language=“javascript” type=“text/javascript” src=“jquery-1.3.2.min.js”></script>
    3. <script language=“javascript” type=“text/javascript” src=“jquery.flow.1.2.js”></script>
  3. Then add this simple JavaScript code to define the settings for jFlow
    view plaincopy to clipboardprint?
    1. <script type=“text/javascript”>
    2. $(document).ready(function(){
    3.     $(“#myController”).jFlow({
    4.         slides: “#slides”,  // the div where all your sliding divs are nested in
    5.         controller: “.jFlowControl”// must be class, use . sign
    6.         slideWrapper : “#jFlowSlide”// must be id, use # sign
    7.         selectedWrapper: “jFlowSelected”,  // just pure text, no sign
    8.         width: “610px”,  // this is the width for the content-slider
    9.         height: “235px”,  // this is the height for the content-slider
    10.         duration: 400,  // time in miliseconds to transition one slide
    11.         prev: “.jFlowPrev”// must be class, use . sign
    12.         next: “.jFlowNext” // must be class, use . sign
    13.     });
    14. });
    15. </script>

    Think #slides as a container, which contain all slides, while .jFlowControlis the controller which let us go to the individual slide. As you can see from the style.css, we have 300px .slide-thumbnail to display our image, 290px .slide-details to display our title and description. Besides these, we have 10px margin-left for the .slide-description and 5px padding for the .slide-wrapper. So totally we need 610px width. For the “height” setting, you need to estimate the maximum height for your content. Here, i will use 235px as an example.

  4. Now is the turn to create the individual slide. The slide is contained in the #slides div. Here, i will conly show one slide. You may refer the source code for more info.
    view plaincopy to clipboardprint?
    1. <div class=“slide-wrapper”>
    2.     <div class=“slide-thumbnail”>
    3.         <!– image here –>
    4.     </div>
    5.     <div class=“slide-details”>
    6.         <h2><!– title here –></h2>
    7.         <div class=“description”>
    8.             <!– description here. –>
    9.         </div>
    10.     </div>
    11.     <div class=“clear”></div>
    12. </div>

    We will wrap everything in a .slide-wrapper class, and float our thumbnail to the left while our content to the right. Don’t forget to clear the floats also. Replace image, title, and description with your own content.

  5. After that, we may add controls to the slides, which are resides in a #myController div.
    view plaincopy to clipboardprint?
    1. <span class=“jFlowPrev”>Prev</span>
    2. <span class=“jFlowControl”>1</span>
    3. <span class=“jFlowNext”>Next</span>

    The .jFlowPrev and .jFlowNext are necessary controls to go to previous and next slides, while .jFlowControl is a “button” to jump to any particular slide. The number of .jFlowControl span is depends on the number of slides you have.

  6. That’s all!! We had done everything. You can view the demo or download the source code to use in your own project.