How we can animate the typing effect in a text input field? Solution: See this Animated Input Characters With jQuery & CSS, Change Typing Animation.
Previously I have shared many input related programs, but this program is for animating the characters while typing. Basically, a text input that we can use for a search box, fields like username, name, and email in forms. Mostly we customize the input field and the texts inside it, with the same way we can animate the text while typing.
Today you will learn to Change Typing Animation for text input. Basically, there is a text input field and options box with the next/previous navigations. The button is for showing the active effect and we can change the effect by clicking on next and pre buttons. And there are 26 kinds of animations, that you will see when typing inside the input field.
So, Today I am sharing Animated Input Characters With jQuery & CSS. There I have used jQuery for creating the animations and styles are based on CSS, but also jQuery using CSS commands to animating characters. As we know jQuery is a JS library, that’s why I am putting this in the JavaScript category. You can use it on your website for fun.
If you are thinking now how these characters typing animations actually are, then see the preview given below.
Preview Of Typing Animation Program
See this video preview to getting an idea of how the program looks like.
Now you can see this visually, also you can see it live by pressing the button given above. If you like this, then get the source code of its.
You May Also Like:
- Bootstrap Password Strength
- Moving Input Focus Effect
- JavaScript RGB Slider
- Instagram Media Downloader
Animated Input Characters With jQuery and CSS Source Code
Before sharing source code, let’s talk about it. First I have created a main div and put 2 divs inside it, one for selector box and one for the input field. There are only things which I have done using CSS, later elements will create using jQuery dynamically. Also in the HTML file, I have linked other files like CSS, JS, and jQuery CDN.
Now using CSS I have placed all the elements in the right place, as you can see in the preview. I gave value to all the elements which are created in an HTML file and jQuery file. With CSS I gave basic values like size, position, margin, padding, to the elements. I have used CSS @keyframe command for creating some animations.
jQuery handling here all the features of the program. The JS part is difficult, you must have good knowledge of JS/jQuery for creating this program. There I have used if{} else{} statements most of the time. Basically, jQuery adding CSS animation value to animating the characters. Also jQuery added options in the selector box.
Left all other things you will understand after getting the codes, I can’t explain all in writing. For creating this program you have to create 3 files. First file for HTML, second for CSS, and the third file for JavaScript. Follow the steps to creating this without any error.
index.html
Create an HTML file named ‘index.html‘ and put these codes given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<!DOCTYPE html> <!-- Code By Webdevtrick ( https://webdevtrick.com ) --> <html lang="en" > <head> <meta charset="UTF-8"> <title>Animated Input Characters | Webdevtrick.com</title> <link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.css'> <link rel="stylesheet" href="style.css"> </head> <body> <div class="center"> <div id="selector"></div> <div id="myInput"></div> </div> <script src='https://code.jquery.com/jquery-2.2.4.min.js'></script> <script src="function.js"></script> </body> </html> |
style.css
Now create a CSS file named ‘style.css‘ and put these codes given here.
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 |
/* Code By Webdevtrick ( https://webdevtrick.com ) */ @import "https://fonts.googleapis.com/css?family=Lato"; body { background-color: #788b9c; } .center { position: absolute; margin: auto; top: 0; right: 0; bottom: 0; left: 0; width: 570px; height: 40px; } .input { height: 40px; width: 400px; background-color: white; display: inline-block; border-radius: 5px; } .textZone { position: absolute; top: 5px; padding-left: 8px; width: 392px; height: 30px; outline: none; display: inline-block; white-space: nowrap; overflow: hidden; cursor: text; } .cursor { width: 1px; height: 100%; background-color: #222222; display: inline-block; animation-name: blink; animation-duration: 1s; animation-iteration-count: infinite; } .hidden { visibility: hidden; } .character, .placeholder { position: relative; display: inline-block; vertical-align: top; font-size: 24px; color: #555555; } .placeholder { color: #BFBFBF; } .space { display: inline-block; width: 7.2px; height: 100%; } @keyframes blink { 0% { opacity: 1; } 50% { opacity: 0; } } @keyframes colorTransition { 0% { color: #555555; } 50% { color: #F54E4E; } 75% { color: #444444; } 100% { color: #555555; } } .selector { width: 150px; margin-right: 10px; height: 40px; border: 1px white solid; display: inline-block; vertical-align: top; text-align: center; line-height: 40px; color: white; border-radius: 5px; font-family: 'Lato', sans-serif; -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; cursor: ns-resize; } .selection { animation-duration: 100ms; } .upArrow { width: 0; height: 0; border-style: solid; border-width: 0 5px 10px 5px; position: absolute; top: -20px; left: 72.5px; cursor: pointer; } .downArrow { width: 0; height: 0; border-style: solid; border-width: 10px 5px 0 5px; position: absolute; bottom: -22px; left: 72.5px; cursor: pointer; } .upWhiteArrow { border-color: transparent transparent #ffffff transparent; } .upGreyArrow { border-color: transparent transparent #777777 transparent; } .downWhiteArrow { border-color: #ffffff transparent transparent transparent; } .downGreyArrow { border-color: #777777 transparent transparent transparent; } |
function.js
The final step, create a JavaScript file named ‘function.js‘ and put the codes.
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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
// Code By Webdevtrick ( https://webdevtrick.com ) function _defineProperty(obj, key, value) {if (key in obj) {Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true });} else {obj[key] = value;}return obj;}class Input { constructor(input, placeholder) { this.isFocused = false; this.size = 0; this.animation = "zoomIn"; $(input).addClass("input"); this.$element = $(document.createElement("div")); this.$element.addClass("textZone"); this.$element.attr("tabindex", 0); $(input).append(this.$element); this.cursor = new Cursor(this); this.setEvents(); Keyboard.readCharacters(this); Keyboard.readSpecialCharacters(this); this.placeholder = new Placeholder(placeholder, this); } setEvents() { var input = this; this.$element.on("click", function (event) { input.focus(); event.stopPropagation(); }); $(document).on("click", function (event) { input.unfocus(); }); } focus() { if (this.size == 0) { this.$element.prepend(this.cursor.$element); } else { this.cursor.$element.insertAfter(this.$element.children().last()); } this.cursor.show(); this.isFocused = true; } unfocus() { if (this.size == 0) { this.placeholder.show(); } this.cursor.hide(); this.isFocused = false; } write(character) { this.size++; this.placeholder.hide(); character.setEvents(this); character.$element.insertAfter(this.cursor.$element); character.animate(this.animation); this.cursor.move("right"); } erase() { var last = this.cursor.$element.prev(); if (last.length && this.size > 0) { this.size--; this.cursor.move("left"); last.remove(); if (this.size == 0) { this.placeholder.show(); } } } suppress() { var next = this.cursor.$element.next(); if (next.length && this.size > 0) { this.size--; next.remove(); if (this.size == 0) { this.placeholder.show(); } } }} class Placeholder { constructor(placeholder, input) { this.input = input; this.$element = $(document.createElement("div")); this.$element.text(placeholder); this.$element.addClass("placeholder"); this.show(); } show() { this.input.$element.append(this.$element); } hide() { this.$element.remove(); }} class Keyboard { static readCharacters(input) { input.$element.on("keypress", function (event) { event.preventDefault(); input.write(new Character(String.fromCharCode(event.which))); }); } static readSpecialCharacters(input) { input.$element.on("keydown", function (event) { switch (event.keyCode) { case Keyboard.backspace: event.preventDefault(); input.erase(); break; case Keyboard.leftArrow: input.cursor.move("left"); break; case Keyboard.rightArrow: input.cursor.move("right"); break; case Keyboard.suppress: input.suppress(); break; case Keyboard.top: input.cursor.goTo("top"); break; case Keyboard.end: input.cursor.goTo("end"); break; default: break;} }); }}_defineProperty(Keyboard, "space", 32);_defineProperty(Keyboard, "backspace", 8);_defineProperty(Keyboard, "leftArrow", 37);_defineProperty(Keyboard, "rightArrow", 39);_defineProperty(Keyboard, "suppress", 46);_defineProperty(Keyboard, "top", 36);_defineProperty(Keyboard, "end", 35); class Cursor { constructor(input) { this.$element = $(document.createElement("div")); this.$element.addClass("cursor"); this.$element.addClass("hidden"); input.$element.prepend(this.$element); } show() { this.$element.removeClass("hidden"); } hide() { this.$element.addClass("hidden"); } move(direction) { var offSet = this.$element.get(0).offsetLeft; var textZone = this.$element.parent(); if (direction == "right") { var next = this.$element.next(); this.$element.insertAfter(next); if (offSet > textZone.width() * 0.99) { var scroll = textZone.scrollLeft(); textZone.animate({ scrollLeft: scroll + '100' }, 1000); } } else if (direction == "left") { var prev = this.$element.prev(); this.$element.insertBefore(prev); } } goTo(point) { if (point == "top") { this.$element.parent().prepend(this.$element); } else if (point == "end") { this.$element.parent().append(this.$element); } }} class Character { constructor(character) { this.$element = $(document.createElement("div")); if (character != " ") { this.$element.addClass("character"); this.$element.text(character); } else { this.$element.addClass("space"); } } setEvents(input) { var character = this; this.$element.on("click", function (event) { input.cursor.$element.insertBefore(character.$element); if (!input.isFocused) { input.cursor.show(); } event.stopPropagation(); }); } animate(animation) { this.$element.css("animation", animation + " 500ms, colorTransition 500ms"); }} class Selector { constructor(selector, options, defaultOption, callback) { this.$element = $(selector); this.$element.addClass("selector"); this.$selection = $(document.createElement("div")); this.$selection.addClass("selection"); var i = 0; this.current = i; this.$selection.text(options[i]); for (i = 0; i < options.length; i++) { if (options[i] == defaultOption) { this.current = i; this.$selection.text(options[i]); } } this.$element.append(this.$selection); this.options = options; this.setEvents(); this.setArrows(); this.animationEnd = 'webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend'; this.callback = callback; this.selecting = false; callback(options[this.current]); } setArrows() { var selector = this; this.$upArrow = $(document.createElement("div")); this.$upArrow.addClass("upArrow"); this.$element.append(this.$upArrow); this.$upArrow.on("click", function () { if (!selector.isFirst() && !selector.selecting) { selector.select("Down"); } }); this.$downArrow = $(document.createElement("div")); this.$downArrow.addClass("downArrow"); this.$element.append(this.$downArrow); this.$downArrow.on("click", function () { if (!selector.isLast() && !selector.selecting) { selector.select("Up"); } }); this.updateArrows(); } setEvents() { var selector = this; this.$element.on("wheel", function (event) { if (event.originalEvent.deltaY > 0) { if (!selector.isLast() && !selector.selecting) { selector.select("Up"); } } else { if (!selector.isFirst() && !selector.selecting) { selector.select("Down"); } } }); } isFirst() { return this.current == 0; } isLast() { return this.current == this.options.length - 1; } select(direction) { this.selecting = true; this.current = direction == "Up" ? this.current + 1 : this.current - 1; var selector = this; this.$selection.addClass("fadeOut" + direction).on(this.animationEnd, function () { selector.$selection.removeClass("fadeOut" + direction); selector.$selection.text(selector.options[selector.current]); selector.$selection.addClass("fadeIn" + direction).on(selector.animationEnd, function () { selector.$selection.removeClass("fadeIn" + direction); selector.callback(selector.options[selector.current]); selector.selecting = false; selector.updateArrows(); }); }); } updateArrows() { this.$upArrow.removeClass("upWhiteArrow"); this.$upArrow.removeClass("upGreyArrow"); this.$downArrow.removeClass("downWhiteArrow"); this.$downArrow.removeClass("downGreyArrow"); if (this.current == 0) { this.$upArrow.addClass("upGreyArrow"); if (this.options.length < 2) { this.$downArrow.addClass("downGreyArrow"); } else { this.$downArrow.addClass("downWhiteArrow"); } } else if (this.current == this.options.length - 1) { this.$upArrow.addClass("upWhiteArrow"); this.$downArrow.addClass("downGreyArrow"); } else { this.$upArrow.addClass("upWhiteArrow"); this.$downArrow.addClass("downWhiteArrow"); } }} var input = new Input("#myInput", "Try me!"); new Selector("#selector", [ "bounce", "fadeIn", "fadeInDown", "fadeInUp", "fadeInLeft", "fadeInRight", "flash", "jello", "lightSpeedIn", "pulse", "rollIn", "rotateIn", "rotateInDownLeft", "rotateInDownRight", "rotateInUpLeft", "rotateInUpRight", "rubberBand", "shake", "slideInDown", "slideInUp", "slideInLeft", "slideInRight", "swing", "tada", "wobble", "zoomIn"], "rubberBand", function (selection) { input.animation = selection; }); |
That’s It. Now you have successfully created Animated Input Characters With jQuery & CSS, Change Typing Animation inside the input field. If you have any doubt or question comment down below.
Thanks For Visiting, Keep Visiting.