Sunday, February 23, 2014

Image Rotator with jQuery & JSON

In this post i am going to create a image rotator with jQuery and JSON for this i am using a logic less template libraries Mustache.js which get the data from the external JSON file and a jQuery plugin cycle.js which rotate the data
Image Rotator with jQuery and JSON

Download Script View Demo 

html

html is very simple in html we have a main div a rotaor div in this div all the data from the json file will be feeded. we have 2 a tag which acts like next and previous button
<div id="main">
 <a href="#" id="prev_btn" class="btn">&laquo;</a>
        <a href="#" id="next_btn" class="btn">&raquo;</a>
 <div id="rotaor">
 </div>
</div>

JavaScript

javascript has very less code in simple word we are getting the data from the json file via jQuery and formatting the data with mustache.js and creating the rotator with cycle.js plugin
<!--formating the data fetched from the json file with mustache.js start-->
 <!--mustache.js template code-->
 <script type="text/template" id="mustacheTemplate">
 {{#hunklessons}}
  <div id="info">
  <h3>{{name}}</h3>
  <img src="{{photo}}" style="height:100px;" alt="image of {{name}}">
  </div>
 {{/hunklessons}}
 </script>
 <!--formating the data fetched from the json file with mustache.js end-->
 <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.0/jquery.js"></script>
 <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery.cycle/3.03/jquery.cycle.all.min.js"></script>
 <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/mustache.js/0.7.2/mustache.js"></script>
 <script type="text/javascript">
 $(document).ready(function(){
  $.getJSON('hunklessons.json', function(data){//get the data from json file
   var temp = $('#mustacheTemplate').html(); //target the mustache.js template code
   var html = Mustache.to_html(temp, data);//using mustache.js to_html function to match template from the json data
   $('#rotaor').html(html);//put the data to the rotaor div

   $('#rotaor').cycle({ //using cycle.js plugin
    fx: 'fade',
    pause: 1,
    next: '#nxt_btn',
    prev: '#prev_btn',
    speed: 500,
    timeout: 1000
   });
  });
 });
 </script>

json

{"hunklessons":[
 {
  "name":"husain saify",
  "photo":"http://lh5.googleusercontent.com/-oqoAWmCo7CA/AAAAAAAAAAI/AAAAAAAAAPk/osoYaXlwlxI/s512-c/photo.jpg"
 },
 {
  "name":"hunk lessons",
  "photo":"http://3.bp.blogspot.com/-snGcDs0qmtY/Uv4F13kHtRI/AAAAAAAAAMk/x20wmnG6j_I/s1600/hunlessonslogo.png"
 },
 {
  "name":"Embed Youtube Videos With JavaScript",
  "photo":"http://2.bp.blogspot.com/-cNbriEi6Pio/UwXyIfzXQUI/AAAAAAAAAOA/l9rAlFgah5k/s1600/embed-yt-videos.png"
 },
 {
  "name":"Drag and Drop Game With html5 & javascript",
  "photo":"http://4.bp.blogspot.com/-NDMehgFOvWM/UwNMEcA1XLI/AAAAAAAAANk/c__iisocakg/s1600/dragndropgame.png"
 },
 {
  "name":"Css3 Progress Bar",
  "photo":"http://2.bp.blogspot.com/--J0-soRNh3M/UwC64Y6ttnI/AAAAAAAAANQ/JEu1I2zJTpE/s1600/cssprogressbar.png"
 },
 {
  "name":"jQuery Username Availability check",
  "photo":"http://1.bp.blogspot.com/-m4x9VmOI-2E/Uv5PtDXZlnI/AAAAAAAAAMw/QiAqCH-lNr8/s1600/username+available.png"
 },
 {
  "name":"Select And Upload Multiple Images With jQuery & Ajax",
  "photo":"http://3.bp.blogspot.com/-LVKfO3xavWU/UwBgusO2WuI/AAAAAAAAANA/kpzTqM4l0E8/s1600/selectanduploadmultipleimaes.png"
 },
 {
  "name":"hunk husain",
  "photo":"http://lh5.googleusercontent.com/-oqoAWmCo7CA/AAAAAAAAAAI/AAAAAAAAAPk/osoYaXlwlxI/s512-c/photo.jpg"
 }
]}

Like Us on Facebook / Add Hunk As Friend