搜索框html代码 + 平滑展开

图片展示:

未展开的

 

展开的
<!DOCTYPE html>
<html>
<head>
  <title>搜索框示例</title>
  <style>
  
    @font-face {
      font-family: "1";
      font-weight: 400;
      src: url("//at.alicdn.com/wf/webfont/t6dUpf78QV1c/b5zii7nYsuGM.woff2") format("woff2"),
           url("//at.alicdn.com/wf/webfont/t6dUpf78QV1c/MhBYWdtT6YRn.woff") format("woff");
      font-display: swap;
    }

    .search-container {
      text-align: center;
      margin-top: 20px;
    }

    .search-input-container {
      position: relative;
      display: inline-block;
      overflow: hidden;
      transition: width 0.3s;
      width: 100px;
    }

    .search-input-container.expand {
      width: 300px;
    }

    .search-icon {
      position: absolute;
      top: 50%;
      left: 10px;
      transform: translateY(-50%);
      fill: #ccc;
    }

    .search-text-after-icon::after {
      content: "|";
      position: absolute;
      top: 50%;
      right: 9px;
      transform: translateY(-50%);
      font-size: 15px;
      color: #ccc;
    }

    #search-input {
      padding: 10px 11px 10px 31px; /* 调整右侧内边距为10px */
      width: calc(100% - 50px); /* 减去右侧图标和竖线的宽度 */
      border: 2px solid #ccc;
      border-radius: 5px;
      font-size: 15.4px;
      font-family: "1", Arial, sans-serif;
      outline: none;
    }

  </style>
</head>
<body>
  <div class="search-container">
    <div class="search-input-container" onclick="expandSearch()">
      <svg t="1703847659003" class="icon search-icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="3205" width="21" height="21">
  <path d="M639.800062 0.15995c-211.997751 0-383.880037 171.882287-383.880037 383.880038 0 64.811746 16.218932 125.816682 44.626054 179.399937l-266.924586 266.860606 0.22393 0.19194A112.444861 112.444861 0 0 0 0 910.979319a112.892721 112.892721 0 0 0 112.892721 112.892721c31.510153 0 59.949266-12.987941 80.454858-33.877413l-0.06398-0.06398 266.828616-266.796626c53.615245 28.503093 114.716151 44.786004 179.687847 44.786004 211.997751 0 383.880037-171.882287 383.880038-383.880037s-171.882287-383.880037-383.880038-383.880038zM153.392065 950.007123a55.982505 55.982505 0 0 1-40.499344 17.370571 56.430366 56.430366 0 0 1-56.462355-56.430365c0-15.931022 6.717901-30.166573 17.370571-40.499344l-0.28791-0.25592 258.031365-258.031365a387.111028 387.111028 0 0 0 79.783068 79.911028l-257.935395 257.935395zM639.800062 703.972009c-176.648797 0-319.900031-143.283224-319.900031-319.900031 0-176.648797 143.251234-319.900031 319.900031-319.900032 176.616807 0 319.900031 143.251234 319.900032 319.900032 0 176.616807-143.283224 319.900031-319.900032 319.900031z" fill="#333333" p-id="3206"></path>
  <path d="M639.800062 160.109966a223.930022 223.930022 0 0 0-223.930021 223.930022 15.995002 15.995002 0 0 0 31.990003 0 191.940019 191.940019 0 0 1 191.940018-191.940019 15.995002 15.995002 0 0 0 0-31.990003z" fill="#333333" p-id="3207"></path>
</svg>

      <input type="text" id="search-input" placeholder="搜索..." class="search-text-after-icon">
    </div>
  </div>

  <script>
    var searchInputContainer = document.querySelector(".search-input-container");
    var searchInput = document.getElementById("search-input");

    function expandSearch() {
      searchInputContainer.classList.add("expand");
      searchInput.focus();
    }

    function collapseSearch() {
      searchInputContainer.classList.remove("expand");
    }

    searchInput.addEventListener("keyup", function(event) {
      if (event.keyCode === 13) {
        event.preventDefault();
        performSearch();
      }
    });

    searchInput.addEventListener("blur", function() {
      setTimeout(collapseSearch, 40);
    });

    function performSearch() {
      var searchText = searchInput.value;
      console.log("搜索:" + searchText);
    }
  </script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值