Simple slider prototype using json,jquery

Folks,

I developed a basic prototype design of a slider like plugin where next and previous will change the images. This has been achieved by getting details,images from external json file and imported to script and displayed in html.

Output will be like this..

23-07-2014 17-59-40

23-07-2014 18-00-13

 

For this, {title,image,address,contact} i am getting from an external json and reading in my script and displaying with html. Hope process is clear now. Find the below code..

Json File: Contains an array of records…

Snippet

{
 "hotels" : [
 
        {
            "id": 1,
            "title": "Tomster",
            "url": "images/3.png",
            "address": "chennai",
            "contact": "98948022338"
        }, 
        {
            "id": 2,
            "title": "Eiffel Tower",
            "url": "images/5.png",
            "address": "chennai",
            "contact": "98948022338"
        },
        {
            "id": 3,
            "title": "Taj Mahal",
            "url": "images/6.jpg",
            "address": "Agra",
            "contact": "98948022338"
        },
        {
            "id": 4,
            "title": "Charminar",
            "url": "images/7.jpg",
            "address": "Hyderabad",
            "contact": "98948022338"
        },
        {
            "id": 5,
            "title": "Qutub Minar",
            "url": "images/8.jpg",
            "address": "Chennai",
            "contact": "98948022338"
        }
 
 
]

}

Index.html file:

Snippet

<!DOCTYPE html>
 
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
        
    </head>
    <body >
         <div id="product_list"><img src="http://emberjs.com/images/about/ember-productivity-sm.png"/></div>
        <button id="prev">Prev</button>
        <button id="next">Next</button>
       
       <script src="js/jquery-1.10.2.js"></script>
        <script>
               $.ajax({
                url: 'js/w3.js',
                dataType: 'json',
                type: 'get',
                cache: false,
                success: function (data) {
                    //   console.log(data.hotels.length);
                    //   console.log(data);
                    var list = $(data.hotels).each(function (index, value) {
                        // alert(value.id);
                        
                    });
                    // console.log(content);
                    console.log(list.length);
                    //console.log(list[1].url);
                    index = 0; count = list.length;
                    $("#next").click(function () {
                        if (index < count) {
                             console.log(index);
                            content = '<p>' + list[index].title + '</p>';
                            content += '<img src="' + list[index].url + '"/>';
                            content += '<p>' + list[index].address + '</p>';
                            content += '<p>' + list[index].contact + '</p>';
                            $('#product_list').html('');
                            $(content).appendTo("#product_list").show(1000);
                            if (index != count - 1)
                                index = index + 1;
                            //console.log(content);
                            // alert("This is next");
                            
                            return false;
                        }
                    });
 
                    $("#prev").click(function () {
                        if (index != 0) {
                            index = index - 1;
                            content = '<p>' + list[index].title + '</p>';
                            content += '<img src="' + list[index].url + '"/>';
                            content += '<p>' + list[index].address + '</p>';
                            content += '<p>' + list[index].contact + '</p>';
                            $('#product_list').html('');
                            $(content).appendTo("#product_list").show(1000);
                            console.log(index);
                            return false;
                        }
                    });
 
                }
 
            });
 
 
        </script>
       
    </body>
</html>

You can even get source from here.

Advertisements

One thought on “Simple slider prototype using json,jquery

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s