【后端学前端】第三天 css动画 动态搜索框(定位、动态设置宽度)

1、学习信息

视频地址:css动画 动态搜索框(定位、动态设置宽度)_哔哩哔哩_bilibili

2、源码

复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test3</title>
    <link rel="stylesheet" href="http://at.alicdn.com/t/c/font_4156699_i9rfozb6ac.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            width: 100vw;
            height: 100vh;
            background: linear-gradient(to bottom, skyblue, #003462);
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .searchBar {
            width: 400px;
            height: 60px;
            background-color: #ffffff;
            box-shadow: 0 0 10px rgba(0, 0, 0, .4);
            border-radius: 60px;
            position: relative;
            overflow: hidden;
            transition: .5s;
        }
        .icon {
            width: 60px;
            height: 60px;
            display: flex;
            justify-content: center;
            align-items: center;
            cursor: pointer;
        }
        .icon i{
            color: dodgerblue;
            font-size: 30px;
        }
        .textInput {
            width: 320px;
            height: 60px;
            position: absolute;
            top: 0;
            left: 60px;
            display: flex;
            justify-content: center;
            align-items: center;
            background-color: blue;
        }
        .textInput input {
            width: 100%;
            height: 100%;
            border: none;
            outline: none;
            font-size: 18px;
        }
        .clear {
            width: 15px;
            height: 15px;
            position: absolute;
            right: 22%;
            top: 50%;
            transform: translateY(-50%);
            cursor: pointer;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .clear i{
            color: #999;
        }
        .goBtn {
            width: 12%;
            height: 60%;
            position: absolute;
            top: 20%;
            right: 0;
            border-radius: 8px;
            outline: none;
            border: none;
            color: #ffffff;
            box-shadow: 0 0 2px rgba(0, 0, 0, .4);
            background: linear-gradient(skyblue, deepskyblue);
            cursor: pointer;
        }
        .searchBar{
            width: 60px;
        }
        .changeWidth{
            width: 400px;
        }
    </style>
    <script>

        window.onload =()=>{
            let searchIcon = document.querySelector(".icon")
            let clearIcon = document.querySelector(".clear")
            let searchBar = document.querySelector(".searchBar")
            let inp = document.querySelector(".inp")

            searchIcon.addEventListener("click",()=>{
                searchBar.classList.toggle("changeWidth")
            })
            clearIcon.addEventListener("click",()=>{
                inp.value=""
            })

        }
    </script>
</head>
<body>
    <div class="searchBar">
        <div class="icon"> <i class="iconfont icon-sousuoxiao"></i></div>
        <div class="textInput">
            <input class="inp" type="text" placeholder="请输入搜索关键字">
            <button class="goBtn">go</button>
            <div class="clear"><i class="iconfont icon-close"></i></div>
        </div>
    </div>
</body>
</html>

3、怎么实现拉出

通过对大的容器动态设置宽度按,也就是

复制代码
searchBar 的宽度设置为400px

这里学到一个注意点:在class后的属性会覆盖先写的属性

searchBar的最早宽度是400px,在后面又新定义了一个searchBar的样式为60px

4、 关于<i>

看教程,<i> 定义与文本中其余部分不同的部分,并把这部分文本呈现为斜体文本。

但是在代码中却用图标,在知乎也看到了类似的问题

问题地址:为什么大家都用i标签<i></i>用作小图标? - 知乎

貌似这样不对,但是大家都这样用

5、关于布局

布局一直我想学习的,一直也没总结出什么

找了一个文章【CSS】CSS布局解决方案(终结版) - 掘金

1.2 页面整体布局

  1. 先分析行模块
  2. 如果一行当中有多个模块,一定要放在同一个父模块中

1.3 单个模块布局

  1. 先给模块宽高背景颜色
  2. 然后再分析模块的文本属性背景其他属性

CSS书写顺序:

  • 位置属性(position, top, right, z-index, display, float等)
  • 大小(width, height, padding, margin)
  • 文本(font, line-height, letter-spacing, color- text-align等)
  • 背景(background, border等)
  • 其他(animation, transition等)

6、缩起来只显示搜索

复制代码
        .textInput {
            width: 320px;
            height: 60px;
            position: absolute; /*当被设置了绝对定位的元素,在文档流中是不占据空间的*/
            top: 0;
            left: 60px;
            display: flex;
            justify-content: center;
            align-items: center;
            background-color: blue;
        }

position: absolute; /*当被设置了绝对定位的元素,在文档流中是不占据空间的*/

textInput 不占据空间,最终searchBar的空间会全部给到icon

相关推荐
Amos_Web8 分钟前
通过 Rspack 源码学 Rust(七):Copy 与 Clone,什么时候复制,什么时候移动
前端
map_3d_vis13 分钟前
异步加载百度地图:callback、Promise 封装和 jsapi-loader(JSAPI 4.0)
前端·javascript·性能优化·百度地图
用户479492835691524 分钟前
开源突破 10 万+ Star:Archify v3.3.2 深度拆解——超越 Mermaid 的离线架构诊断、1080p Story 视频与 PR 变更可视化
前端·后端·agent
Amos_Web26 分钟前
Rspack 源码解析(三十七):RealContentHashPlugin 如何用 Aho-Corasick 替换真实的 contenthash
前端
用户479492835691539 分钟前
2026 流程图与架构图工具选型对比:draw.io、ProcessOn、Mermaid、Excalidraw、Archify 分别适合谁?
前端·后端·agent
恋猫de小郭41 分钟前
Flutter 的FFI/JNI 插件终于能区分不同的 FlutterEngine 了
android·前端·flutter
秋天的一阵风1 小时前
🧠2026主流知识库技术对比:RAG、MD+grep、LLM Wiki优劣与落地场景
前端·人工智能·ai编程
北城笑笑16 小时前
Vue 112 ,下篇:前端 dist 还原成 Vue3 + Vite 项目( krpano 全景接入与页面适配实战,Vue2 → Vue3 )
前端·javascript·vue.js·3d·前端框架·vue·webgl
我叫黑大帅16 小时前
搜“张三”把备注也带出来了?不用加高级筛选,如何让一个搜索框实现「选项直达」!
前端·javascript·面试