example-singleServer-progressBar.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no, user-scalable=no" />
  6. <title>HTML5 Speedtest</title>
  7. <script type="text/javascript" src="speedtest.js"></script>
  8. <script type="text/javascript">
  9. //INITIALIZE SPEEDTEST
  10. var s=new Speedtest(); //create speedtest object
  11. s.onupdate=function(data){ //callback to update data in UI
  12. I("ip").textContent=data.clientIp;
  13. I("dlText").textContent=(data.testState==1&&data.dlStatus==0)?"...":data.dlStatus;
  14. I("ulText").textContent=(data.testState==3&&data.ulStatus==0)?"...":data.ulStatus;
  15. I("pingText").textContent=data.pingStatus;
  16. I("jitText").textContent=data.jitterStatus;
  17. var prog=(Number(data.dlProgress)*2+Number(data.ulProgress)*2+Number(data.pingProgress))/5;
  18. I("progress").style.width=(100*prog)+"%";
  19. }
  20. s.onend=function(aborted){ //callback for test ended/aborted
  21. I("startStopBtn").className=""; //show start button again
  22. if(aborted){ //if the test was aborted, clear the UI and prepare for new test
  23. initUI();
  24. }
  25. }
  26. function startStop(){ //start/stop button pressed
  27. if(s.getState()==3){
  28. //speedtest is running, abort
  29. s.abort();
  30. }else{
  31. //test is not running, begin
  32. s.start();
  33. I("startStopBtn").className="running";
  34. }
  35. }
  36. //function to (re)initialize UI
  37. function initUI(){
  38. I("dlText").textContent="";
  39. I("ulText").textContent="";
  40. I("pingText").textContent="";
  41. I("jitText").textContent="";
  42. I("ip").textContent="";
  43. }
  44. function I(id){return document.getElementById(id);}
  45. </script>
  46. <style type="text/css">
  47. html,body{
  48. border:none; padding:0; margin:0;
  49. background:#FFFFFF;
  50. color:#202020;
  51. }
  52. body{
  53. text-align:center;
  54. font-family:"Roboto",sans-serif;
  55. }
  56. h1{
  57. color:#404040;
  58. }
  59. #startStopBtn{
  60. display:inline-block;
  61. margin:0 auto;
  62. color:#6060AA;
  63. background-color:rgba(0,0,0,0);
  64. border:0.15em solid #6060FF;
  65. border-radius:0.3em;
  66. transition:all 0.3s;
  67. box-sizing:border-box;
  68. width:8em; height:3em;
  69. line-height:2.7em;
  70. cursor:pointer;
  71. box-shadow: 0 0 0 rgba(0,0,0,0.1), inset 0 0 0 rgba(0,0,0,0.1);
  72. }
  73. #startStopBtn:hover{
  74. box-shadow: 0 0 2em rgba(0,0,0,0.1), inset 0 0 1em rgba(0,0,0,0.1);
  75. }
  76. #startStopBtn.running{
  77. background-color:#FF3030;
  78. border-color:#FF6060;
  79. color:#FFFFFF;
  80. }
  81. #startStopBtn:before{
  82. content:"Start";
  83. }
  84. #startStopBtn.running:before{
  85. content:"Abort";
  86. }
  87. #test{
  88. margin-top:2em;
  89. margin-bottom:12em;
  90. }
  91. div.testArea{
  92. display:inline-block;
  93. width:14em;
  94. height:9em;
  95. position:relative;
  96. box-sizing:border-box;
  97. }
  98. div.testName{
  99. position:absolute;
  100. top:0.1em; left:0;
  101. width:100%;
  102. font-size:1.4em;
  103. z-index:9;
  104. }
  105. div.meterText{
  106. position:absolute;
  107. bottom:1.5em; left:0;
  108. width:100%;
  109. font-size:2.5em;
  110. z-index:9;
  111. }
  112. #dlText{
  113. color:#6060AA;
  114. }
  115. #ulText{
  116. color:#309030;
  117. }
  118. #pingText,#jitText{
  119. color:#AA6060;
  120. }
  121. div.meterText:empty:before{
  122. color:#505050 !important;
  123. content:"0.00";
  124. }
  125. div.unit{
  126. position:absolute;
  127. bottom:2em; left:0;
  128. width:100%;
  129. z-index:9;
  130. }
  131. div.testGroup{
  132. display:inline-block;
  133. }
  134. @media all and (max-width:65em){
  135. body{
  136. font-size:1.5vw;
  137. }
  138. }
  139. @media all and (max-width:40em){
  140. body{
  141. font-size:0.8em;
  142. }
  143. div.testGroup{
  144. display:block;
  145. margin: 0 auto;
  146. }
  147. }
  148. #progressBar{
  149. width:90%;
  150. height:0.3em;
  151. background-color:#EEEEEE;
  152. position:relative;
  153. display:block;
  154. margin:0 auto;
  155. margin-bottom:2em;
  156. }
  157. #progress{
  158. position:absolute;
  159. top:0; left:0;
  160. height:100%;
  161. width:0%;
  162. transition: width 2s;
  163. background-color:#90BBFF;
  164. }
  165. </style>
  166. </head>
  167. <body>
  168. <h1>HTML5 Speedtest</h1>
  169. <div id="startStopBtn" onclick="startStop()"></div>
  170. <div id="test">
  171. <div id="progressBar"><div id="progress"></div></div>
  172. <div class="testGroup">
  173. <div class="testArea">
  174. <div class="testName">Download</div>
  175. <div id="dlText" class="meterText"></div>
  176. <div class="unit">Mbps</div>
  177. </div>
  178. <div class="testArea">
  179. <div class="testName">Upload</div>
  180. <div id="ulText" class="meterText"></div>
  181. <div class="unit">Mbps</div>
  182. </div>
  183. </div>
  184. <div class="testGroup">
  185. <div class="testArea">
  186. <div class="testName">Ping</div>
  187. <div id="pingText" class="meterText"></div>
  188. <div class="unit">ms</div>
  189. </div>
  190. <div class="testArea">
  191. <div class="testName">Jitter</div>
  192. <div id="jitText" class="meterText"></div>
  193. <div class="unit">ms</div>
  194. </div>
  195. </div>
  196. <div id="ipArea">
  197. IP Address: <span id="ip"></span>
  198. </div>
  199. </div>
  200. <a href="https://github.com/adolfintel/speedtest">Source code</a>
  201. <script type="text/javascript">
  202. initUI();
  203. </script>
  204. </body>
  205. </html>