Before you begin
This tutorial requires InstantSearch JS.High-level overview
To create an animated placeholder, you want to run some code that animates text character by character with pauses between, and injects it into the search box as placeholder text. The length of a pause should be random so it looks like actual typing. The recommended delay range of 50-90 milliseconds looks the most natural.Single placeholder
Add an InstantSearchsearchBox
widget and set its placeholder to an empty string.
JavaScript
- The search box element
- The delay after the animation has run
- The animation delay between letters (a minimum and maximum value)
- Your placeholder text.
JavaScript
JavaScript
JavaScript
JavaScript
animateLetters
.
JavaScript
JavaScript
Multiple placeholders
To animate multiple placeholders, you need to make a few changes to the code you wrote for a single placeholder implementation. Change yourPLACEHOLDER
constant to an array of strings called PLACEHOLDERS
,
containing all the placeholder sentences you want to display.
JavaScript
onAnimationEnd
callback function that selects a random (but different) placeholder string from your PLACEHOLDERS
array and calls the animatePlaceholder
function with the new string. This function triggers every time the animation ends.
JavaScript
animateLetter
function so that it calls the onAnimationEnd
function whenever a placeholder animation ends.
JavaScript
load
event listener so that it passes the onAnimationEnd
callback and the first item of your PLACEHOLDERS
array to animatePlaceholders
.
JavaScript