初めて質問させていただきます。
実現したいこと
- 一定時間ごとに背景色(background)を変えたい
発生している問題・エラーメッセージ
setTimeoutを使い6秒毎にクラスを追加・削除して背景の色を替えることができましたが 最後のクラスまで行った後に、最初にもどる処理を実現できませんでした
該当のソースコード
html
1<div id="colorbox" class="color01"> 2 <p>テキスト</p> 3</div>
css
1#colorbox{ 2 width:100%; 3 height:300px; 4} 5 6.color01 { 7 background: linear-gradient(180deg, rgb(154, 174, 186), rgb(90, 129, 145)); 8} 9.color02 { 10 background: linear-gradient(180deg, rgb(239, 209, 184), rgb(224, 146, 149)); 11} 12.color03 { 13 background: linear-gradient(180deg, rgb(219, 196, 189), rgb(191, 146, 108)); 14} 15.color04 { 16 background: linear-gradient(180deg, rgb(227, 204, 159), rgb(213, 194, 90)); 17} 18.color05 { 19 background: linear-gradient(180deg, rgb(219, 189, 189), rgb(110, 136, 103)); 20} 21.color06 { 22 background: linear-gradient(180deg, rgb(154, 174, 186), rgb(90, 129, 145)); 23}
js
1$(function(){ 2 setTimeout(function(){ 3 $("#colorbox").toggleClass("color02"); 4 $("#colorbox").removeClass("color01"); 5 },6000); 6 setTimeout(function(){ 7 $("#colorbox").toggleClass("color03"); 8 $("#colorbox").removeClass("color02"); 9 },12000); 10 setTimeout(function(){ 11 $("#colorbox").toggleClass("color04"); 12 $("#colorbox").removeClass("color03"); 13 },18000); 14 setTimeout(function(){ 15 $("#colorbox").toggleClass("color05"); 16 $("#colorbox").removeClass("color04"); 17 },24000); 18 setTimeout(function(){ 19 $("#colorbox").toggleClass("color06"); 20 $("#colorbox").removeClass("color05"); 21 },30000); 22 setTimeout(function(){ 23 $("#colorbox").toggleClass("color01"); 24 $("#colorbox").removeClass("color06"); 25 },36000); 26 });
お手数ですが、ご教授いただけると助かります。

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2023/01/24 14:06