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
SN@JPER^
post
Post #2





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

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


Zrobilem tak jak mówiles - dodalem do prototypu, usunałem var. a w li zrobiłem tak:

Kod
<li><a id="mainss_<?php echo $i;?>" onclick="oSH.showAc();"><?php echo $i;?></a></li>



JS

Kod
function ShowHide(){
    this._aEl = [];
}

ShowHide.prototype = {
    _aEl: null,
    _sHideClass: 'contentdiv none',
    _sShowClass: 'contentdiv block',
    _i:0,
    _interval:null,
    setElements: function(sPrefix,iBegin, iEnd){
        var el, ar = this._aEl;
        for(var i = iBegin; i<=iEnd; ++i){
            el = document.getElementById(sPrefix+i);
            if(!!el)
                ar[ar.length] = el;
        }
        el = ar = null;
        return this;
    },
    hideAll: function(){
        for(var s=this._sHideClass,i=0,a=this._aEl,iL = a.length; i<iL;++i)
            a[i].setAttribute('class', s);
        return this;
    },
    showFirst: function(){
        if(this._aEl[0])
            this._aEl[0].setAttribute('class', this._sShowClass);
        return this;
    },
    showNext: function(){
        var a = this._aEl;
        if(a.length){
            a[this._i].setAttribute('class', this._sHideClass);
            if(this._i<(a.length-1)){
                ++this._i;
            }else{
                this._i=0;
            }
            a[this._i].setAttribute('class', this._sShowClass);
        }
        a= null;
        return this;
    },
    
    showAc: function(i){if(this._aEl[i-1]) {//index'y są od 0 a nie od jeden
this._aEl[i-1].setAttribute('class', this._sShowClass);
this._i = i;
}
// return this nie musi być - jak uważasz
return this;},
    
    startInterval:function(i){
        if(!i) i = 1000;
        this._interval = setInterval((function(ob){
            return function(){
                ob.showNext();
            }
        })(this),i);
        return this;
    },
    stopInterval:function(){
        clearInterval(this._interval);
        return this;
    }
};


onload=function() {
// PRZYKŁADOWE UŻYCIE
oSH = new ShowHide();
oSH
.setElements('mains_', 0, 6)
.hideAll()
.showFirst()
.startInterval(5000);


I nie działa, zadnego efektu. Konsola FF tez nic nie pokazuje.

Ten post edytował SN@JPER^ 19.10.2011, 16:43:03
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: 6.10.2025 - 19:44