V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
zxCoder
V2EX  ›  问与答

css transition 问题请教

  •  
  •   zxCoder · Nov 12, 2021 · 1338 views
    This topic created in 1627 days ago, the information mentioned may be changed or developed.
    .test{
        transition: width 1s;
        &:hover{
          width: 400px;
        }
      }
      
      
    <input type="text"  class="test"/>
    

    怎么让过渡效果起作用呢

    4 replies    2021-11-12 10:07:36 +08:00
    otakustay
        1
    otakustay  
       Nov 12, 2021
    width 默认是 auto ,但 transition 无法基于 auto 搞,用 min-width 搞 0-400 的 transition 的话会有“启动延时感”,你要的这个功能我理解是无法用纯 CSS 完美实现的
    otakustay
        2
    otakustay  
       Nov 12, 2021   ❤️ 1
    <!DOCTYPE html>
    <html>
    <head>
    <style>
    .test{
    transition: min-width 1s linear;
    min-width: 0;
    }

    .test:hover{
    min-width: 400px;
    }
    </style>
    </head>

    <input type="text" class="test"/>
    </html>

    这个是 min-width 的做法,会有一个延迟(从 0 到当前 width 的过程看不到)
    icedwatermelon
        3
    icedwatermelon  
       Nov 12, 2021
    `
    .test-wrapper{
    transition: width 1s;
    width:100px;
    &:hover{
    width: 400px;
    }
    .test
    }


    <div class="test-wrapper">
    <input type="text" class="test"/>
    </div>

    `
    icedwatermelon
        4
    icedwatermelon  
       Nov 12, 2021   ❤️ 1
    @icedwatermelon
    .test-wrapper{
    transition: width 1s;
    width:100px;
    &:hover{
    width: 400px;
    }
    .test{
    width:100%
    }


    <div class="test-wrapper">
    <input type="text" class="test"/>
    </div>
    在外面包一个 div 好像可以
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3283 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 13:21 · PVG 21:21 · LAX 06:21 · JFK 09:21
    ♥ Do have faith in what you're doing.