views:

1552

answers:

4

Can any one tell me how can i implement a auto slider/scroller in my web page. The slider should show dynamic data from a database. (Ex : Hot jobs tab in the plipl.com site's home page (www.plipl.com) . Is there any easy way to do this with jQuery ?

+1  A: 

yes. this is easy. grab your data from the database. lets say its a list of HotJob objects, each object containing some String fields. Convert each object to a JSON object (which is a hash). Make a list of these JSON objects. use your web framework X to print out the JSON.

Use jquery's getJSON to grab the JSON object. Iterate over each HotJob and write out the information.

Google for "jquery json scroller" and find something that supports JSON out of the box. this does: http://www.logicbox.net/jquery/simplyscroll/flickr.html sure, its images, but you can modify it to support text.

mkoryak
+1  A: 

jCarousel is a jQuery carousel plugin that supports dynamic content. Although there are no JSON-driven examples on his website, it is easy enough to implement using jQuery's default AJAX functionality.

This is the most robust and customizable carousel I have found thus far for jQuery.

Mark Hurd
+1  A: 

Or if you don't want to use plugins you could use jQuery's AJAX functionality to load in the data, have a div with overflow:hidden and populate it with the data loaded in, only showing a section at a time.

You could then use the setInterval() function to change the $("#container").scrollTop(xx) to move the next set of information into view at a set interval. You could scroll it in or have it popup suddenly, up to you but fairly easy using jQuery.

Fermin
Can you please provide me a sample code snipeet ?
Shyju
+1  A: 

The following is nothing special, just quickly tried it. There's 2 div's that it continously switches between, hopefully give you a head start. You could use the same idea and load content into div's via AJAX.

Code snippet:

css:

div{width:100px;height:100px;}
#container{overflow:hidden;border:1px solid black;}
#left{background:red;float:left;}
#right{background:green;float:right;}

jScript:

$(function() {
    //Call scrollContent function every 3secs
var timerUp = setInterval(scrollContent, 3000);

function scrollContent(){
    //Toggle top between 100 and 0
    var top = $("#container").scrollTop() == 0 ? 100 : 0;
    $("#container").scrollTop(top);
}

});

markup:

<div id="container">
        <div id="left">
            <ul>
                <li>One</li>
                <li>Two</li>
                <li>Three</li>
                <li>Four</li>
            </ul>
        </div>
        <div id="right">
            <ul>
                <li>Five</li>
                <li>Six</li>
                <li>Seven</li>
                <li>Eight</li>
            </ul>
        </div>
    </div>
Fermin
Thanks femin ,Unfortunately This is not working when i tried .I can see both divs inside the main div always. Did u miss anything to post here . No transition effect also there
Shyju
Femin, It worked .Actually i need to reduce the size of main container to show only one div at a time. Thanks. When swithing the Div's can we have some fading /sliding effects ?IF the number of divs are 5, What change need to be done ?
Shyju
Hi, glad it worked. The same premise should work for the 5 divs, if they're each 100px move scrollTop to 100 then 200..300..400 etc. then back to 0 after you've shown the contents of the 5th div.For transitions you could use jQuery animate or jQuery UI built in effects, or a timer to set the ScrollTop of the container div in increments of 5 so that it flows rather than just pops up? Sorry, I don't have any development tools on the PC to try it out.
Fermin