HTML/CSS实现小米官网搜索框效果

效果图:

需求分析:

1、输入框焦点事件

onfocus:成为焦点, 点击输入框的时候,出现闪烁光标,此时可以输入内容。

onblur :失去焦点, 点击页面空白区域,光标消失。此时不可以输入内容。

2、获取元素

3、注册事件

2.1、onfocus:成为焦点, 点击输入框的时候,出现闪烁光标,此时可以输入内容

1)、显示ul

2)、自身边框改变 (通过新增search类名)

2.2、onblur :失去焦点, 点击页面空白区域,光标消失。此时不可以输入内容

1)、隐藏ul

2)、自身边框改变 (通过移除search类名)

源码如下:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    ul {
      list-style: none;
    }

    .mi {
      position: relative;
      width: 223px;
      margin: 100px auto;
    }

    .mi input {
      width: 223px;
      height: 48px;
      padding: 0 10px;
      font-size: 14px;
      line-height: 48px;
      border: 1px solid #e0e0e0;
      outline: none;
      transition: all 0.3s;
    }

    .mi .search {
      border: 1px solid #ff6700;
    }

    .result-list {
      display: none;
      position: absolute;
      left: 0;
      top: 48px;
      width: 223px;
      border: 1px solid #ff6700;
      border-top: 0;
      background: #fff;
    }

    .result-list a {
      display: block;
      padding: 6px 15px;
      font-size: 12px;
      color: #424242;
      text-decoration: none;
    }

    .result-list a:hover {
      background-color: #eee;
    }
  </style>
</head>

<body>
<div class="mi">
  <input type="search" placeholder="红米手机" />
  <ul class="result-list">
    <li><a href="#">全部商品</a></li>
    <li><a href="#">红米手机</a></li>
    <li><a href="#">小米10S</a></li>
    <li><a href="#">小米笔记本</a></li>
    <li><a href="#">小米手机</a></li>
    <li><a href="#">云米空调</a></li>
    <li><a href="#">云米智能机器人</a></li>
  </ul>
</div>

<script>
  /*
  1.输入框焦点事件
    onfocus:成为焦点, 点击输入框的时候,出现闪烁光标,此时可以输入内容
    onblur :失去焦点, 点击页面空白区域,光标消失。此时不可以输入内容
  2.小米官网
    成为焦点: (1)显示ul  (2)自身边框改变 (通过新增search类名)
    失去焦点: (1)隐藏ul  (2)自身边框改变 (通过移除search类名)
  3. this关键字 : 环境对象。 常用于函数中,代表当前函数的所属环境。
        说人话: 相当于中文的 ‘我’ 字, 谁说的这个字,就代表谁
  4.js中this规则: 谁调用这个函数,this就指向谁
        * 事件处理函数中this一般指向事件源自身
  */

  //1.获取元素
  let input = document.querySelector('input')
  let ul = document.querySelector('.result-list')

  //2.注册事件

  //2.1 onfocus:成为焦点, 点击输入框的时候,出现闪烁光标,此时可以输入内容
  input.onfocus = function(){
    console.log('你点我了,我出现光标了,此时可以输入文字')
    //(1)显示ul
    ul.style.display = 'block'
    //(2)自身边框改变 (通过新增search类名)
    this.classList.add('search')
  }

  //2.2 onblur :失去焦点, 点击页面空白区域,光标消失。此时不可以输入内容
  input.onblur = function(){
    console.log('你点其他地方了,我光标消失了,此时不可以输入文字')
    //(1)隐藏ul
    ul.style.display = 'none'
    //(2)自身边框改变 (通过移除search类名)
    this.classList.remove('search')
  }


</script>
</body>
</html>

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我可以提供一些指导。首先,你需要在HTML中用标签和结构创建页面的基本框架。然后,使用CSS样式来添加样式和布局,使其看起来像小米商城。 以下是一个简单的HTML结构示例: ```html <!DOCTYPE html> <html> <head> <title>小米商城</title> <meta charset="UTF-8"> <link rel="stylesheet" href="style.css"> </head> <body> <header> <nav> <ul> <li><a href="#">首页</a></li> <li><a href="#">手机</a></li> <li><a href="#">电视</a></li> <li><a href="#">笔本</a></li> <li><a href="#">家电</a></li> <li><a href="#">路由器</a></li> <li><a href="#">智能硬件</a></li> </ul> </nav> <div class="logo"> <a href="#"><img src="images/logo.png" alt="小米商城"></a> </div> <div class="search"> <form> <input type="text" placeholder="搜索商品"> <button type="submit">搜索</button> </form> </div> </header> <main> <section class="banner"> <img src="images/banner.jpg" alt="小米10"> </section> <section class="products"> <h2>热门产品</h2> <ul> <li> <a href="#"> <img src="images/product1.jpg" alt="小米电视"> <p>小米电视</p> <span>¥1999</span> </a> </li> <li> <a href="#"> <img src="images/product2.jpg" alt="小米手机"> <p>小米手机</p> <span>¥2999</span> </a> </li> <li> <a href="#"> <img src="images/product3.jpg" alt="小米路由器"> <p>小米路由器</p> <span>¥199</span> </a> </li> <li> <a href="#"> <img src="images/product4.jpg" alt="小米智能手表"> <p>小米智能手表</p> <span>¥999</span> </a> </li> </ul> </section> <section class="news"> <h2>新闻资讯</h2> <ul> <li> <a href="#"> <img src="images/news1.jpg" alt="小米10发布会"> <p>小米10发布会</p> </a> </li> <li> <a href="#"> <img src="images/news2.jpg" alt="小米电视新品发布"> <p>小米电视新品发布</p> </a> </li> <li> <a href="#"> <img src="images/news3.jpg" alt="小米智能家居"> <p>小米智能家居</p> </a> </li> </ul> </section> </main> <footer> <ul> <li><a href="#">关于小米</a></li> <li><a href="#">加入小米</a></li> <li><a href="#">投资者关系</a></li> <li><a href="#">企业社会责任</a></li> <li><a href="#">法律声明</a></li> <li><a href="#">友情链接</a></li> <li><a href="#">联系我们</a></li> <li><a href="#">常见问题</a></li> </ul> <p>© 2021 小米商城</p> </footer> </body> </html> ``` 然后,你需要在CSS中进行样式定义,例如: ```css body { font-family: Arial, sans-serif; margin: 0; padding: 0; } header { background-color: #f6f6f6; display: flex; justify-content: space-between; align-items: center; padding: 20px; } nav ul { list-style: none; margin: 0; padding: 0; display: flex; } nav ul li { margin-right: 20px; } nav ul li a { color: #333; text-decoration: none; } .logo img { max-height: 50px; } .search input[type="text"] { border: none; padding: 10px; border-radius: 5px 0 0 5px; } .search button[type="submit"] { background-color: #ff6700; color: #fff; border: none; padding: 10px; border-radius: 0 5px 5px 0; cursor: pointer; } .main { max-width: 1200px; margin: 0 auto; } .banner img { width: 100%; } .products h2 { font-size: 30px; margin-top: 30px; margin-bottom: 20px; } .products ul { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; } .products li { flex-basis: 25%; margin-bottom: 20px; padding: 10px; box-sizing: border-box; text-align: center; } .products li a { display: block; border: 1px solid #ccc; padding: 10px; border-radius: 5px; text-decoration: none; color: #333; } .products li a:hover { background-color: #f6f6f6; } .products li img { max-width: 100%; } .products li p { font-size: 18px; margin-top: 10px; margin-bottom: 5px; } .products li span { font-size: 16px; color: #f60; } .news h2 { font-size: 30px; margin-top: 30px; margin-bottom: 20px; } .news ul { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; } .news li { flex-basis: 33.33%; margin-bottom: 20px; padding: 10px; box-sizing: border-box; text-align: center; } .news li a { display: block; border: 1px solid #ccc; padding: 10px; border-radius: 5px; text-decoration: none; color: #333; } .news li a:hover { background-color: #f6f6f6; } .news li img { max-width: 100%; } .news li p { font-size: 18px; margin-top: 10px; margin-bottom: 5px; } footer { background-color: #f6f6f6; padding: 20px; display: flex; justify-content: space-between; align-items: center; } footer ul { list-style: none; margin: 0; padding: 0; display: flex; } footer ul li { margin-right: 20px; } footer ul li a { color: #333; text-decoration: none; } footer p { font-size: 14px; color: #999; margin: 0; } ``` 以上只是简单的示例,你可以根据小米商城的实际样式进行调整和完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈老说

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值