vroom.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Calculator for converting Miles to Kilometers
  2. $(document).ready(function () {
  3. TweenLite.to(needle, 2, {rotation: -31, transformOrigin: "bottom right"});
  4. // select current content in input boxes on click
  5. $("input[type='text']").on("click", function () {
  6. $(this).select();
  7. });
  8. //clear kilometers value when miles is selected
  9. $("#miles").focus(function () {
  10. $("#kilometers").val('');
  11. });
  12. //clear miles value when kilometers is selected
  13. $("#kilometers").focus(function () {
  14. $("#miles").val('');
  15. });
  16. // convert miles to kilometers
  17. $('#miles').keyup(function () {
  18. var mi = $(this).val();
  19. var miNum = parseInt(mi) * 1.6093;
  20. //make sure kmNum is a number then output
  21. if ((mi <= 75) && !isNaN(miNum)) {
  22. var speedMi = miNum * 2 - 31;
  23. $('#numbers').css('text-align', 'center');
  24. $('#kilometers').val(miNum.toFixed(2));
  25. $('#numbers').html(miNum.toFixed(0));
  26. $('#mi-km').html('Kilometers');
  27. } else if (!isNaN(miNum)) {
  28. var speedMi = 215;
  29. $('#numbers').css('text-align', 'right');
  30. $('#kilometers').val(miNum.toFixed(2));
  31. $('#numbers').html(miNum.toFixed(0));
  32. $('#mi-km').html('Kilometers');
  33. } else {
  34. $('#miles').val('');
  35. $('#kilometers').val('');
  36. $('#numbers').html('');
  37. $("#errmsg").html("Numbers Only").show().fadeOut(1600);
  38. }
  39. var needle = $("#needle");
  40. TweenLite.to(needle, 2, {rotation: speedMi, transformOrigin: "bottom right"});
  41. });
  42. // convert kilometers to miles
  43. $('#kilometers').keyup(function () {
  44. var km = $(this).val();
  45. var kmNum = parseInt(km) * 0.62137;
  46. //make sure kmNum is a number then output
  47. if ((km <= 195) && !isNaN(kmNum)) {
  48. var speedKm = kmNum * 2 - 31;
  49. $('#numbers').css('text-align', 'center');
  50. $('#miles').val(kmNum.toFixed(2));
  51. $('#numbers').html(kmNum.toFixed(0));
  52. $('#mi-km').html('Miles');
  53. } else if (!isNaN(kmNum)) {
  54. var speedKm = 215;
  55. $('#numbers').css('text-align', 'right');
  56. $('#miles').val(kmNum.toFixed(2));
  57. $('#numbers').html(kmNum.toFixed(0));
  58. $('#mi-km').html('Miles');
  59. } else {
  60. $('#miles').val('');
  61. $('#kilometers').val('');
  62. $('#numbers').html('');
  63. $("#errmsg").html("Numbers Only").show().fadeOut(1600);
  64. }
  65. var needle = $("#needle");
  66. TweenLite.to(needle, 2, {rotation: speedKm, transformOrigin: "bottom right"});
  67. });
  68. });