Skip to content

Commit 031647f

Browse files
Feat: Add visual feedback on conversion
1 parent b2b2513 commit 031647f

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

script.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ document.addEventListener('DOMContentLoaded', function() {
66
const milligramsInput = document.getElementById('milligrams');
77
const convertButton = document.getElementById('convertButton');
88
const clearButton = document.getElementById('clearButton');
9+
const highlightDuration = 300; // milliseconds
910

1011
function saveConversion() {
1112
localStorage.setItem('lastKilograms', kilogramsInput.value);
@@ -25,7 +26,6 @@ document.addEventListener('DOMContentLoaded', function() {
2526
if (lastPounds !== null) poundsInput.value = lastPounds;
2627
if (lastMilligrams !== null) milligramsInput.value = lastMilligrams;
2728

28-
// Trigger input events to perform conversions on load if values are present
2929
if (lastKilograms) kilogramsInput.dispatchEvent(new Event('input'));
3030
else if (lastGrams) gramsInput.dispatchEvent(new Event('input'));
3131
else if (lastPounds) poundsInput.dispatchEvent(new Event('input'));
@@ -39,16 +39,26 @@ document.addEventListener('DOMContentLoaded', function() {
3939
if (elementToExclude !== milligramsInput) milligramsInput.value = '';
4040
}
4141

42+
function highlightInput(inputElement) {
43+
inputElement.classList.add('conversion-highlight');
44+
setTimeout(() => {
45+
inputElement.classList.remove('conversion-highlight');
46+
}, highlightDuration);
47+
}
48+
4249
kilogramsInput.addEventListener('input', function() {
4350
clearAllInputsExcept(kilogramsInput);
4451
const kg = parseFloat(kilogramsInput.value);
4552
if (!isNaN(kg)) {
4653
gramsInput.value = (kg * 1000).toFixed(2);
4754
poundsInput.value = (kg * 2.20462).toFixed(2);
4855
milligramsInput.value = (kg * 1000000).toFixed(2);
56+
highlightInput(gramsInput);
57+
highlightInput(poundsInput);
58+
highlightInput(milligramsInput);
4959
saveConversion();
5060
} else {
51-
saveConversion(); // Still save even if input is cleared
61+
saveConversion();
5262
}
5363
});
5464

@@ -59,6 +69,9 @@ document.addEventListener('DOMContentLoaded', function() {
5969
kilogramsInput.value = (g / 1000).toFixed(2);
6070
poundsInput.value = (g * 0.00220462).toFixed(2);
6171
milligramsInput.value = (g * 1000).toFixed(2);
72+
highlightInput(kilogramsInput);
73+
highlightInput(poundsInput);
74+
highlightInput(milligramsInput);
6275
saveConversion();
6376
} else {
6477
saveConversion();
@@ -72,6 +85,9 @@ document.addEventListener('DOMContentLoaded', function() {
7285
kilogramsInput.value = (lbs / 2.20462).toFixed(2);
7386
gramsInput.value = (lbs / 0.00220462).toFixed(2);
7487
milligramsInput.value = (lbs * 453592.37).toFixed(2);
88+
highlightInput(kilogramsInput);
89+
highlightInput(gramsInput);
90+
highlightInput(milligramsInput);
7591
saveConversion();
7692
} else {
7793
saveConversion();
@@ -85,6 +101,9 @@ document.addEventListener('DOMContentLoaded', function() {
85101
kilogramsInput.value = (mg / 1000000).toFixed(2);
86102
gramsInput.value = (mg / 1000).toFixed(2);
87103
poundsInput.value = (mg / 453592.37).toFixed(2);
104+
highlightInput(kilogramsInput);
105+
highlightInput(gramsInput);
106+
highlightInput(poundsInput);
88107
saveConversion();
89108
} else {
90109
saveConversion();
@@ -100,7 +119,7 @@ document.addEventListener('DOMContentLoaded', function() {
100119
gramsInput.value = '';
101120
poundsInput.value = '';
102121
milligramsInput.value = '';
103-
saveConversion(); // Save empty values to clear local storage on clear
122+
saveConversion();
104123
});
105124

106125
loadLastConversion();

style.css

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ h1 {
2222
box-shadow: 0 0 25px rgba(0, 0, 0, 0.2);
2323
display: flex;
2424
flex-direction: column;
25-
gap: 20px; /* Adjusted gap to accommodate more fields */
25+
gap: 20px;
2626
width: 400px;
2727
animation: fadeIn 0.5s ease-out;
2828
}
@@ -48,15 +48,16 @@ label {
4848
font-weight: 500;
4949
color: #495057;
5050
font-size: 1.1em;
51-
width: 150px; /* Adjusted label width */
51+
width: 150px;
5252
}
5353

5454
input[type="number"] {
5555
padding: 12px;
5656
border: 1px solid #ced4da;
5757
border-radius: 8px;
58-
width: 220px; /* Adjusted input width */
58+
width: 220px;
5959
font-size: 1.1em;
60+
transition: background-color 0.2s ease-in-out, border-color 0.2s ease-in-out; /* Added transition for smoother highlight */
6061
}
6162

6263
input[type="number"]:focus {
@@ -65,6 +66,11 @@ input[type="number"]:focus {
6566
box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25);
6667
}
6768

69+
.conversion-highlight {
70+
background-color: #e9ecef; /* Light gray background for highlight */
71+
border-color: #007bff !important; /* Highlight border color */
72+
}
73+
6874
button {
6975
padding: 14px 28px;
7076
background-color: #007bff;

0 commit comments

Comments
 (0)