


comprar viagra online
comprar viagra generico barato
comprar cialis generico online
comprar cialis online seguro
jQuery quickie: Unlimited Scroll using the Twitter API |
Time for another relatively simple jQuery tutorial, just like my previous jQuery quickie. At work, I'm currently working with Silverlight and implemented unlimited scroll. This is a great technique that could be used on loads of websites. Instead of the regular pagination, where the user has to click to see the next page, unlimited scroll automatically loads the next page when the user is at the bottom. I wanted to take this technique and port it to another jQuery example. So the quickie for today is Unlimited scroll using jQuery. I've using the Twitter API to make the example, so you'll learn a little bit about JSON too. ![]() Simply check out the demo to see the first tweets from my Twitter stream. Scroll down to load more tweets and see the unlimited scroll in action. Of course, you can use this same technique for something else instead of loading tweets, for example for loading next blog posts etc. Now, let's take a look at how you can create this Twitter example. HTML Just for the purpose of the example and aiming the focus on the jQuery part, I've created the most simplistic and minimal HTML you'll need. <div id="tweets"> <!-- Tweets will get loaded from jQuery --> </div> <div id="overlay"> <img src="images/ajax-loader.gif" /> </div> The first division ( Now to give this HTML some little bit of styling using CSS. CSS I've used (almost) the same CSS styling as my Unique website for your Twitter updates Reloaded example. We're using an #tweets { position: absolute; left: 186px; top: 105px; width: 376px; height:350px; overflow:auto; } #tweets p { font-size: 14px; margin-bottom: 10px; padding: 10px; color: #7a8a99; background: url("../images/transpBlue.png"); } We're also styling the paragraphs that'll be placed inside the Since we're using absolute positioning, we can easily place the overlay on top of the container. #overlay { position: absolute; left: 168px; top: 87px; width: 408px; height:386px; background: url("../images/transpBlue_overlay.png"); } #overlay img { position:relative; left:200px; top:189px; } As you can see, the image has been placed in the center of the container. Nothing really exiting going on over here, but now comes the most fun part: the jQuery code! jQuery After loading jQuery and waiting for the document to be ready with loading, we can start the jQuery script. First, we'll need a couple of variables. As usual, comments are added to make some things clear. // Set the size for each page to load var pageSize = 15; // Username to load the timeline from var username = 'marcofolio'; // Variable for the current page var currentPage = 1; Now, we need to call a function that'll load the first page. // First time, directly load the tweets loadTweets(); The // Loads the next tweets var loadTweets = function() { var url = "http://twitter.com/status/user_timeline/" + username + ".json?count="+pageSize+"&page="+currentPage+"&callback=?"; $.getJSON(url,function(data) { $.each(data, function(i, post) { appendTweet(post.text, post.id); }); // We're done loading the tweets, so hide the overlay $("#overlay").fadeOut(); }); }; As you can see, we retrieve the Twitter timeline by calling the API and retrieving the current page from the user. The jQuery After we're done loading the tweets, we hide the overlay. But wait - we're calling a function // Appends the new tweet to the UI var appendTweet = function(tweet, id) { $("<p />") .html(tweet) .append($("<a />") .attr("href", "http://twitter.com/" + username + "/status/" + id) .attr("title", "Go to Twitter status") .append($("<img />") .attr("src", "images/link.png") ) ) .appendTo($("#tweets")); }; We're using the jQuery function to dynamically the tweet paragraphs. We also add the link to the tweet by showing an image. We append the new element to the But now, we have the initial fill (the first page). We now need to detect that the user is scrolling the // Append a scroll event handler to the container $("#tweets").scroll(function() { // We check if we're at the bottom of the scrollcontainer if ($(this)[0].scrollHeight - $(this).scrollTop() == $(this).outerHeight()) { // If we're at the bottom, show the overlay and retrieve the next page currentPage++; $("#overlay").fadeIn(); loadTweets(); } }); Take note of the That's all there is to it! I've added a little bit more code in the demo, which you can check out yourself if you download the source. Conclusion and Download Unlimited scroll could be great for user experience (so the example with the Twitter API would be useful), but when you think about SEO, it's not recommended. After all, a search engine spider doesn't actually scroll. What do you think? Should unlimited scroll be implemented more often? Or do you prefer the old skool technique? And do you see where you could improve the code? Feel free to share! Tags: scroll twitter jquery tutorial simple Interested in this topic? You might enjoy another article I've written called |
< Prev | Next > |
---|
Search |
---|
Or try the sitemap |