I'm implementing a design where a "fade to black" mask is expected to appear on the left side of the input once the text content reachs the maximum visible width of the input box.
The image bellow illustrates better what I'm trying to acomplish:
I already implemented part of it, with the code below:
var input = document.querySelector('#input');
var container = document.querySelector('.myInput');
input.addEventListener('keydown', function(e) {
if (input.value.length > 12) {
container.classList.add('faded');
} else {
container.classList.remove('faded');
}
});
body {
background: #000;
}
.myInput {
position: relative;
}
.myInput input {
font-family: "Trim", "NOW G", "Oscine";
font-style: normal;
font-weight: 500;
font-size: 28px;
background: #000;
padding: 12px;
color: #fff;
box-sizing: border-box;
margin: 0 0 0 10px;
border: 1px solid #ccc;
width: 200px;
}
.faded::before {
display: block;
background-image: -webkit-linear-gradient(left, black, rgba(0, 0, 0, 0));
width: 20px;
position: absolute;
content: "";
left: 15px;
top: 1px;
bottom: 1px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="myInput">
<input id="input" placeholder="Search" />
</div>
My problem now is how to add this mask conditionally (that is, removing the hardcoded 12 chars length and the input hardcoded width) and also, how to create a responsible solution that will work with all widths and all text sizes.
Any ideas?