input text with following underline
by bizamajig
HTML
<input type='text' value='Some very long text example which exceeds the lengh of this splendid input field by almost twice the size' />
CSS
input{
padding:5px; margin: 20px; width:250px; font-size:20px;
border-radius:8px; background-color:#F1F1F1; border:1px solid #CCC;
box-shadow:3px 3px 4px #CCC inset;
background-image: linear-gradient(0deg, #40C8F4 100%, transparent 0%);
background-image: -webkit-linear-gradient(0deg, #40C8F4 100%, transparent 0%);
background-repeat: no-repeat;
background-position:0 100px;
transition:200ms;
-webkit-transition:200ms;
-o-transition:200ms;
}
/* for ugly chrome */
textarea:focus, input:focus{
outline: none;
}
/* for centering only */
input{ display:inline-block; vertical-align:middle; }
html, body{ height:100%; text-align: center; }
body:before{content:''; display:inline-block; height:100%; vertical-align:middle; margin-right:-0.25em; }
JavaScript
(function($){
var defaults = {
bgPos : '30px'
},
canvas = document.createElement('canvas'), // Canvas used to check text widths.
context = canvas.getContext('2d'),
iCaretPos, caret, totalWidth, cssRules, font, totalTextWidth, currentTextWidth,
locationPercentage, textWidthRatio, settings, selectors = [],
// find in which direction the user has selected the text, left or right
getSelectionDirection = {
direction : null,
lastOffset : null,
set : function(e){
if( e.shiftKey && e.keyCode == 36 )
getSelectionDirection.direction = 'left';
else if( e.shiftKey && e.keyCode == 35 )
getSelectionDirection.direction = 'right';
if( e.type == 'mousedown' )
getSelectionDirection.lastOffset = e.clientX;
else if( e.type == 'mouseup' ){
getSelectionDirection.direction = e.clientX < getSelectionDirection.lastOffset ? 'left' : 'right';
console.log(direction);
}
}
};
function getCaretPosition(oField, direction){
// if IE
if( document.selection ){
oField.focus();
var oSel = document.selection.createRange();
oSel.moveStart('character', -oField.value.length);
iCaretPos = oSel.text.length;
}
else if( oField.selectionStart || oField.selectionStart == '0' )
...