美文网首页网页前端后台技巧(CSS+HTML)码农的世界让前端飞
web前端入门到实战:纯css实现输入框placeholder动

web前端入门到实战:纯css实现输入框placeholder动

作者: 560b7bb7b879 | 来源:发表于2019-10-08 15:02 被阅读0次

    背景

    话不多说,我们能否用纯css实现以下效果:

    答案是肯定的。

    借助css:placeholder-shown :valid :invalid伪类及html5 input pattern 属性就可以实现,:placeholder-shown伪类目前兼容性如下:

    直接上代码!

    html:

    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width">
      <title>JS Bin</title>
    </head>
    <body>
      <div class="input-fill-box">
        <input class="input-fill" placeholder="邮箱" pattern="^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$" required>
        <a href="javascript:" class="clear">close</a>
        <label class="input-label">邮箱</label>
    </div>
    </body>
    </html>
    
    

    css:

    .input-fill{
      width: 100%;
      margin: 0;
      font-size: 16px;
      line-height: 1.5;
      outline: none;
      padding: 20px 16px 6px;
      border: 1px solid transparent;
       background: #f5f5fa;
      border-radius:10px;
      transition: border-color .25s;
    }
    .input-fill:placeholder-shown::placeholder {
        color: transparent;
    
    }
    .input-fill-box {
      width: 50%;
        position: relative;
    }
    .input-label {
        position: absolute;
        left: 16px; top: 14px;
        pointer-events: none;
        color:#BEC1D9;
       padding: 0 2px;
        transform-origin: 0 0;
        pointer-events: none;
        transition: all .25s;
    }
    .input-fill:not(:placeholder-shown) ~ .input-label,
    .input-fill:focus ~ .input-label {
        transform: scale(0.75) translate(0px, -14px);    
    }
    .input-fill:focus{
      border: 2px solid #1d31aa;
    }
    
    .clear{
      position:absolute;
      top:10px;
      right:-20px;
       display: none;
        transition: all .25s;
    }
    .input-fill::-ms-clear { display: none; }
    .input-fill:not(:placeholder-shown) + .clear { display: inline; }
    
    .input-fill:valid {
     border-color: green;
     box-shadow: inset 5px 0 0 green;
    }
    .input-fill:not(:placeholder-shown):invalid {
     border-color: red;
     box-shadow: inset 5px 0 0 red;
    }
    

    自己是一个6年的前端工程师,希望本文对你有帮助!

    这里推荐一下我的前端学习交流扣qun:731771211 ,里面都是学习前端的,如果你想制作酷炫的网页,想学习编程。自己整理了一份2019最全面前端学习资料,从最基础的HTML+CSS+JS【炫酷特效,游戏,插件封装,设计模式】到移动端HTML5的项目实战的学习资料都有整理,送给每一位前端小伙伴,每天分享技术

    点击:加入

    相关文章

      网友评论

        本文标题:web前端入门到实战:纯css实现输入框placeholder动

        本文链接:https://www.haomeiwen.com/subject/hvpkuctx.html