2016-11-18 4 views
0

私は、各反復後にカルーセル内の各アイテムを再び消滅させようとしています。ループは繰り返されますが、項目は消えません。コードはcodepenです。カルーセル効果 - 最初の反復後にアイテムを消す

ご協力いただければ幸いです。おかげで..

$(function() { 
 
    var $items = $('img','.container'); 
 
    var currentIdx = 0; 
 
    // var timer; 
 

 
    var cycleItems = function() { 
 
    console.log('Running from cycleItems'); 
 
    
 
    
 
    $items.each(function(index, item) { 
 
    var $self = $(item); 
 
    
 
    setTimeout(function() { 
 
     
 
     console.log(`We are at this item: ${item}`);   
 
     console.log('currentindex has value : ' + currentIdx); 
 
     console.log('And we are at this index: ' + index);   
 
     $self.removeClass('isHidden').addClass('isActive'); 
 
     currentIdx++ 
 

 
    }, 1000*index); /* End of setTimeOut */ 
 
       
 
    if (index == $items.length - 1) {  
 
    // item.removeClass('isActive').addClasss('isHidden'); 
 
     console.log('Items : '+ $items); 
 
     setTimeout(cycleItems, (index + 1) * 1000); 
 
    } 
 
     
 
    }) /* End of each */ 
 
    
 
    } /* End of cycleItems func */ 
 
    
 
    cycleItems(); 
 
    
 
});
html { 
 
    box-sizing: border-box; 
 
} 
 
*, 
 
*:before, 
 
*:after { 
 
    box-sizing: inherit; 
 
} 
 
body { 
 
    background: black; 
 
} 
 
.container { 
 
    display: inline; 
 
    //border: 1px solid white; 
 

 
} 
 
.slide {} .isActive { 
 
    visibility: visible; 
 
} 
 
.isHidden { 
 
    visibility: hidden; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class=container> 
 
    <img class='isActive' src="http://placehold.it/350x150"> 
 
    <img class='isHidden' src="http://placehold.it/350x150"> 
 
    <img class='isHidden' src="http://placehold.it/350x150"> 
 
    <img class='isHidden' src="http://placehold.it/350x150"> 
 
</div>

+0

ちょうどん '$( "IMG")addClass( "isHidden")'各反復の後に。 – Palo

+0

@パロありがとう、thartが問題を解決しました。しかし、なぜ私は '$ self.addClass( 'isHidden')'をしないのですか?再度 '( 'img')を指定する必要はありませんか? – intercoder

答えて

2

パロはたったの$( "IMG")を追加し、コメントで言ったように。addClass( "isHidden")。

コード:

$(function() { 
    var $items = $('img','.container'); 
    var currentIdx = 0; 
    // var timer; 

    var cycleItems = function() { 
    console.log('Running from cycleItems'); 


    $items.each(function(index, item) { 
    var $self = $(item); 

    setTimeout(function() { 

     console.log(`We are at this item: ${item}`);   
     console.log('currentindex has value : ' + currentIdx); 
     console.log('And we are at this index: ' + index);  

     $("img").addClass("isHidden") /* inputted piece of code */ 

     $self.removeClass('isHidden').addClass('isActive'); 

     currentIdx++ 

    }, 1000*index); /* End of setTimeOut */ 

    if (index == $items.length - 1) {  
    // item.removeClass('isActive').addClasss('isHidden'); 
     console.log('Items : '+ $items); 
     setTimeout(cycleItems, (index + 1) * 1000); 
    } 

    }) /* End of each */ 

    } /* End of cycleItems func */ 

    cycleItems(); 
    //clearTimeout(timer); 

    /*var toggleInvisible = function() { 

    }*/ 
}); 
関連する問題