前提・実現したいこと
jsで画像を使用したカウントダウンタイマーを作成しています。
ブラウザの横幅によって表示サイズを変更するためレスポンシブで作成し、PCでは問題なく動作、表示切り替えできるのですが、スマートフォン時にうまくいきません。
表示自体はできているのですが、読み込み直後はスクロールさせないとタイマーが動作しません。読み込み後スクロールしなくても動くようにしたしです。
該当のソースコード
下記サイトを参考にさせていただいています。
http://cly7796.net/wp/javascript/countdown-image-timer/
JavaScript
1$(function(){ 2 $(window).on('load resize', function() { 3 4 var windowWidth = window.innerWidth; 5 6 if (windowWidth > 768) { 7 if (window.name != "pc"){ 8 location.reload(); 9 window.name = "pc"; 10 } 11 // ********** PCの記述 ********** // 12 var gYear = 2020, // 年 13 gMonth = 9, // 月 14 gDay = 9, // 日 15 gHour = 0, // 時 16 gMinute = 0, // 分 17 gSecond = 0; // 秒 18 19 var goal = new Date(gYear, gMonth-1, gDay, gHour, gMinute, gSecond); 20 21 var numHeight = 118; 22 23 var currentTime, period, 24 cDay, cDayHundred, cDayTen, cDayOne, 25 cHour, cHourTen, cHourOne, 26 cMinute, cMinuteTen, cMinuteOne, 27 cSecond, cSecondTen, cSecondOne, 28 cFlame, 29 insert = ""; 30 31 var dayTen = document.getElementById('day-ten'), 32 dayOne = document.getElementById('day-one'), 33 hourTen = document.getElementById('hour-ten'), 34 hourOne = document.getElementById('hour-one'), 35 minuteTen = document.getElementById('minute-ten'), 36 minuteOne = document.getElementById('minute-one'), 37 secondTen = document.getElementById('second-ten'), 38 secondOne = document.getElementById('second-one'); 39 40 function countdown() { 41 42 currentTime = new Date(); 43 period = goal.getTime() - currentTime.getTime(); 44 45 if(period >= 0) { 46 cDay = Math.floor(period / (1000 * 60 * 60 * 24)); 47 period -= (cDay * (1000 * 60 * 60 * 24)); 48 cDay = String(cDay); 49 while(cDay.length < 4) { 50 cDay = '0' + cDay; 51 } 52 dayTen.style.top = (- cDay.substr(2, 1) * numHeight) + 'px'; 53 dayOne.style.top = (- cDay.substr(3, 1) * numHeight) + 'px'; 54 55 cHour = Math.floor(period / (1000 * 60 * 60)); 56 period -= (cHour * (1000 * 60 * 60)); 57 cHour = String(cHour); 58 while(cHour.length < 2) { 59 cHour = '0' + cHour; 60 } 61 hourTen.style.top = (- cHour.substr(0, 1) * numHeight) + 'px'; 62 hourOne.style.top = (- cHour.substr(1, 1) * numHeight) + 'px'; 63 64 cMinute = Math.floor(period / (1000 * 60)); 65 period -= (cMinute * (1000 * 60)); 66 cMinute = String(cMinute); 67 while(cMinute.length < 2) { 68 cMinute = '0' + cMinute; 69 } 70 minuteTen.style.top = (- cMinute.substr(0, 1) * numHeight) + 'px'; 71 minuteOne.style.top = (- cMinute.substr(1, 1) * numHeight) + 'px'; 72 73 cSecond = Math.floor(period / (1000)); 74 period -= (cSecond * (1000)); 75 cSecond = String(cSecond); 76 while(cSecond.length < 2) { 77 cSecond = '0' + cSecond; 78 } 79 secondTen.style.top = (- cSecond.substr(0, 1) * numHeight) + 'px'; 80 secondOne.style.top = (- cSecond.substr(1, 1) * numHeight) + 'px'; 81 82 setTimeout(countdown, 10); 83 84 } else { 85 } 86 } 87 countdown(); 88 } else { 89 if (window.name != "sp"){ 90 location.reload(); 91 window.name = "sp"; 92 } 93 // ********** SPの記述 ********** // 94 var gYear = 2020, // 年 95 gMonth = 9, // 月 96 gDay = 9, // 日 97 gHour = 0, // 時 98 gMinute = 0, // 分 99 gSecond = 0; // 秒 100 101 var goal = new Date(gYear, gMonth-1, gDay, gHour, gMinute, gSecond); 102 103 var numHeight = 11.33333; 104 105 var currentTime, period, 106 cDay, cDayHundred, cDayTen, cDayOne, 107 cHour, cHourTen, cHourOne, 108 cMinute, cMinuteTen, cMinuteOne, 109 cSecond, cSecondTen, cSecondOne, 110 cFlame, 111 insert = ""; 112 113 var dayTen = document.getElementById('day-ten'), 114 dayOne = document.getElementById('day-one'), 115 hourTen = document.getElementById('hour-ten'), 116 hourOne = document.getElementById('hour-one'), 117 minuteTen = document.getElementById('minute-ten'), 118 minuteOne = document.getElementById('minute-one'), 119 secondTen = document.getElementById('second-ten'), 120 secondOne = document.getElementById('second-one'); 121 122 function countdown() { 123 124 currentTime = new Date(); 125 period = goal.getTime() - currentTime.getTime(); 126 127 if(period >= 0) { 128 cDay = Math.floor(period / (1000 * 60 * 60 * 24)); 129 period -= (cDay * (1000 * 60 * 60 * 24)); 130 cDay = String(cDay); 131 while(cDay.length < 4) { 132 cDay = '0' + cDay; 133 } 134 dayTen.style.top = (- cDay.substr(2, 1) * numHeight) + 'vw'; 135 dayOne.style.top = (- cDay.substr(3, 1) * numHeight) + 'vw'; 136 137 cHour = Math.floor(period / (1000 * 60 * 60)); 138 period -= (cHour * (1000 * 60 * 60)); 139 cHour = String(cHour); 140 while(cHour.length < 2) { 141 cHour = '0' + cHour; 142 } 143 hourTen.style.top = (- cHour.substr(0, 1) * numHeight) + 'vw'; 144 hourOne.style.top = (- cHour.substr(1, 1) * numHeight) + 'vw'; 145 146 cMinute = Math.floor(period / (1000 * 60)); 147 period -= (cMinute * (1000 * 60)); 148 cMinute = String(cMinute); 149 while(cMinute.length < 2) { 150 cMinute = '0' + cMinute; 151 } 152 minuteTen.style.top = (- cMinute.substr(0, 1) * numHeight) + 'vw'; 153 minuteOne.style.top = (- cMinute.substr(1, 1) * numHeight) + 'vw'; 154 155 cSecond = Math.floor(period / (1000)); 156 period -= (cSecond * (1000)); 157 cSecond = String(cSecond); 158 while(cSecond.length < 2) { 159 cSecond = '0' + cSecond; 160 } 161 secondTen.style.top = (- cSecond.substr(0, 1) * numHeight) + 'vw'; 162 secondOne.style.top = (- cSecond.substr(1, 1) * numHeight) + 'vw'; 163 164 setTimeout(countdown, 10); 165 166 } else { 167 } 168 } 169 countdown(); 170 } 171 }); 172 }); 173
html
1<div id="countdown-unit"> 2 <div><img src="images/number.png" id="day-ten" alt=""></div> 3 <div><img src="images/number.png" id="day-one" alt=""></div> 4 <div><img src="images/time.png" alt=""></div> 5 <div><img src="images/number.png" id="hour-ten" alt=""></div> 6 <div><img src="images/number.png" id="hour-one" alt=""></div> 7 <div><img src="images/time.png" alt=""></div> 8 <div><img src="images/number.png" id="minute-ten" alt=""></div> 9 <div><img src="images/number.png" id="minute-one" alt=""></div> 10 <div><img src="images/time.png" alt=""></div> 11 <div><img src="images/number.png" id="second-ten" alt=""></div> 12 <div><img src="images/number.png" id="second-one" alt=""></div> 13</div>
試したこと
jsの切り替えについて「window.matchMedia」などいくつか試しましたができませんでした。
「resize」の動作の部分に問題があるのでしょうか。
あなたの回答
tips
プレビュー