• React 简书项目实战【3】实现搜索框动画


    React 简书项目实战【3】实现搜索框动画

    添加判断变量

    header/index.js

    核心代码

    ...
    class Header extends Component {
      constructor(props) {
        super(props);
        this.state = {
          focused: true
        }
      }
        render() {
    ...
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    添加classname

    header/index.js

    核心代码

    搜索框和放大镜图标

    <SearchWrapper>
      <NavSearch
        className={this.state.focused ? 'focused' : ''}
      ></NavSearch>
      <i className={this.state.focused ? 'focused iconfont' : 'iconfont'}>&#xe62f;</i>
    </SearchWrapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    设置css

    header/style.js

    核心代码

    export const SearchWrapper = styled.div`
        position: relative;
        float: left;
        .iconfont {
            position: absolute;
            right: 5px;
            bottom: 5px;
            width: 30px;
            line-height: 30px;
            border-radius: 15px;
            text-align: center;
            &.focused {
                background: #777;
                color: #fff;
            }
        }
    `;
    export const NavSearch = styled.input.attrs({
        placeholder: '搜索'
    })`
        width: 160px;
        height: 38px;
        padding: 0 30px 0 20px;
        margin-top: 9px;
        margin-left: 20px;
        box-sizing: border-box;
        border: none;
        outline: none;
        border-radius: 19px;
        background: #eee;
        font-size: 14px;,
        color: #666;
        &::placeholder {
            color: #999;
        }
        &.focused {
            width: 240px;
        }
    `;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    变化效果

    image-20220628021702949

    image-20220628021648789

    绑定触发事件

    header/index.js

    核心代码

    <NavSearch
      className={this.state.focused ? 'focused' : ''}
      onFocus={this.handleInputFocus}
      onBlur={this.handleInputBlur}
    ></NavSearch>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    handleInputFocus() {
      this.setState({
        focused: true
      })
    }
    handleInputBlur() {
      this.setState({
        focused: false
      })
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    实现动画

    下载react-transition-group

    image-20220628023531060

    添加CSSTransition标签

    header/index.js

    核心代码

    引入

    import { CSSTransition } from 'react-transition-group'
    
    • 1
    <SearchWrapper>
      <CSSTransition
        in={this.state.focused}
        timeout={200}
        classNames="slide"
      >
        <NavSearch
          className={this.state.focused ? 'focused' : ''}
          onFocus={this.handleInputFocus}
          onBlur={this.handleInputBlur}
        ></NavSearch>
      </CSSTransition>
      <i className={this.state.focused ? 'focused iconfont' : 'iconfont'}>&#xe62f;</i>
    </SearchWrapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    添加动画的css

    header/style.js

    核心代码

    .slide-enter {
        transition: all .2s ease-out;
    }
    .slide-enter-active {
        width: 240px;
    }
    .slide-exit {
        transition: all .2s ease-out;
    }
    .slide-exit-active {
        width: 160px;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    整体代码

    header/style.js

    import styled from 'styled-components';
    import logoPic from '../../statics/logo.png'
    export const HeaderWrapper = styled.div`
        position: relative;
        height: 58px;
        border-bottom: 1px solid #f0f0f0;
    `;
    export const Logo = styled.a.attrs({href:'/'})`
        position: absolute;
        top: 0;
        left: 0;
        display: block;
        width: 100px;
        height: 56px;
        background: url(${logoPic});
        background-size: contain;
    `;
    export const Nav = styled.div`
        width: 960px;
        height: 100%;
        padding-right: 70px;
        box-sizing: border-box;
        margin: 0 auto;
    `;
    export const NavItem = styled.div`
        line-height: 56px;
        padding: 0 15px;
        font-size: 17px;
        color: #333;
        &.left {
            float: left;
        }
        &.right {
            float: right;
            color: #969696;
        }
        &.active {
            color: #ea6f5a;
        }
    `;
    export const SearchWrapper = styled.div`
        position: relative;
        float: left;
        .slide-enter {
            transition: all .2s ease-out;
        }
        .slide-enter-active {
            width: 240px;
        }
        .slide-exit {
            transition: all .2s ease-out;
        }
        .slide-exit-active {
            width: 160px;
        }
        .iconfont {
            position: absolute;
            right: 5px;
            bottom: 5px;
            width: 30px;
            line-height: 30px;
            border-radius: 15px;
            text-align: center;
            &.focused {
                background: #777;
                color: #fff;
            }
        }
    `;
    export const NavSearch = styled.input.attrs({
        placeholder: '搜索'
    })`
        width: 160px;
        height: 38px;
        padding: 0 30px 0 20px;
        margin-top: 9px;
        margin-left: 20px;
        box-sizing: border-box;
        border: none;
        outline: none;
        border-radius: 19px;
        background: #eee;
        font-size: 14px;,
        color: #666;
        &::placeholder {
            color: #999;
        }
        &.focused {
            width: 240px;
        }
    `;
    
    export const Addition = styled.div`
        position: absolute;
        right: 0;
        top: 0;
        height: 56px;
    `;
    
    export const Button = styled.div`
        float: right;
        margin-top: 9px;
        margin-right: 20px;
        padding: 0 20px;
        line-height: 38px;
        border-radius: 19px;
        border: 1px solid #ec6149;
        font-size: 14px;
        &.reg {
            color: #ec6149;
        }
        &.writting {
            color: #fff;
            background: #ec6149;
        }
    `;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116

    header/index.js

    import React, { Component } from "react";
    import { CSSTransition } from 'react-transition-group'
    import {SearchWrapper,HeaderWrapper,Logo,Nav,NavItem,NavSearch,Addition,Button} from "./style";
    class Header extends Component {
      constructor(props) {
        super(props);
        this.state = {
          focused: false
        }
        this.handleInputFocus = this.handleInputFocus.bind(this);
        this.handleInputBlur = this.handleInputBlur.bind(this);
      }
        render() {
            return (
                <HeaderWrapper>
                    <Logo />
                    <Nav>
                        <NavItem className='left active'>首页</NavItem>
                        <NavItem className='left'>下载App</NavItem>
                        <NavItem className='right'>登录</NavItem>
                        <NavItem className='right'>
                            <i className="iconfont ">&#xe636;</i>
                        </NavItem>
    
                        <SearchWrapper>
                          <CSSTransition
                            in={this.state.focused}
                            timeout={200}
                            classNames="slide"
                          >
                            <NavSearch
                              className={this.state.focused ? 'focused' : ''}
                              onFocus={this.handleInputFocus}
                              onBlur={this.handleInputBlur}
                            ></NavSearch>
                          </CSSTransition>
                          <i className={this.state.focused ? 'focused iconfont' : 'iconfont'}>&#xe62f;</i>
                        </SearchWrapper>
                    </Nav>
                    <Addition>
                        <Button className='writting'>
                            <i className="iconfont ">&#xe600;</i>
                            写文章</Button>
                        <Button className='reg'>注册</Button>
                    </Addition>
                </HeaderWrapper>
            )
        }
      handleInputFocus() {
        this.setState({
          focused: true
        })
      }
      handleInputBlur() {
        this.setState({
          focused: false
        })
      }
    }
    
    export default Header;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
  • 相关阅读:
    Python+pytest接口自动化之参数关联
    YOLOv8最新改进系列:YOLOv8改进之添加注意力-ContextAggregation,有效涨点!!!
    低代码维格云甘特视图入门教程
    聚观早报 | 《三体》将于2023年上映;李恩祐加入京东董事会
    c语言:三个数排序(if-else实现)
    微信小程序 API 简介
    C++二叉树
    linux-任务计划和日志管理
    vue3.0--2.watch、vue3生命周期函数、Teleport、自定义事件、状态驱动的动态 CSS、Suspense
    计算机毕业设计(42)java小程序毕设作品之小说电子书阅读小程序系统
  • 原文地址:https://blog.csdn.net/weixin_42403632/article/details/127975194