views:

259

answers:

2

Facebook and myspace both have a nice feature in the photo galleries, when you go to an image page, a page with the user's image and user comments on the page. You can hit you keyboard left and right arrow key and the page will load the next page, it does it super fast, if you held down the key it would go fast through many pages. I know this is mostly with javascript, my main question is how to make the new pages load all soo quickly?

Im using PHP/mysql/Javascript

+1  A: 
Pekka
+1  A: 

You'd capture the keys the user is pressing, and response for left-arrows and right-arrows. Capturing keys isn't all that difficult:

$(window).bind("keypress", function(e) {
  var code = (e.keyCode ? e.keyCode : e.which); // which code?
  alert(String.fromCharCode(code)); // which key, according to the code?
});

You'd want to adapt that code to your liking. If the right arrow was click, fire off a request to get-image.php?direction=next&user=12838203 and if it were the back-arrow, simply swap out "next" for "prev".

$("#loading").show(); // fancy ajax loading animation
$.post("get-image.php", {direction:"next",user:12838203}, function(response){
  // hide our ajax loading animation
  $("#loading").hide();
  // if we get a proper response, like a source for a new image back
  $("#image").fadeOut("fast").attr("src", response).fadeIn("fast");
});
Jonathan Sampson
facebook for example when you go to the next photo in an album with a key-press they will load a new image, ads, and all commments that should go with the new image but 1 thing they also do is they will change the id in the URL somehow without reloading the whole page. Thanks for the help
jasondavis
Facebook changes the `window.location.hash`. Getting comments could be part of what the server replies with. The image, and the comments. Or you could have a separate call for the comments.
Jonathan Sampson