A Pen by Josh Studley on CodePen.
Created
September 20, 2018 20:17
-
-
Save Steamforge/1dcac2300106158e1025e14729373e68 to your computer and use it in GitHub Desktop.
Floating label
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="floatContainer1" class="float-container"> | |
<label for="floatField1">Float Label 1</label> | |
<input type="text" id="floatField1" data-placeholder="Placeholder 1"> | |
</div> | |
<div id="floatContainer2" class="float-container"> | |
<label for="floatField2">Float Label 2</label> | |
<input type="text" id="floatField2" data-placeholder="Placeholder 2"> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const FloatLabel = (() => { | |
// add active class and placeholder | |
const handleFocus = (e) => { | |
const target = e.target; | |
target.parentNode.classList.add('active'); | |
target.setAttribute('placeholder', target.getAttribute('data-placeholder')); | |
}; | |
// remove active class and placeholder | |
const handleBlur = (e) => { | |
const target = e.target; | |
if(!target.value) { | |
target.parentNode.classList.remove('active'); | |
} | |
target.removeAttribute('placeholder'); | |
}; | |
// register events | |
const bindEvents = (element) => { | |
const floatField = element.querySelector('input'); | |
floatField.addEventListener('focus', handleFocus); | |
floatField.addEventListener('blur', handleBlur); | |
}; | |
// get DOM elements | |
const init = () => { | |
const floatContainers = document.querySelectorAll('.float-container'); | |
floatContainers.forEach((element) => { | |
if (element.querySelector('input').value) { | |
element.classList.add('active'); | |
} | |
bindEvents(element); | |
}); | |
}; | |
return { | |
init: init | |
}; | |
})(); | |
FloatLabel.init(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
* { | |
font-family: arial; | |
} | |
/** float container */ | |
.float-container { | |
border: solid 1px #ccc; | |
box-sizing: border-box; | |
margin-bottom: 8px; | |
padding: 0 8px; | |
position: relative; | |
width: 300px; | |
input { | |
border: none; | |
font-size: 16px; | |
outline: 0; | |
padding: 16px 0 10px; | |
width: 100%; | |
} | |
label { | |
font-size: 16px; | |
position: absolute; | |
transform-origin: top left; | |
transform: translate(0, 16px) scale(1); | |
transition: all .1s ease-in-out; //speed | |
} | |
/** active label */ | |
&.active { | |
label { | |
//move the x coordinate and reduce size | |
transform: translate(0, 4px) scale(.75); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment