Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP][JavaScript]pobieranie nazw pliku z katalogu do skryptu
Forum PHP.pl > Forum > Przedszkole
shd
Witam,
Jako mocno początkujący w temacie js nie potrafię sobie poradzić z poniższym skryptem. A chodzi o to, że chciałbym, aby pobierał on obrazki ze wskazanego katalogu, a nie żebym musiał ręcznie podawać nazwy i ścieżki plików.
Znalazłem kilka rozwiązań i próbowałem wykorzystać funkcje glob czy readdir ale nie wiem czemu - nie działało mi to.
Czy mógłby ktoś na to zerknąć i doradzić jak stworzyć tablicę, z której skrypt będzie korzystał?
(Oryginalny skrypt pochodzi z http://srobbin.com/jquery-plugins/backstretch )

  1. // Create an array of images that you'd like to use
  2. var images = new Array();
  3. images[1] = "img/bg1.jpeg";
  4. images[2] = "img/bg2.jpeg";
  5. images[3] = "img/bg3.jpeg";
  6. images[4] = "img/bg4.jpeg";
  7. images[5] = "img/bg5.jpeg";
  8. images[6] = "img/bg6.jpeg";
  9. images[7] = "img/bg7.jpeg";
  10. images[8] = "img/bg8.jpeg";
  11. images[9] = "img/bg9.jpeg";
  12.  
  13.  
  14. Array.prototype.shuffle = function() {
  15. var len = this.length;
  16. var i = len;
  17. while (i--) {
  18. var p = parseInt(Math.random()*len);
  19. var t = this[i];
  20. this[i] = this[p];
  21. this[p] = t;
  22. }
  23. };
  24.  
  25. images.shuffle();
  26.  
  27. // A little script for preloading all of the images
  28. // It"s not necessary, but generally a good idea
  29. $(images).each(function(){
  30. $("<img/>")[0].src = this;
  31. });
  32.  
  33. // The index variable will keep track of which image is currently showing
  34. var index = 0;
  35.  
  36. // Call backstretch for the first time,
  37. // In this case, I"m settings speed of 500ms for a fadeIn effect between images.
  38. $.backstretch(images[index], {speed: 500});
  39.  
  40. // Set an interval that increments the index and sets the new image
  41. // Note: The fadeIn speed set above will be inherited
  42. setInterval(function() {
  43. index = (index >= images.length - 1) ? 0 : index + 1;
  44. $.backstretch(images[index]);
  45. }, 5000);
rocktech.pl
Witam.

  1. $img = json_encode( glob("*.jpg") );


Zapoznaj się z http://api.jquery.com/jQuery.parseJSON/

[JAVASCRIPT] pobierz, plaintext
  1.  
  2. // Create an object of images that you'd like to use
  3.  
  4. var images = jQuery.parseJSON('<?php echo $img; ?>')
  5.  
  6. // A little script for preloading all of the images
  7.  
  8. // It"s not necessary, but generally a good idea
  9.  
  10. $(images).each(function(){
  11.  
  12. $("<img/>")[0].src = this;
  13.  
  14. });
  15. ....
  16.  
[JAVASCRIPT] pobierz, plaintext
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.