index.html 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <!-- Source code available under GPLv3 here: https://notabug.org/adnan360/bmi-calc-html -->
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  7. <title>BMI Calculator</title>
  8. <script src="assets/js/jquery.min.js" type="text/javascript"></script>
  9. <style>
  10. * {
  11. padding:0;
  12. margin:0;
  13. font-family:sans-serif;
  14. }
  15. h1 {
  16. text-align:center;
  17. font-weight: 400;
  18. margin: 10px 0;
  19. }
  20. p {
  21. margin:5px 0;
  22. line-height: 2em;
  23. }
  24. label {
  25. width:120px;
  26. display: inline-block;
  27. }
  28. input[type=text] {
  29. width: 100px;
  30. border-radius:3px;
  31. border:1px solid #aaa;
  32. padding:5px 7px;
  33. }
  34. select {
  35. border-radius: 3px;
  36. padding: 4px;
  37. border: 1px solid #ccc;
  38. }
  39. .wrap {
  40. width: 600px;
  41. margin: 0 auto;
  42. }
  43. form.bmi, input[type=text], input[type=submit], select {
  44. font-size:28px;
  45. }
  46. input[type=submit] {
  47. border-radius: 3px;
  48. padding:5px 25px;
  49. border:1px solid #aaa;
  50. margin:0 auto;
  51. display: inherit;
  52. }
  53. .result {
  54. margin:10px auto;
  55. padding: 8px 10px;
  56. border-radius: 5px;
  57. border: 1px solid #ddd;
  58. background: #feffdd;
  59. text-align: center;
  60. }
  61. .result.error {
  62. background:#fbccaf;
  63. }
  64. .result .message {
  65. display:none;
  66. }
  67. .result .result-body {
  68. display:unset;
  69. }
  70. .result .bmi {
  71. border: 4px solid #f00;
  72. border-radius: 50%;
  73. width: 80px;
  74. height: 80px;
  75. text-align: center;
  76. vertical-align: middle;
  77. display: table-cell;
  78. font-size: 30px;
  79. }
  80. .result.error .message {
  81. display:unset;
  82. }
  83. .result.error .result-body, .legend-content {
  84. display:none;
  85. }
  86. .legend li {
  87. margin-left: 20px;
  88. }
  89. .legend-toggle {
  90. text-align:center;
  91. width: 100%;
  92. display: inline-block;
  93. }
  94. select {
  95. -moz-appearance: none;
  96. -webkit-appearance:none;
  97. appearance: none;
  98. padding: 4px 25px 4px 8px;
  99. background:#eee;
  100. }
  101. .select-wrapper {
  102. position:relative;
  103. display: inline-block;
  104. }
  105. .select-arrow {
  106. right: 8px;
  107. top: -1px;
  108. position: absolute;
  109. pointer-events: none;
  110. font-size: 15px;
  111. }
  112. /* --- RESPONSIVE --- */
  113. /* --- Little smaller than 1024 --- */
  114. @media only screen and (max-width: 900px) {
  115. }
  116. /* --- Tablet --- */
  117. @media only screen and (max-width: 768px) {
  118. label {
  119. display:block;
  120. }
  121. .wrap {
  122. width: unset;
  123. padding:0 20px;
  124. }
  125. }
  126. /* --- Mobile Phone --- */
  127. @media only screen and (max-width: 480px) {
  128. select, .select-wrapper {
  129. display:block;
  130. width: 100%;
  131. }
  132. input[type="text"] {
  133. width:80%;
  134. }
  135. #weight1 {
  136. width: -moz-available;
  137. width: -webkit-fill-available;
  138. }
  139. }
  140. </style>
  141. </head>
  142. <body>
  143. <div class="wrap">
  144. <h1>BMI Calculator</h1>
  145. <form class="bmi" method="post">
  146. <p>
  147. <label for="height1">Height:</label>
  148. <input type="text" id="height1" name="height1" value="" />
  149. <span id="height1_label">Ft</span>
  150. <span id="height2_container">
  151. <input type="text" id="height2" name="height2" value="" />
  152. <span id="height2_label">In</span>
  153. </span>
  154. <select name="height_measurement" id="height_measurement">
  155. <option value="ftin" data-label1="Ft" data-label2="In">Ft+In</option>
  156. <option value="cm" data-label1="Cm" data-label2="">Cm</option>
  157. <option value="in" data-label1="In" data-label2="">In</option>
  158. </select>
  159. </p>
  160. <p><label for="weight1">Weight:</label> <input type="text" name="weight1" id="weight1" value="" />
  161. <select name="weight_measurement" id="weight_measurement">
  162. <option value="kg">Kg</option>
  163. <option value="lbs">lbs</option>
  164. </select>
  165. </p>
  166. <p><input type="submit" name="submit" value="Calculate!" /></p>
  167. </form>
  168. <div class="result" style="display:none">
  169. <div class="result-body">
  170. <p>BMI:</p> <div class="bmi"></div>
  171. <p>This means you are: <span class="bmi-meaning"></span></p>
  172. </div>
  173. <p class="message"></p>
  174. </div>
  175. <div class="legend">
  176. <a class="legend-toggle" href="javascript:void(0);">BMI Categories</a>
  177. <div class="legend-content">
  178. <h4>BMI Categories</h4>
  179. <ul>
  180. <li>Underweight = less than 18.5</li>
  181. <li>Normal weight (healthy) = 18.5–24.9</li>
  182. <li>Overweight = 25–29.9</li>
  183. <li>Obese = BMI of 30 or greater</li>
  184. </ul>
  185. <p><a href="https://en.wikipedia.org/wiki/Body_mass_index#Categories">(The categories have been simplified from here)</a></p>
  186. </div>
  187. </div>
  188. <script>
  189. // Select arrow styling
  190. $('select').wrap( '<span class="select-wrapper"></span>' ).after( '<span class="select-arrow">▼</span>' );
  191. $('#height_measurement').change(function(){
  192. var label1 = $( "#height_measurement option:selected" ).attr('data-label1');
  193. var label2 = $( "#height_measurement option:selected" ).attr('data-label2');
  194. $('#height2_container').css('display', (label2=='') ? 'none' : '' );
  195. $('#height1_label').html(label1);
  196. $('#height2_label').html(label2);
  197. });
  198. $('form.bmi').submit(function(e){
  199. e.preventDefault();
  200. var measure1 = $( "#height_measurement option:selected" ).val();
  201. var measure2 = $( "#weight_measurement option:selected" ).val();
  202. var cm = convertMeasurement($('#height1').val(), $('#height2').val(), measure1);
  203. var kg = convertMeasurement($('#weight1').val(), 0, measure2);
  204. var bmi = calculateBMI(cm, kg);
  205. var bmiMeaning = getBMIMeaning(bmi);
  206. if ( isNaN(bmi) == true || isFinite(bmi) == false ) {
  207. $('.result').addClass('error').find('.message').html( 'Error occured. Please check inputs. Enter only numbers and not any characters.' );
  208. } else {
  209. $('.result').removeClass('error');
  210. $('.result .bmi').html( bmi );
  211. $('.result .bmi-meaning').html( bmiMeaning );
  212. }
  213. $('.result').show();
  214. });
  215. $('.legend-toggle').click(function(){
  216. $('.legend-content').toggle();
  217. });
  218. // type can be: ftin, cm, in, kg, lbs
  219. // converts to cm for lengths, kg for masses
  220. function convertMeasurement(input1, input2=0, input_type) {
  221. var lengths = ['ftin', 'cm', 'in'];
  222. var masses = ['kg', 'lbs'];
  223. var output;
  224. input1 = parseFloat(input1);
  225. input2 = parseFloat(input2);
  226. if (lengths.includes(input_type)==true) { // length conversions
  227. if (input_type=='ftin') {
  228. output = ( parseFloat(input1 * 12) + parseFloat(input2) ) * 2.54;
  229. } else if (input_type=='cm') {
  230. output = input1;
  231. } else if (input_type=='in') {
  232. output = input1 * 2.54;
  233. }
  234. } else if (masses.includes(input_type)==true) { // masses conversion
  235. if (input_type=='kg') {
  236. output = input1;
  237. } else if (input_type=='lbs') {
  238. output = input1 * 0.45359;
  239. }
  240. } else {
  241. // TODO: raise error
  242. }
  243. return output;
  244. }
  245. // Source: https://www.codentricks.com/make-bmi-calculator-using-pure-javascript/
  246. function calculateBMI(cm, kg){
  247. var bmi;
  248. var newCm= parseFloat(cm/100);
  249. bmi = kg / (newCm * newCm);
  250. bmi = bmi.toFixed(1);
  251. return bmi;
  252. }
  253. function getBMIMeaning(bmi) {
  254. if ( isNaN(bmi) == true || isFinite(bmi) == false ) {
  255. bmiMeaning = '';
  256. } else if (bmi < 18.5) {
  257. bmiMeaning = 'Underweight';
  258. } else if (bmi >= 18.5 && bmi <= 24.9) {
  259. bmiMeaning = 'Normal weight (healthy)';
  260. } else if (bmi >= 25 && bmi <= 29.9) {
  261. bmiMeaning = 'Overweight';
  262. } else if (bmi >= 30) {
  263. bmiMeaning = 'Obese';
  264. } else {
  265. bmiMeaning = '';
  266. }
  267. return bmiMeaning;
  268. }
  269. </script>
  270. </div>
  271. </body>
  272. </html>