JavaScript - Animacion de control input text

 
Vista:
sin imagen de perfil

Animacion de control input text

Publicado por Maria Elena (3 intervenciones) el 16/09/2021 13:57:39
Hola a todos! Espero puedan ayudarme. Soy nueva en JS.
He personalizado un control input (he conseguido en la web como hacer esto) y funciona muy bien pero solo para el primer control. No se como hacerlo funcionar para el resto de input's del formulario.

HTML
1
2
3
4
5
6
<div class='ctrl__button ctrl__button--decrement'>&ndash;</div>
                          <div class='ctrl__counter'>
                              <input class='ctrl__counter-input'   maxlength='3' type='text' value='0'>
                            <div class='ctrl__counter-num'>0</div>
                          </div>
                          <div class='ctrl__button ctrl__button--increment'>+</div>

CSS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
.ctrl {
  -webkit-box-flex: 0;
  -webkit-flex: 0 0 auto;
  -ms-flex: 0 0 auto;
  flex: 0 0 auto;
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
  -webkit-align-items: center;
  -ms-flex-align: center;
  align-items: center;
  background-color: #fff;
  border-radius: 5px;
  font-size: 1em;
}
 
.ctrl__counter {
  position: relative;
  width: 100px;
  height: 25px;
  color: #333C48;
  text-align: center;
  overflow: hidden;
}
 
.ctrl__counter.is-input .ctrl__counter-num {
  visability: hidden;
  opacity: 0;
  -webkit-transition: opacity 100ms ease-in;
  transition: opacity 100ms ease-in;
}
 
.ctrl__counter.is-input .ctrl__counter-input {
  visability: visible;
  opacity: 1;
  -webkit-transition: opacity 100ms ease-in;
  transition: opacity 100ms ease-in;
}
 
.ctrl__counter-input {
  width: 100%;
  margin: 0;
  padding: 0;
  position: relative;
  z-index: 2;
  box-shadow: none;
  outline: none;
  border: none;
  color: #333C48;
  font-size: 1em;
  line-height: 30px;
  text-align: center;
  visability: hidden;
  opacity: 0;
  -webkit-transition: opacity 100ms ease-in;
  transition: opacity 100ms ease-in;
}
.ctrl__button {
  width: 50px;
  line-height: 30px;
  text-align: center;
  color: #fff;
  cursor: pointer;
  background-color: #2196F3;
  -webkit-transition: background-color 100ms ease-in;
  transition: background-color 100ms ease-in;
}
 
.ctrl__button:hover {
  background-color: #90a2b0;
  -webkit-transition: background-color 100ms ease-in;
  transition: background-color 100ms ease-in;
}
 
.ctrl__button:active {
  background-color: #778996;
  -webkit-transition: background-color 100ms ease-in;
  transition: background-color 100ms ease-in;
}
 
.ctrl__button--decrement { border-radius: 5px 0 0 5px; }
 
.ctrl__button--increment { border-radius: 0 5px 5px 0; }
 
.ctrl__counter-num {
  position: absolute;
  z-index: 1;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  line-height: 30px;
  visability: visible;
  opacity: 1;
  -webkit-transition: opacity 100ms ease-in;
  transition: opacity 100ms ease-in;
}
 
.ctrl__counter-num.is-increment-hide {
  opacity: 0;
  -webkit-transform: translateY(-50px);
  transform: translateY(-50px);
  -webkit-animation: increment-prev 100ms ease-in;
  animation: increment-prev 100ms ease-in;
}
 
.ctrl__counter-num.is-increment-visible {
  opacity: 1;
  -webkit-transform: translateY(0);
  transform: translateY(0);
  -webkit-animation: increment-next 100ms ease-out;
  animation: increment-next 100ms ease-out;
}
 
.ctrl__counter-num.is-decrement-hide {
  opacity: 0;
  -webkit-transform: translateY(50px);
  transform: translateY(50px);
  -webkit-animation: decrement-prev 100ms ease-in;
  animation: decrement-prev 100ms ease-in;
}
 
.ctrl__counter-num.is-decrement-visible {
  opacity: 1;
  -webkit-transform: translateY(0);
  transform: translateY(0);
  -webkit-animation: decrement-next 100ms ease-out;
  animation: decrement-next 100ms ease-out;
}
 
@-webkit-keyframes
decrement-prev {  from {
 opacity: 1;
 -webkit-transform: translateY(0);
 transform: translateY(0);
}
}
 
@keyframes
decrement-prev {  from {
 opacity: 1;
 -webkit-transform: translateY(0);
 transform: translateY(0);
}
}
 
@-webkit-keyframes
decrement-next {  from {
 opacity: 0;
 -webkit-transform: translateY(-50px);
 transform: translateY(-50px);
}
}
 
@keyframes
decrement-next {  from {
 opacity: 0;
 -webkit-transform: translateY(-50px);
 transform: translateY(-50px);
}
}
 
@-webkit-keyframes
increment-prev {  from {
 opacity: 1;
 -webkit-transform: translateY(0);
 transform: translateY(0);
}
}
 
@keyframes
increment-prev {  from {
 opacity: 1;
 -webkit-transform: translateY(0);
 transform: translateY(0);
}
}
 
@-webkit-keyframes
increment-next {  from {
 opacity: 0;
 -webkit-transform: translateY(50px);
 transform: translateY(50px);
}
}
 
@keyframes
increment-next {  from {
 opacity: 0;
 -webkit-transform: translateY(50px);
 transform: translateY(50px);
}
}

JS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
(function() {
 
  'use strict';
 
  function ctrls() {
    var _this = this;
 
    this.counter = 0;
    this.els = {
      decrement: document.querySelector('.ctrl__button--decrement'),
      counter: {
        container: document.querySelector('.ctrl__counter'),
        num: document.querySelector('.ctrl__counter-num'),
        input: document.querySelector('.ctrl__counter-input')
      },
      increment: document.querySelector('.ctrl__button--increment')
    };
 
    this.decrement = function() {
      var counter = _this.getCounter();
      var nextCounter = (_this.counter > 0) ? --counter : counter;
      _this.setCounter(nextCounter);
    };
 
    this.increment = function() {
      var counter = _this.getCounter();
      var nextCounter = (counter < 9999999999) ? ++counter : counter;
      _this.setCounter(nextCounter);
    };
 
    this.getCounter = function() {
      return _this.counter;
    };
 
    this.setCounter = function(nextCounter) {
      _this.counter = nextCounter;
    };
 
    this.debounce = function(callback) {
      setTimeout(callback, 100);
    };
 
    this.render = function(hideClassName, visibleClassName) {
      _this.els.counter.num.classList.add(hideClassName);
 
      setTimeout(function() {
        _this.els.counter.num.innerText = _this.getCounter();
        _this.els.counter.input.value = _this.getCounter();
        _this.els.counter.num.classList.add(visibleClassName);
      }, 100);
 
      setTimeout(function() {
        _this.els.counter.num.classList.remove(hideClassName);
        _this.els.counter.num.classList.remove(visibleClassName);
      }, 200);
    };
 
    this.ready = function() {
      _this.els.decrement.addEventListener('click', function() {
        _this.debounce(function() {
          _this.decrement();
          _this.render('is-decrement-hide', 'is-decrement-visible');
        });
      });
 
      _this.els.increment.addEventListener('click', function() {
        _this.debounce(function() {
          _this.increment();
          _this.render('is-increment-hide', 'is-increment-visible');
        });
      });
 
      _this.els.counter.input.addEventListener('input', function(e) {
        var parseValue = parseInt(e.target.value);
        if (!isNaN(parseValue) && parseValue >= 0) {
          _this.setCounter(parseValue);
          _this.render();
        }
      });
 
      _this.els.counter.input.addEventListener('focus', function(e) {
        _this.els.counter.container.classList.add('is-input');
      });
 
      _this.els.counter.input.addEventListener('blur', function(e) {
        _this.els.counter.container.classList.remove('is-input');
        _this.render();
      });
    };
  };
 
  // init
  var controls = new ctrls();
  document.addEventListener('DOMContentLoaded', controls.ready);
})();
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder