Reputation: 1615
I have a animated search bar, which changes width and height when focused.
Then I had attached a autocomplete suggestion list with it. But the problem is it's not animating with search bar.
So if the user types before completion of animation my autocomplete list just spawn in front of it with no related size and position
What I want:-
A way to smoothly animate autocomplete list like the search bar with same size and position of search-bar.
I have tried '+' sibling combinator of css but I was not able to make it work.
Can anyone help?
function autocomplete(inp, arr) {
/*the autocomplete function takes two arguments,
the text field element and an array of possible autocompleted values:*/
var currentFocus;
/*execute a function when someone writes in the text field:*/
inp.addEventListener("input", function(e) {
var a, b, i, val = this.value;
/*close any already open lists of autocompleted values*/
closeAllLists();
if (!val) { return false;}
currentFocus = -1;
/*create a DIV element that will contain the items (values):*/
a = document.createElement("DIV");
a.setAttribute("id", this.id + "autocomplete-list");
a.setAttribute("class", "autocomplete-items");
/*append the DIV element as a child of the autocomplete container:*/
this.parentNode.appendChild(a);
/*for each item in the array...*/
for (i = 0; i < arr.length; i++) {
/*check if the item starts with the same letters as the text field value:*/
if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
/*create a DIV element for each matching element:*/
b = document.createElement("DIV");
/*make the matching letters bold:*/
b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>";
b.innerHTML += arr[i].substr(val.length);
/*insert a input field that will hold the current array item's value:*/
b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";
/*execute a function when someone clicks on the item value (DIV element):*/
b.addEventListener("click", function(e) {
/*insert the value for the autocomplete text field:*/
inp.value = this.getElementsByTagName("input")[0].value;
/*close the list of autocompleted values,
(or any other open lists of autocompleted values:*/
closeAllLists();
});
a.appendChild(b);
}
}
});
/*execute a function presses a key on the keyboard:*/
inp.addEventListener("keydown", function(e) {
var x = document.getElementById(this.id + "autocomplete-list");
if (x) x = x.getElementsByTagName("div");
if (e.keyCode == 40) {
/*If the arrow DOWN key is pressed,
increase the currentFocus variable:*/
currentFocus++;
/*and and make the current item more visible:*/
addActive(x);
} else if (e.keyCode == 38) { //up
/*If the arrow UP key is pressed,
decrease the currentFocus variable:*/
currentFocus--;
/*and and make the current item more visible:*/
addActive(x);
} else if (e.keyCode == 13) {
/*If the ENTER key is pressed, prevent the form from being submitted,*/
e.preventDefault();
if (currentFocus > -1) {
/*and simulate a click on the "active" item:*/
if (x) x[currentFocus].click();
}
}
});
function addActive(x) {
/*a function to classify an item as "active":*/
if (!x) return false;
/*start by removing the "active" class on all items:*/
removeActive(x);
if (currentFocus >= x.length) currentFocus = 0;
if (currentFocus < 0) currentFocus = (x.length - 1);
/*add class "autocomplete-active":*/
x[currentFocus].classList.add("autocomplete-active");
}
function removeActive(x) {
/*a function to remove the "active" class from all autocomplete items:*/
for (var i = 0; i < x.length; i++) {
x[i].classList.remove("autocomplete-active");
}
}
function closeAllLists(elmnt) {
/*close all autocomplete lists in the document,
except the one passed as an argument:*/
var x = document.getElementsByClassName("autocomplete-items");
for (var i = 0; i < x.length; i++) {
if (elmnt != x[i] && elmnt != inp) {
x[i].parentNode.removeChild(x[i]);
}
}
}
/*execute a function when someone clicks in the document:*/
document.addEventListener("click", function (e) {
closeAllLists(e.target);
});
}
/*An array containing all the country names in the world:*/
var countries = ["Afghanistan","Albania","Algeria","Andorra","Angola","Anguilla","Antigua & Barbuda","Argentina","Armenia","Aruba","Australia","Austria","Azerbaijan","Bahamas","Bahrain","Bangladesh","Barbados","Belarus","Belgium","Belize","Benin","Bermuda","Bhutan","Bolivia","Bosnia & Herzegovina","Botswana","Brazil","British Virgin Islands","Brunei","Bulgaria","Burkina Faso","Burundi","Cambodia","Cameroon","Canada","Cape Verde","Cayman Islands","Central African Republic","Chad","Chile","China","Colombia","Congo","Cook Islands","Costa Rica","Cote D Ivoire","Croatia","Cuba","Curacao","Cyprus","Czech Republic","Denmark","Djibouti","Dominica","Dominican Republic","Ecuador","Egypt","El Salvador","Equatorial Guinea","Eritrea","Estonia","Ethiopia","Falkland Islands","Faroe Islands","Fiji","Finland","France","French Polynesia","French West Indies","Gabon","Gambia","Georgia","Germany","Ghana","Gibraltar","Greece","Greenland","Grenada","Guam","Guatemala","Guernsey","Guinea","Guinea Bissau","Guyana","Haiti","Honduras","Hong Kong","Hungary","Iceland","India","Indonesia","Iran","Iraq","Ireland","Isle of Man","Israel","Italy","Jamaica","Japan","Jersey","Jordan","Kazakhstan","Kenya","Kiribati","Kosovo","Kuwait","Kyrgyzstan","Laos","Latvia","Lebanon","Lesotho","Liberia","Libya","Liechtenstein","Lithuania","Luxembourg","Macau","Macedonia","Madagascar","Malawi","Malaysia","Maldives","Mali","Malta","Marshall Islands","Mauritania","Mauritius","Mexico","Micronesia","Moldova","Monaco","Mongolia","Montenegro","Montserrat","Morocco","Mozambique","Myanmar","Namibia","Nauro","Nepal","Netherlands","Netherlands Antilles","New Caledonia","New Zealand","Nicaragua","Niger","Nigeria","North Korea","Norway","Oman","Pakistan","Palau","Palestine","Panama","Papua New Guinea","Paraguay","Peru","Philippines","Poland","Portugal","Puerto Rico","Qatar","Reunion","Romania","Russia","Rwanda","Saint Pierre & Miquelon","Samoa","San Marino","Sao Tome and Principe","Saudi Arabia","Senegal","Serbia","Seychelles","Sierra Leone","Singapore","Slovakia","Slovenia","Solomon Islands","Somalia","South Africa","South Korea","South Sudan","Spain","Sri Lanka","St Kitts & Nevis","St Lucia","St Vincent","Sudan","Suriname","Swaziland","Sweden","Switzerland","Syria","Taiwan","Tajikistan","Tanzania","Thailand","Timor L'Este","Togo","Tonga","Trinidad & Tobago","Tunisia","Turkey","Turkmenistan","Turks & Caicos","Tuvalu","Uganda","Ukraine","United Arab Emirates","United Kingdom","United States of America","Uruguay","Uzbekistan","Vanuatu","Vatican City","Venezuela","Vietnam","Virgin Islands (US)","Yemen","Zambia","Zimbabwe"];
/*initiate the autocomplete function on the "myInput" element, and pass along the countries array as possible autocomplete values:*/
autocomplete(document.getElementById("patternSearchInput"), countries);
.patternSearchBar {
position: absolute;
top: 50%;
left: 35.5%;
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
width: 40%;
margin-left: 15%;
box-sizing: border-box;
border-width: 3px;
border-style: solid;
border-radius: 4px;
font-size: 16px;
background-color: white;
background-image: url("search-icon.svg");
background-position: 10px 10px;
background-repeat: no-repeat;
border-color: #ffaf7b;
padding: 12px 20px 12px 40px;
transition: width 0.5s ease, top 0.4s ease, border-color 0.5s ease;
}
.homeImage {
position: absolute;
top: 22%;
left: 15.5%;
width: 40%;
margin-left: 15%;
height: 20%;
transition: 0.5s;
}
.patternSearchBar:focus {
width: 75%;
top: 20%;
border-color: #ff512f;
transition: 1s;
margin-right: 50%;
}
.patternSearchBar:focus+.homeImage {
top: 5%;
left: 23%;
width: 20%;
height: 10%;
transition: 0.8s ease;
}
.autocomplete {
margin-left: 15%;
margin-top: 6%;
width: 70%;
transition: 0.8s;
}
.autocomplete-items {
position: relative;
border: 2px solid #d4d4d4;
border-top: none;
border-bottom: none;
box-shadow: 0 10px 20px rgba(0,0,0,0.19), 0 6px 6px rgba(0,0,0,0.23);
z-index: 99;
/*position the autocomplete items to be the same width as the container:*/
}
.autocomplete-items div {
padding: 10px;
cursor: pointer;
color: teal;
background-color: #fff;
border-bottom: 1px solid #d4d4d4;
}
/*when hovering an item:*/
.autocomplete-items div:hover {
background-color: DodgerBlue;
}
/*when navigating through the items using the arrow keys:*/
.autocomplete-active {
background-color: DodgerBlue !important;
color: #ffffff;
}
<form autocomplete="off" action="/action_page.php">
<div class="autocomplete">
<input class="patternSearchBar" id="patternSearchInput" placeholder="Pattern...">
<img class="homeImage" src="https://via.placeholder.com/150x500" alt="Home Page">
</div>
</form>
Here is my code:- CodePen
Upvotes: 3
Views: 1130
Reputation: 275
You can try adding a wrapper to animate the content like this:
Change HTML to:
<form autocomplete="off" action="/action_page.php">
<div class="autocomplete">
<div class="patternSearchBarWrapper">
<input class="patternSearchBar" id="patternSearchInput" placeholder="Pattern...">
</div>
<img class="homeImage" src="https://via.placeholder.com/150x500" alt="Home Page">
</div>
In the css, add the class "patternSearchBarWrapper":
.patternSearchBarWrapper {
position: absolute;
top: 50%;
left: 35.5%;
-ms-transform: translate(-50%, 0);
transform: translate(-50%, 0);
width: 40%;
margin-left: 15%;
box-sizing: border-box;
transition: width 0.5s ease, top 0.4s ease, border-color 0.5s ease;
}
and change the class "patternSearchBar" to:
.patternSearchBar {
font-size: 16px;
border: #fff;
width: 100%;
height: 100%;
box-sizing: border-box;
padding: 12px 20px 12px 40px;
border-width: 3px;
border-style: solid;
border-radius: 4px;
background-color: white;
background-image: url("search-icon.svg");
background-position: 10px 10px;
background-repeat: no-repeat;
border-color: #ffaf7b;
transition: width 0.5s ease, top 0.4s ease, border-color 0.5s ease;
}
also change:
.patternSearchBar:focus {
to
.patternSearchBarWrapper:focus-within {
and
.patternSearchBar:focus + .homeImage {
to
.patternSearchBarWrapper:focus-within + .homeImage {
Finally, change the javascript: (roughly line 30)
from
b.addEventListener("click", function(e) {
to
b.addEventListener("mousedown", function(e) {
Careful: What I wrote is not a complete solution. You will have to do some clean-up after this.
Upvotes: 1
Reputation: 411
If you want to animate the searchbar and autocomplete list together you should put both of them into a DIV, and animate the div instead. Working example on jsFiddle.
<form autocomplete="off" action="/action_page.php">
<div class="autocomplete">
<div class="searchBarContainer">
<input class="patternSearchBar" id="patternSearchInput" placeholder="Pattern...">
</div>
</div>
</form>
.autocomplete {
margin-left: 15%;
margin-top: 6%;
width: 70%;
transition: 0.8s;
}
.searchBarContainer {
position: absolute;
top: 50%;
left: 35.5%;
-ms-transform: translateX(-45%);
transform: translate(-45%);
width: 40%;
margin-left: 15%;
display: flex;
justify-content: center;
align-items: flex-start;
flex-wrap: wrap;
}
.searchBarContainer.focused {
width: 75%;
top: 20%;
margin-right: 50%;
transition: 1s;
}
.patternSearchBar {
width: 100%;
box-sizing: border-box;
border-width: 3px;
border-style: solid;
border-radius: 4px;
font-size: 16px;
background-color: white;
background-image: url("search-icon.svg");
background-position: 10px 10px;
background-repeat: no-repeat;
border-color: #ffaf7b;
padding: 12px 20px 12px 40px;
transition: width 0.5s ease, top 0.4s ease, border-color 0.5s ease;
}
.searchBarContainer.focused .patternSearchBar {
border-color: #ff512f;
}
.homeImage {
position: absolute;
top: 22%;
left: 15.5%;
width: 40%;
margin-left: 15%;
height: 20%;
transition: 0.5s;
}
.searchBarContainer.focused .homeImage {
top: 5%;
left: 23%;
width: 20%;
height: 10%;
transition: 0.8s ease;
}
.autocomplete-items {
width: 100%;
position: relative;
border: 2px solid #d4d4d4;
border-top: none;
border-bottom: none;
box-shadow: 0 10px 20px rgba(0,0,0,0.19), 0 6px 6px rgba(0,0,0,0.23);
z-index: 99;
/*position the autocomplete items to be the same width as the container:*/
}
.autocomplete-items div {
padding: 10px;
cursor: pointer;
color: teal;
background-color: #fff;
border-bottom: 1px solid #d4d4d4;
}
.autocomplete-items div:hover {
background-color: DodgerBlue;
}
/*when navigating through the items using the arrow keys:*/
.autocomplete-active {
background-color: DodgerBlue !important;
color: #ffffff;
}
and finally i defined 'focus' and 'blur' event listener to add/remove animation class to the parent container.
inp.addEventListener("focus", function(e) {
this.parentNode.setAttribute("class", "searchBarContainer focused");
});
inp.addEventListener("blur", function(e) {
this.parentNode.setAttribute("class", "searchBarContainer");
});
Upvotes: 2