Skip to content

Instantly share code, notes, and snippets.

@wwwebman
Created May 27, 2017 08:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wwwebman/1d64f18672b1ff7866b48f09cca01f0d to your computer and use it in GitHub Desktop.
Save wwwebman/1d64f18672b1ff7866b48f09cca01f0d to your computer and use it in GitHub Desktop.
Add/Remove class when on input Focus/Blur with Pure JS
var IputEffect = function(){
var placeholderPosition, appendInputWhenSelect, actions;
actions = {
activate: function(el) {
el.parentNode.parentNode.addClass('active');
},
deactivate: function(el) {
if (el.value === '') el.parentNode.parentNode.removeClass('active');
}
};
placeholderPosition = function (el) {
if (el.value !== '') actions.activate(el);
el.addEventListener('focus', function(){actions.activate(this)});
el.addEventListener('blur', function(){actions.deactivate(this)});
}
document.querySelectorAll('.js_int').forEach(function(el){
placeholderPosition(el);
})
}
IputEffect();
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div class="input-item">
<span><input type="tel" name="d1" value="" class="int js_int"></span>
<label>Data</label>
</div>
</body>
</html>
int {
background-color: transparent;
background-image: none; // Reset unusual Firefox-on-Android default style; see https://github.com/necolas/normalize.css/issues/214
border-radius: 0;
border: 0;
border-bottom: 1px solid #333;
color: #555;
display: block;
padding: 10px;
font-size: 16px;
line-height: 1;
transition: all .3s;
width: 100%;
outline: none;
}
.input-item {
position: relative;
margin-bottom: 30px;
label {
font-size: .9em;
transition: all 0.35s;
pointer-events: none;
top: .4em;
position: absolute;
user-select: none;
color: #555;
padding: 8px 15px;
}
&.active {
label {
top: -1.75em;
color: #000;
font-size: .75em;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment