Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [JS] Zmiana div po x czasie...
SN@JPER^
post
Post #1





Grupa: Zarejestrowani
Postów: 266
Pomógł: 0
Dołączył: 4.01.2007
Skąd: Szczecin

Ostrzeżenie: (0%)
-----


Witam,

w jaki sposób zrobić zmiane div po x czasie?

Chodzi mi bardziej o to, że jak div o danym id nie istnieje to nie idzie na początek...

  1. var myDiv_1 = document.getElementById('mains_1');
  2. var myDiv_2 = document.getElementById('mains_2');
  3. var myDiv_3 = document.getElementById('mains_3');
  4. var myDiv_4 = document.getElementById('mains_4');
  5. var myDiv_5 = document.getElementById('mains_5');
  6. var myDiv_6 = document.getElementById('mains_6');
  7.  
  8. if(myDiv_1.className != 'contentdiv none')
  9. {
  10.  
  11.  
  12.  
  13. myDiv_1.className = 'contentdiv none';
  14. myDiv_2.className = 'contentdiv block';
  15. myDiv_3.className = 'contentdiv none';
  16. myDiv_4.className = 'contentdiv none';
  17. myDiv_5.className = 'contentdiv none';
  18. myDiv_6.className = 'contentdiv none';
  19.  
  20. }
  21. else if(myDiv_2.className != 'contentdiv none')
  22. {
  23.  
  24. myDiv_1.className = 'contentdiv none';
  25. myDiv_2.className = 'contentdiv none';
  26. myDiv_3.className = 'contentdiv block';
  27. myDiv_4.className = 'contentdiv none';
  28. myDiv_5.className = 'contentdiv none';
  29. myDiv_6.className = 'contentdiv none';
  30.  
  31. }
  32. else if(myDiv_3.className != 'contentdiv none')
  33. {
  34.  
  35. myDiv_1.className = 'contentdiv none';
  36. myDiv_2.className = 'contentdiv none';
  37. myDiv_3.className = 'contentdiv none';
  38. myDiv_4.className = 'contentdiv block';
  39. myDiv_5.className = 'contentdiv none';
  40. myDiv_6.className = 'contentdiv none';
  41. }
  42.  
  43. else if(myDiv_4.className != 'contentdiv none')
  44. {
  45.  
  46. myDiv_1.className = 'contentdiv none';
  47. myDiv_2.className = 'contentdiv none';
  48. myDiv_3.className = 'contentdiv none';
  49. myDiv_4.className = 'contentdiv none';
  50. myDiv_5.className = 'contentdiv block';
  51. myDiv_6.className = 'contentdiv none';
  52. }
  53.  
  54. else if(myDiv_5.className != 'contentdiv none')
  55. {
  56.  
  57. myDiv_1.className = 'contentdiv none';
  58. myDiv_2.className = 'contentdiv none';
  59. myDiv_3.className = 'contentdiv none';
  60. myDiv_4.className = 'contentdiv none';
  61. myDiv_5.className = 'contentdiv none';
  62. myDiv_6.className = 'contentdiv block';
  63. }
  64.  
  65. else if(myDiv_6.className != 'contentdiv none')
  66. {
  67.  
  68. myDiv_1.className = 'contentdiv block';
  69. myDiv_2.className = 'contentdiv none';
  70. myDiv_3.className = 'contentdiv none';
  71. myDiv_4.className = 'contentdiv none';
  72. myDiv_5.className = 'contentdiv none';
  73. myDiv_6.className = 'contentdiv none';
  74.  
  75. }
  76.  
  77. setInterval("show_and_hide()", czas);


z tym, że jak myDiv_5 albo myDiv_6 nie istnieje to kończy prace... a jak zrobić, że od początku przesuwało?

Próbowałem też pętlą, ale nic mi nie wyszło...

  1. myDivs = new Array();
  2. myDiv = new Array();
  3. var a = 0;
  4. for(i=0; i<6; i++){
  5. if(document.getElementById('mains_' + i)){
  6. myDivs[i] = document.getElementById('mains_' + i);
  7. }
  8. }
  9.  
  10. for(i=0; i<myDivs.length; i++){
  11. if(typeof(myDivs[i]) != 'undefined' && typeof(myDivs[i]) != 'null'){
  12. if(myDivs[i].className=='contentdiv block'){
  13. myDivs[i].className = 'contentdiv none';
  14.  
  15. //alert(i);
  16. }else{
  17.  
  18.  
  19. myDiv[a] = myDivs[i];
  20. myDiv[a].className = 'contentdiv block';
  21.  
  22.  
  23. }
  24.  
  25. }
  26. }


Ten post edytował SN@JPER^ 16.10.2011, 14:23:03
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
zegarek84
post
Post #2





Grupa: Zarejestrowani
Postów: 1 332
Pomógł: 294
Dołączył: 12.10.2008
Skąd: Olkusz

Ostrzeżenie: (0%)
-----


Cytat(markonix @ 16.10.2011, 16:56:02 ) *
I na pewno łatwiej będzie tu skorzystać z jQuery (manipulacja DOM).

nie na pewno... a tym bardziej nie optymalniej... fakt, iż jeśli chcemy pobrać wszystkie div'y określonym prefixem to zamiast implementacji metody setElements można było skorzystać z selektorów CSS w jQuery lub z .querySelectorAll po atrybucie id zaczynającym się od danego prefix'u



może się nigdzie nie pomyliłem - masz gotowca napisanego z palca z przykładem użycia klasy:
[JAVASCRIPT] pobierz, plaintext
  1. function ShowHide(){
  2. this._aEl = [];
  3. }
  4.  
  5. ShowHide.prototype = {
  6. _aEl: null,
  7. _sHideClass: 'contentdiv none',
  8. _sShowClass: 'contentdiv block',
  9. _i:0,
  10. _interval:null,
  11. setElements: function(sPrefix,iBegin, iEnd){
  12. var el, ar = this._aEl;
  13. for(var i = iBegin; i<=iEnd; ++i){
  14. el = document.getElementById(sPrefix+i);
  15. if(!!el)
  16. ar[ar.length] = el;
  17. }
  18. el = ar = null;
  19. return this;
  20. },
  21. hideAll: function(){
  22. for(var s=this._sHideClass,i=0,a=this._aEl,iL = a.length; i<iL;++i)
  23. a[i].setAttribute('class', s);
  24. return this;
  25. },
  26. showFirst: function(){
  27. if(this._aEl[0])
  28. this._aEl[0].setAttribute('class', this._sShowClass);
  29. return this;
  30. },
  31. showNext: function(){
  32. var a = this._aEl;
  33. if(a.length){
  34. a[this._i].setAttribute('class', this._sHideClass);
  35. if(this._i<(a.length-1)){
  36. ++this._i;
  37. }else{
  38. this._i=0;
  39. }
  40. a[this._i].setAttribute('class', this._sShowClass);
  41. }
  42. a= null;
  43. return this;
  44. },
  45. startInterval:function(i){
  46. if(!i) i = 1000;
  47. this._interval = setInterval((function(ob){
  48. return function(){
  49. ob.showNext();
  50. }
  51. })(this),i);
  52. return this;
  53. },
  54. stopInterval:function(){
  55. clearInterval(this._interval);
  56. return this;
  57. }
  58. };
  59.  
  60. // PRZYKŁADOWE UŻYCIE
  61.  
  62. var oSH = new ShowHide();
  63. oSH
  64. .setElements('mains_', 0, 6)
  65. .hideAll()
  66. .showFirst()
  67. .startInterval(500);
[JAVASCRIPT] pobierz, plaintext


Ten post edytował zegarek84 16.10.2011, 19:22:44
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 8.10.2025 - 04:52