Created
May 27, 2017 08:18
-
-
Save wwwebman/1d64f18672b1ff7866b48f09cca01f0d to your computer and use it in GitHub Desktop.
Add/Remove class when on input Focus/Blur with Pure JS
This file contains hidden or 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
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(); |
This file contains hidden or 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
<!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> |
This file contains hidden or 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
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