원래 의도한 검색창
자동 완성이 뜨고 나서 원래 의도와 다른 색깔이 배경으로 뜬다
<input autocomplete="off"/>
<!--html-->
<header class="container">
<h1 id="headerTitle">EatTheMovie</h1>
<div id="searchUI">
<img src="icons/search.png" id="search-button" class="magnifier" />
<input
type="search"
id="search-input"
placeholder="영화를 검색하세요."
aria-label="search"
autocomplete="off"
/>
<img src="icons//cancel.png" id="cancle-button"/>
</div>
</header>
//css
.container {
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
position: sticky;
top: 0;
z-index: 100;
height: 70px;
padding-left: 60px;
padding-right: 60px;
color: white;
background-color: black;
}
#headerTitle {
font-size: 30px;
font-weight: 700;
color: yellow;
}
#searchUI {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
}
#search-input {
visibility: hidden;
position: absolute;
right: 120px;
height: 40px;
width: 0px;
background-color: black;
}
#search-input:focus {
outline: none;
}
#search-input.expand {
visibility: visible;
display: flex;
border: 1px solid white;
border-radius: 8px;
color: white;
width: 255px;
padding-left: 40px;
font-size: 15px;
transition: 0.4s ease;
}
.magnifier {
position: absolute;
right: 120px;
height: 30px;
width: 30px;
cursor: pointer;
transition: 0.4s ease;
z-index: 10;
}
.magnifier.expand {
right: 340px;
}
#cancle-button {
visibility: hidden;
margin-right: 40px;
height: 15px;
width: 15px;
cursor: pointer;
}
//javascript
const magnifier = document.querySelector(".magnifier");
const searchBar = document.querySelector("#search-input");
const cancleBtn = document.querySelector("#cancle-button");
magnifier.addEventListener("mouseover", () => {
searchBar.classList.add("expand");
magnifier.classList.add("expand");
cancleBtn.style.visibility = 'visible';
});
cancleBtn.addEventListener("click", () => {
searchBar.classList.remove("expand");
magnifier.classList.remove("expand");
cancleBtn.style.visibility = "hidden";
})
<img src="원래 이미지 주소.jpg" onerror="this.onerror=null; this.src='대체 이미지 주소';">