制作漂亮的搜索栏效果

搜索栏

html代码

<div class="search-icon">
  <input type="text" />
  <span class="circle"></span>
  <span class="handle"></span>
</div>

css代码

* {
  padding: 0;
  margin: 0;
}
body {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: #e2deff;
}
.search-icon {
  background-color: #e2deff;
  position: relative;
}
.search-icon input {
  width: 600px;
  height: 120px;
  border-radius: 15px;
  background-color: #5f46ff;
  box-shadow: 2px 3px 15px #5f46ff;
  outline: none;
  color: white;
  font-size: 48px;
  border: none;
  padding-left: 120px;
  caret-color: white;
}
.search-icon input:focus {
  border: none;
  outline: none;
}
.circle {
  position: relative;
  display: block;
  height: 38px;
  width: 38px;
  background-color: #5f46ff;
  border-radius: 70px;
  z-index: 0;
  border: 10px solid white;
  transition: all 0.6s;
  z-index: 1;
  left: 25px;
  top: -100px;
}
.handle {
  position: absolute;
  background-color: white;
  top: 56px;
  left: 24px;
  width: 90px;
  height: 10px;
  display: block;
  transform: rotate(45deg);
  cursor: pointer;
  float: right;
  border-radius: 2px;
}
.handle:after {
  content: "";
  background-color: white;
  width: 90px;
  height: 10px;
  display: block;
  float: right;
  border-radius: 2px;
  transition: all 0.6s;
  z-index: -1;
}
.open .circle {
  transform: scale(0.02);
  transform-origin: 40% 40%;
  background: white;
  border-color: white;
}
.open .handle:after {
  transform-origin: center center;
  transform: rotate(90deg);
}

javascript代码

let input = document.querySelector("input");
let search = document.querySelector(".search-icon");
let handle = document.querySelector(".handle");

input.addEventListener("input", () => {
  input.value === ""
    ? search.classList.remove("open")
    : search.classList.add("open");
});

handle.addEventListener("click", () => {
  input.value = "";
  search.classList.remove("open");
});

阅读更多

三连博主,每天分享一个编程小技巧!

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值