前提
vscodeで下記のようなページを作っています。ボタンをクリックしたらFetch APIでjsonファイルを読み込み,
テキストが出現するはずなのですが、(アニメーションも失敗していますが)なぜか二回クリックしないとテキストが反映されません。理由が知りたいです。
よろしくお願いします。
該当のソースコード
html
1<!DOCTYPE html> 2<html lang="ja"> 3<head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width,initial-scale=1"> 6 <title></title> 7 <link rel="stylesheet" type="text/css" href="style.css"> 8 <script src="data.js" type="text/javascript"></script></script> 9</head> 10<body> 11 <div class="main"> 12 <h1 id="words">メッセージ</h1> 13 <p class="name" id="name"></p> 14 <p class="text-button" id="text-button">ボタン</p> 15 16 <script src="function.js" type="text/javascript"></script> 17 18 </div> 19</body>
data.js
1let list = []; 2let nameValue = "" 3 4async function testData() { 5 const res = await fetch("省略"); 6 const data = await res.json(); 7 nameValue = data[0][1]; 8 for (i = 1; i <data.length; i++){ 9 list.push(data[i][i+1]); 10 } 11 return list; 12}
function.js
1const textBtn =document.getElementById("text-button"); 2let lists; 3 4 5(async function() { 6 lists = await testData(); 7 8 const clicked = () => { 9 let selecter = parseInt(Math.random()*((lists.length)-1)+1); 10 let button = document.getElementById("text-button"); 11 let words = document.getElementById("words"); 12 let name = document.getElementById("name"); 13 14 new Promise((resolve, reject) => { 15 resolve(); 16 }) 17 18 .then(() => { 19 console.log(selecter); 20 console.log(lists[selecter]); 21 console.log(button.getAnimations()[0]); 22 23 24 }) 25 .then(() => { 26 //省略 27 }; 28 textBtn.addEventListener("click",clicked); 29 })(); 30
style.css
1html { 2 cursor: default; 3} 4body { 5 background-image:url("sky01.png"); 6 7 8} 9 10.text-button{ 11 12 transition: all 1.2s; 13 14} 15.text-button:hover{ 16 backdrop-filter: sepia(60%); 17 border-radius:20px; 18 cursor: pointer; 19 width:130px; 20} 21.fadeIn{ 22 animation-name:fadeInAnime; 23 animation-duration:3s; 24 animation-fill-mode:forwards; 25 opacity:0; 26} 27.fadeIn2{ 28 animation-name:fadeInAnime2; 29 animation-duration:7s; 30 animation-fill-mode:forwards; 31 opacity:0; 32} 33 34@keyframes fadeInAnime{ 35 from{ 36 opacity:0; 37 } 38 to{ 39 opacity:1; 40 } 41} 42 43@keyframes fadeInAnime2{ 44 from { 45 opacity: 0; 46 } 47 to { 48 opacity: 1; 49 } 50}
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2024/04/24 03:14 編集
2024/04/24 03:17 編集
2024/04/24 03:53
2024/04/24 04:36
2024/04/24 05:12
2024/04/24 05:17
2024/04/24 06:32
2024/04/24 06:34
2024/04/24 06:57