Carousel of JS / jQuery, PHP with JSON File that gracefully degrades
I decided to create a carousel which will be efficient and offer graceful degradation. The only Carousel I came across which offers such feature is the one of Yahoo! Canada (Featured Area of Home Page). Turn off the JS, and you will still see the same layout, with each Items on the right now acting as a link. Upon clicking them will take you to their corresponding page.
In this jQuery Carousel I use PHP to create the layout and using an external JSON File. This will offer degradation for old browsers or when JS is blocked. The Carousel displays either image file or can load a Full HTML File.
The Carousel
Files that will be used:- class.carousel.php
- jquery.simplecarousel.js
- list.json
- simplecarousel.css
Brief Explanation of the files:
| class.carousel.php | This is the Class File. All you have to do is initialize the Class and call the Method createCarousel to create the Layout |
| jquery.simplecarousel.js | This is the JS File. The JS was created as a jQuery plugin, and its used just like any other jQuery plugin. |
| list.json | This is the external file which lists the images/html files to be loaded. The filename is customizable |
| simplecarousel.css | This is CSS File used for styling the Carousel. |
{ "entry" : [ { "type" : "html" , "url" : "http://www.yahoo.ca/", "src" : "/myblog/final/item1.html" , "title" : "Batman", "body" : "Dark Knight Story" }, { "type" : "image" , "url" : "http://www.flickr.com/", "src" : "/myblog/final/images/flickr.jpg", "title" : "Flickr", "body" : "Its Awesome!" } ] }
As you can see, each entry contains 'type', 'url', 'src', 'title', 'body'. Below are the explanation of these terms:
| type | It can be either 'html' or 'image' |
| url | This is the Link. When type is specified as 'image', then the entire image acts as a link, and the value of link is this url. |
| src | This is the source from where the image or HTML is to be loaded |
| title | This is the title to be shown on 'right' side (depending on Layout) |
| body | This is the body text shown on 'right' side (depending on Layout) |
Details of the JS File:
Show Code »
Usage:
Include the CSS and JS File at first. Since this is a jquery plugin, at first include jquery like any other plugin. Then include this js file
<style type="text/css" media="all"> @import url('simplecarousel.css'); </style>
<script language="JavaScript" type="text/javascript" src="jquery.simplecarousel.js"></script>
Now, include the PHP File, intialize the class by passing the JSON Filename as parameter and call the Method createCarousel where you want to show the carousel. You can also set the JSON File later by using the Method setJSONFile()
include 'path/to/file/class.carousel.php'; $carousel = new Carousel('filename.json'); // you can also over-ride it by calling this Method $carousel->setJSONFile('filename.json'); // now call this Method below to output the Carousel $carousel->createCarousel();
If you have seen the JS Code, it contains one Method called simpleCarousel(filename, params). Some brief information is below:
simpleCarousel(filename, params);
Parameters:
| filename | the JSON File |
| params | Javascript Object. Attributes are timer, delay and index. timer specifies the time after which the function will be called, by default 'timer' is 5000 (5s). delay is the animation/fade out delay, by default it's 1000 (1s). index is the entry's index in JSON File. |
$('.cFeature').simpleCarousel('path/to/filename.json'); // the 2nd parameter is optional and you can use it to over-ride default values $('.cFeature').simpleCarousel('path/to/filename.json', {timer: 6500, delay : 2000});
The PHP File simply echos the layout and generates the list of related Items on the right column based on JSON File.
The following are the list of classes echoed by PHP by default, but they can be overriden:| cWrap | This is a Wrapper Div of the entire Carousel |
| cFeature | This is the feature Div where the current Item is displayed |
| cRelated | This contains the related Carousel Items. |
Now, using the carousel class both cFeature and cRelated can be overriden. If you look inside the PHP Class file, there a public instance variable called '$cssClass', which is an associative array containing two elements 'feature' and 'related'. Just do the following:
$carousel->cssClass['feature'] = 'YOUR_OWN_FEATURED_CSS_CLASS_NAME'; $carousel->cssClass['require'] = 'YOUR_OWN_RELATED_CSS_CLASS_NAME';
Here's a demo:
Feel Free to block JS and see how it degrades.
Oh, I forgot to mention the file sizes. Its quite small too.
File Sizes:
1. jquery.simplecarousel.js - 2.4kb (normal).. Minified = 1.7kb
2. class.simplecarousel.php - 2kb (normal)
I have attached a zip file containing all the test images, html files along with required ones. Please run 'test.php' file which will output the carousel.
(Note: jQuery File has not been included, so please change the path of jquery file on test.php file to your corresponding jquery.js location)
final.zip
That's it for now!
