slideshow.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. var numSlides = 0;
  2. var currentSlide = 0;
  3. var slidesReady = false;
  4. $(document).ready(function() {
  5. $(document).keyup(handleKeys);
  6. numSlides = $("div.slide").each(function (i) {
  7. $(this).attr("id", "slide_" + i);
  8. }).size();
  9. readySlides();
  10. showSlide();
  11. });
  12. function readySlides() {
  13. slidesReady = true;
  14. $("div.slide").css("display", "none").css("position", "absolute").css("height", "90%").click(nextSlide);
  15. }
  16. function unreadySlides() {
  17. slidesReady = false;
  18. $("div.slide").css("display", "inline").css("position", "static").css("height", "12em").click(function() { gotoSlide($(this)); });
  19. }
  20. function handleKeys(e) {
  21. if (! slidesReady) {
  22. return;
  23. }
  24. var keyID = (window.event) ? event.keyCode : e.keyCode;
  25. switch(keyID) {
  26. case 27: // escape
  27. unreadySlides();
  28. break;
  29. case 32: // space
  30. nextSlide();
  31. break;
  32. case 37: // left
  33. prevSlide();
  34. break;
  35. case 38: // up
  36. setSlide(0);
  37. break;
  38. case 39: // right
  39. nextSlide();
  40. break;
  41. case 40: // down
  42. lastSlide();
  43. break;
  44. }
  45. }
  46. function showSlide() {
  47. $("div.slide").css("display", "none");
  48. $("#slide_" + currentSlide).css("display", "inline");
  49. }
  50. function setSlide(number) {
  51. currentSlide = number;
  52. showSlide();
  53. }
  54. function nextSlide() {
  55. currentSlide++;
  56. if (currentSlide >= numSlides) {
  57. currentSlide = 0;
  58. }
  59. showSlide();
  60. }
  61. function prevSlide() {
  62. currentSlide--;
  63. if (currentSlide < 0) {
  64. currentSlide = 0;
  65. }
  66. showSlide();
  67. }
  68. function lastSlide() {
  69. currentSlide = (numSlides - 1);
  70. showSlide();
  71. }
  72. function gotoSlide(slide) {
  73. var number = slide.attr("id").substring(6);
  74. readySlides();
  75. setSlide(number);
  76. }