V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
zxCoder
V2EX  ›  问与答

css transition 问题请教

  •  
  •   zxCoder · 2021-11-12 09:54:01 +08:00 · 721 次点击
    这是一个创建于 889 天前的主题,其中的信息可能已经有所发展或是发生改变。
    .test{
        transition: width 1s;
        &:hover{
          width: 400px;
        }
      }
      
      
    <input type="text"  class="test"/>
    

    怎么让过渡效果起作用呢

    4 条回复    2021-11-12 10:07:36 +08:00
    otakustay
        1
    otakustay  
       2021-11-12 09:59:18 +08:00
    width 默认是 auto ,但 transition 无法基于 auto 搞,用 min-width 搞 0-400 的 transition 的话会有“启动延时感”,你要的这个功能我理解是无法用纯 CSS 完美实现的
    otakustay
        2
    otakustay  
       2021-11-12 09:59:40 +08:00   ❤️ 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  
       2021-11-12 10:06:11 +08:00
    `
    .test-wrapper{
    transition: width 1s;
    width:100px;
    &:hover{
    width: 400px;
    }
    .test
    }


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

    `
    icedwatermelon
        4
    icedwatermelon  
       2021-11-12 10:07:36 +08:00   ❤️ 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 好像可以
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5346 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 08:54 · PVG 16:54 · LAX 01:54 · JFK 04:54
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.