Анимация иконок в поле ввода информации
Делаю страницу регистрации пользователя. Надо сделать так, чтобы при нажатии на одну из полей ввода, появлялась левая стоящая иконка там же(в поле ввода). Но если пользователь ещё не нажал на это поле ввода, иконка не должна показываться.Это как страница выглядит сейчас:
Всё желательно сделать на CSS,на JS тоже можно.
Пока код выглядит вот так(HTML):
<div className="input-form">
<input
type="text" value={name} className="has-icon-user" onChange={(e) => setName(e.target.value)}
placeholder="Name, Surname" />
</div>
<div className="input-form">
<input
type="email" value={email} className="has-icon-email" onChange={(e) => setEmail(e.target.value)}
placeholder="E-mail" />
</div>
<div className="input-form">
<input type="password" value={password} className="has-icon-password" onChange={(e) => setPassword(e.target.value)} placeholder="Password"/>
</div>
<button onClick={login} type="submit">Register</button>
<p onClick={alreadyHaveProfile}>I have a profile</p>
На CSS выглядит вот так:
>.input-form>input{
width: 100%;
height: 50px;
min-width: 250px;
margin-bottom: 20px;
padding: 0 10px;
outline: none;
color: var(--fg-input);
background-color: var(--bg-input);
border-radius: 4px;
font-family: 'Kalam', cursive;
font-size:14px;
font-weight:bold;
transition:border 250ms ease-in-out;
::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
:focus,:active {
background-color: #E8E8E8;
border:2px solid #ADADAD;
outline: none;
}
}
>.input-form>input:invalid {
border: 2px dashed red;
}
>.input-form>input[type=number],
input[type=text],
input[type=email],
input[type=password] {
-moz-appearance: textfield;
padding-left:55px;
}
>.input-form>.has-icon-user{
background-image:url("images/user.svg") ;
background-repeat: no-repeat;
background-size: 40px 40px;
background-position: 5px;
}
>.input-form>.has-icon-email{
background-image:url("images/email.svg") ;
background-size: 40px 40px;
background-repeat: no-repeat;
background-position: 5px;
}
>.input-form>.has-icon-password{
background-image:url("images/lock.svg") ;
background-repeat: no-repeat;
background-position: 5px;
background-size: 40px 40px;
}
Надеюсь на ваш ответ!
Ответы (2 шт):
Автор решения: Andrey Fedorov
→ Ссылка
Вот модельный код. Надеюсь разберетесь.
input {
padding-left: 20px;
}
input:focus-within+span {
display: block;
width: 0;
border: 10px solid red;
transform: translateY(-20px);
}
<input type="text" placeholder="Name, Surname">
<span></span>
Автор решения: Sevastopol'
→ Ссылка
Для иконки:
opacity: 0;
При фокусе:
.input:focus~.icon,
.input:active~.icon {
opacity: 1;
}
Пример:
.form {
position: relative;
width: 100%;
}
.input {
display: block;
position: relative;
width: calc(100% - 80px);
height: 60px;
padding: 0 0 0 80px;
border: 2px solid gray;
border-radius: 15px;
outline: none;
font-size: 20px;
color: gray;
}
.icon {
position: absolute;
top: 11px;
left: 15px;
width: 40px;
height: 40px;
fill: gray;
transform: scale(-1, 1);
opacity: 0;
transition: all 0.5s;
}
.input:focus~.icon,
.input:active~.icon {
opacity: 1;
}
<div class="form">
<form action="" method="GET">
<input class="input" type="text" placeholder="Поиск" name="name" required-placeholder="">
<svg class="icon" focusable="false" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M15.5 14h-.79l-.28-.27A6.471 6.471 0 0 0 16 9.5 6.5 6.5 0 1 0 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path></svg>
</form>
</div>
Можно добавить легкую анимацию. Например:
.form {
position: relative;
width: 100%;
}
.input {
display: block;
position: relative;
width: calc(100% - 80px);
height: 60px;
padding: 0 0 0 80px;
border: 2px solid gray;
border-radius: 15px;
outline: none;
font-size: 20px;
color: gray;
}
.icon {
position: absolute;
top: 31px;
left: 35px;
width: 0px;
height: 0px;
fill: gray;
transform: scale(-1, 1);
transition: all 0.5s;
}
.input:focus~.icon,
.input:active~.icon {
top: 11px;
left: 15px;
width: 40px;
height: 40px;
}
<div class="form">
<form action="" method="GET">
<input class="input" type="text" placeholder="Поиск" name="name" required-placeholder="">
<svg class="icon" focusable="false" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M15.5 14h-.79l-.28-.27A6.471 6.471 0 0 0 16 9.5 6.5 6.5 0 1 0 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path></svg>
</form>
</div>
