実現したいこと
アコーディオンメニューをクリックして開閉した際、右端につけたアイコンを右回りに半回転したいです。
発生している問題・分からないこと
上記のような内容は参考サイトやほかの皆様のご助力もあって大方完成しましたが、最初にクリックをしていないのに勝手に半周してしまうのを止めるべく、scriptを付けましたが、動かなくなってしまいました。
勝手に半周してしまうのを止めるにはここからどうすればよろしいでしょうか?
よろしくお願いいたします。
該当のソースコード
html、css、JavaScript
1<meta name="robots" content="noindex,nofollow" /> 2<!doctype html> 3<html lang="ja"> 4 5<head> 6 <meta charset="UTF-8" /> 7 <meta name="viewport" content="width=device-width, initial-scale=1" /> 8 <link rel="stylesheet" href="https://plot-hub.com/code_example/lib/css/reset.css"> 9 10<style> 11/*カテゴリーボタン*/ 12.button02 { 13 margin-left: 8rem; 14/*transform: rotate(90deg);*/ 15} 16.button_animation{ 17 transform: rotate(90deg); 18} 19.button02 { 20 /* スタイル用 */ 21 display: block; 22 width: 30px; 23 height: 30px; 24 border-radius: 50%; 25 border: 1px solid #00a0e9; 26 /* スタイル用 */ 27 28 position: relative; 29} 30 31.button02:before, 32.button02:after { 33 position: absolute; 34 content: ''; 35} 36 37.button02:before { 38 top: 14px; 39 left: 6px; 40 width: 10px; 41 height: 1px; 42 background: #00a0e9; 43} 44.button02:after { 45 top: 11px; 46 left: 16px; 47 width: 0; 48 height: 0; 49 border-top: 4px solid transparent; 50 border-right: 0 solid transparent; 51 border-bottom: 0 solid transparent; 52 border-left: 5px solid #00a0e9; 53} 54.acd-check{ 55 display: none; 56} 57.acd-label{ 58 background: #e5f5fd; 59 margin-bottom: 1px; 60 padding: 10px; 61 display: flex; 62 justify-content: space-between; 63} 64.acd-content{ 65 display: block; 66 height: 0; 67 opacity: 0; 68 padding: 0 10px; 69 transition: .5s; 70 visibility: hidden; 71} 72.acd-check:checked + .acd-label .button_animation .clicked { 73 animation: rotateAnimation .5s ease-out forwards; 74} 75.acd-check + .acd-label .button_animation .clicked { 76 animation: rotateAnimation2 .5s ease-in-out forwards; 77} 78@keyframes rotateAnimation { 79 from { 80 transform: rotate(90deg); 81 } 82 to { 83 transform: rotate(270deg); 84 } 85} 86@keyframes rotateAnimation2 { 87 from { 88 transform: rotate(-90deg); 89 } 90 to { 91 transform: rotate(90deg); 92 } 93} 94 95.acd-check:checked + .acd-label + .acd-content{ 96 height: 50px; 97 opacity: 1; 98 padding: 10px; 99 visibility: visible; 100} 101</style> 102 103</head> 104 105<body> 106<input id="acd-check1" class="acd-check" type="checkbox"> 107<label class="acd-label t-font25" for="acd-check1">クリックで開く1<i class="button02 button_animation"></i></label> 108<div class="acd-content"><p>hello.world!</p></div> 109 110<input id="acd-check2" class="acd-check" type="checkbox"> 111<label class="acd-label" for="acd-check2">クリックで開く2<i class="button02 button_animation"></i></label> 112<div class="acd-content"><p>hello.world2!</p></div> 113 114<input id="acd-check3" class="acd-check" type="checkbox"> 115<label class="acd-label" for="acd-check3">クリックで開く3</label> 116<div class="acd-content"><p>hello.world3!</p></div> 117 118<input id="acd-check4" class="acd-check" type="checkbox"> 119<label class="acd-label" for="acd-check4">クリックで開く4</label> 120<div class="acd-content"><p>hello.world4!</p></div> 121 122 123<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> 124 125<script> 126const t = document.querySelector(".acd-check"); 127const a = document.querySelector(".button_animation"); 128t.addEventListener("click", (event) => { 129 a.classList.add("clicked"); 130}); 131</script> 132 133</body> 134</html> 135
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
前回質問をして、いただいたコードを参考に自分なりにアレンジをしてみましたが、上手くいきませんでした。
お教え頂ければ幸いです。
追記しました。
html、css、JavaScript
1<meta name="robots" content="noindex,nofollow" /> 2<!doctype html> 3<html lang="ja"> 4 5<head> 6 <meta charset="UTF-8" /> 7 <meta name="viewport" content="width=device-width, initial-scale=1" /> 8 <link rel="stylesheet" href="https://plot-hub.com/code_example/lib/css/reset.css"> 9 10<style> 11/*カテゴリーボタン*/ 12.button02 { 13 margin-left: 8rem; 14/*transform: rotate(90deg);*/ 15} 16.button_animation{ 17 transform: rotate(90deg); 18} 19.button02 { 20 /* スタイル用 */ 21 display: block; 22 width: 30px; 23 height: 30px; 24 border-radius: 50%; 25 border: 1px solid #00a0e9; 26 /* スタイル用 */ 27 28 position: relative; 29} 30 31.button02:before, 32.button02:after { 33 position: absolute; 34 content: ''; 35} 36 37.button02:before { 38 top: 14px; 39 left: 6px; 40 width: 10px; 41 height: 1px; 42 background: #00a0e9; 43} 44.button02:after { 45 top: 11px; 46 left: 16px; 47 width: 0; 48 height: 0; 49 border-top: 4px solid transparent; 50 border-right: 0 solid transparent; 51 border-bottom: 0 solid transparent; 52 border-left: 5px solid #00a0e9; 53} 54.acd-check{ 55 display: none; 56} 57 58.acd-label{ 59 background: #e5f5fd; 60 margin-bottom: 1px; 61 padding: 10px; 62 display: flex; 63 justify-content: space-between; 64} 65.acd-content{ 66 display: block; 67 height: 0; 68 opacity: 0; 69 padding: 0 10px; 70 transition: .5s; 71 visibility: hidden; 72} 73 74.acd-check:checked + .acd-label + .acd-content{ 75 height: 50px; 76 opacity: 1; 77 padding: 10px; 78 visibility: visible; 79} 80.click1{ transform: rotate(270deg);transition: .5s;} 81.click2{ transform: rotate(450deg);transition: .5s;} 82</style> 83 84</head> 85 86<body> 87<input id="acd-check1" class="acd-check" type="checkbox"> 88<label class="acd-label t-font25" for="acd-check1">クリックで開く1<i class="button02 button_animation"></i></label> 89<div class="acd-content"><p>hello.world!</p></div> 90 91<input id="acd-check2" class="acd-check" type="checkbox"> 92<label class="acd-label" for="acd-check2">クリックで開く2<i class="button02 button_animation"></i></label> 93<div class="acd-content"><p>hello.world2!</p></div> 94 95<input id="acd-check3" class="acd-check" type="checkbox"> 96<label class="acd-label" for="acd-check3">クリックで開く3</label> 97<div class="acd-content"><p>hello.world3!</p></div> 98 99<input id="acd-check4" class="acd-check" type="checkbox"> 100<label class="acd-label" for="acd-check4">クリックで開く4</label> 101<div class="acd-content"><p>hello.world4!</p></div> 102 103 104<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> 105 106<script> 107const a = document.querySelector(".button_animation"); 108document.addEventListener('click',e=>{ 109 const t=e.target; 110 if(t.matches('.button_animation:not(.running)')){ 111 t.classList.toggle('click1'); 112 t.classList.toggle('click2',!t.classList.contains('click1')); 113 } 114}); 115addEventListener("transitionstart", e=>{ 116 const t=e.target; 117 t.classList.add('running'); 118}); 119document.addEventListener("transitionend",e=> { 120 const t=e.target; 121 t.classList.remove('running'); 122 t.classList.remove('click2'); 123}); 124</script> 125 126</body> 127</html>

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