css實現(xiàn)input搜索框展開動畫

1.實現(xiàn)效果

input1.gif

2.實現(xiàn)原理

CSS 選擇器 CSS 中,選擇器是選取需設置樣式的元素的模式。

CSS :focus 選擇器:
一個輸入字段獲得焦點時選擇的樣式,:focus選擇器用于選擇具有焦點的元素唆鸡,接受鍵盤事件或其他用戶輸入的元素。

CSS :invalid 選擇器:
:invalid 選擇器用于在表單元素中的值是非法時設置指定樣式。 :invalid 選擇器只作用于能指定區(qū)間值的元素贮尖,例如 input 元素中的 min 和 max 屬性,及正確的 email 字段, 合法的數(shù)字字段等趁怔。

CSS :valid 選擇器:
:valid 選擇器在表單元素的值需要根據(jù)指定條件驗證時設置指定樣式湿硝。:valid 選擇器只作用于能指定區(qū)間值的元素薪前,例如 input 元素中的 min 和 max 屬性,及正確的 email 字段, 合法的數(shù)字字段等关斜。

element+element 選擇器(相鄰兄弟選擇器):可選擇緊接在另一個元素后的元素示括,且二者有相同父元素。
例:h1+p{}:選擇緊跟 h1元素的首個 p元素痢畜。

element1~element2 選擇器:
例:p ~ ul:選擇前面有p 元素的每個 ul元素垛膝。

CSS3 :not 選擇器::not(selector) 選擇器匹配每個元素是不是指定的元素/選擇器。

:placeholder-shown:使用此偽類來設置當前顯示占位符文本的輸入的樣式丁稀。將樣式應用于具有占位符文本的 input或 textarea吼拥。:placeholder-showd必須具有占位符(placeholder),當輸入文本不為空時候线衫,動態(tài)設置input框樣式凿可。

input 的required 屬性:
required 屬性是一個布爾屬性。required 屬性規(guī)定必需在提交表單之前填寫輸入字段授账。required 屬性適用于下面的 input 類型:text枯跑、search、url白热、tel敛助、email、password棘捣、date pickers辜腺、number、checkbox乍恐、radio 和 file评疗。

CSS font-style 屬性
font-style屬性指定文本的字體樣式。

描述
normal 默認值茵烈。瀏覽器顯示一個標準的字體樣式百匆。
italic 瀏覽器會顯示一個斜體的字體樣式。
oblique 瀏覽器會顯示一個傾斜的字體樣式呜投。
inherit 規(guī)定應該從父元素繼承字體樣式加匈。

3.實現(xiàn)步驟

3.1 示例1

1.gif
  • 先寫一個input標簽+搜索按鈕。
<div class=" input-box mb20">
    <input type="text" class="input" />
    <span class="span">搜</span>
</div>
.input-box {
    position: relative;
    display: inline-block;
}

.input {
    padding: 0 40px 0 20px;
    width: 160px;
    height: 38px;
    font-size: 14px;
    border: 1px solid #eee;
    border-radius: 40px;
    background: #eee;
    transition: width .5s;
    transition-delay: .1s;
}

.span {
    position: absolute;
    top: 4px;
    right: 5px;
    width: 30px;
    height: 30px;
    line-height: 30px;
    padding: 0;
    color: #969696;
    text-align: center;
    background: #222;
    border-radius: 50%;
    font-size: 15px;
    cursor: pointer;
}

  • 當焦點移入時仑荐,input:focus屬性雕拼,修改input的寬度,添加transition過渡效果粘招。通過加號選擇器啥寇,改變span標簽的樣式。
.input:focus {
    width: 280px;
    outline: none;
    box-shadow: none;
}
    
.input:focus+.span {
    background-color: pink;
    color: #fff;
}

3.2 示例2

2.gif
  • 先寫一個圓形按鈕+input輸入框。
<div class="btn-box mb20">
    <span>搜</span>
    <input type="text" placeholder=" " />
</div>
.btn-box {
    color: #fff;
    width: auto;
    border-radius: 25px;
    min-width: 50px;
    height: 50px;
    line-height: 50px;
    display: inline-block;
    position: relative;
    overflow: hidden;
    background-image: linear-gradient(315deg, #6772FF 0, #00F9E5 100%);
    background-size: 104% 104%;
    cursor: pointer;
}

.btn-box span {
    position: absolute;
    right: 0;
    top: 0;
    width: 50px;
    height: 50px;
    text-align: center;
    font-size: 18px;
    cursor: pointer;
}

.btn-box input {
    display: inline-block;
    background: 0 0;
    border: none;
    color: #fff;
    padding-left: 20px;
    line-height: 50px !important;
    height: 50px;
    box-sizing: border-box;
    vertical-align: 4px;
    font-size: 16px;
    width: 50px;
    transition: all .3s ease-in-out;
    font-style: italic;
    text-transform: uppercase;
    letter-spacing: 5px;
}

  • 懸浮按鈕時辑甜,修改input寬度衰絮,添加transition過渡效果。當輸入文字之后磷醋,即占位符為空猫牡,保持input寬度。
.btn-box:hover input {
    display: inline-block;
    width: 160px;
    padding-right: 50px
}

.btn-box input:not(:placeholder-shown) {
    display: inline-block;
    width: 160px;
    padding-right: 50px
}

3.3 示例3

3.gif
  • 先寫一個input輸入框+span文字+一條橫線(x軸縮放為0)邓线。
<div class="input-boxLine" >
    <input type="text" required />
    <div class="line"></div>
    <span>請輸入搜索內(nèi)容</span>
</div>
.input-boxLine {
    position: relative;
    width: 160px;
    height: 40px;
    margin-top: 10px;
}

.input-boxLine input {
    width: 100%;
    height: 100%;
    border: none;
    font-size: 17px;
    border-bottom: 1px solid #afaebd;
    color: #fff;
    font-style: italic;
    text-transform: uppercase;
    letter-spacing: 5px;
}

.input-boxLine span {
    position: absolute;
    bottom: 10px;
    left: 0px;
    color: #afaebd;
    pointer-events: none;
    transition: all 0.3s ease;
}

.input-boxLine .line {
    position: absolute;
    bottom: 0px;
    height: 2px;
    width: 100%;
    background-color: #ffaa7f;
    transform: scaleX(0);
    transition: all 0.3s ease;
}
  • 當input獲取焦點時(input:focus)或輸入的文字有效時(input:valid)淌友,通過~選擇器,將span標簽文字上移并設置傾斜骇陈,橫線縮放為1亩进,添加transition過渡效果。
.input-boxLine input:focus~span,
.input-boxLine input:valid~span {
    top: -10px;
    font-size: 12px;
    color: #ffaa7f;
    font-style: oblique;
}

.input-boxLine input:focus~.line,
.input-boxLine input:valid~.line {
    transform: scaleX(1);
}

4.完整代碼

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>input輸入框展開動畫</title>
    </head>
    <link rel="stylesheet" href="../common.css">
    <style>
        .mb20 {
            margin-bottom: 20px;
        }
        body {
            overflow: hidden;
            background: #222;
        }
        .input-box {
            position: relative;
            display: inline-block;
        }
        .input {
            padding: 0 40px 0 20px;
            width: 160px;
            height: 38px;
            font-size: 14px;
            border: 1px solid #eee;
            border-radius: 40px;
            background: #eee;
            transition: width .5s;
            transition-delay: .1s;
        }

        .span {
            position: absolute;
            top: 4px;
            right: 5px;
            width: 30px;
            height: 30px;
            line-height: 30px;
            padding: 0;
            color: #969696;
            text-align: center;
            background: #222;
            border-radius: 50%;
            font-size: 15px;
            cursor: pointer;
        }

        .input:focus {
            width: 280px;
            outline: none;
            box-shadow: none;
        }
        .input:focus+.span {
            background-color: pink;
            color: #fff;
        }
        /* 第二個 */
        .btn-box {
            color: #fff;
            width: auto;
            border-radius: 25px;
            min-width: 50px;
            height: 50px;
            line-height: 50px;
            display: inline-block;
            position: relative;
            overflow: hidden;
            background-image: linear-gradient(315deg, #6772FF 0, #00F9E5 100%);
            background-size: 104% 104%;
            cursor: pointer;
        }
        .btn-box span {
            position: absolute;
            right: 0;
            top: 0;
            width: 50px;
            height: 50px;
            text-align: center;
            font-size: 18px;
            cursor: pointer;
        }
        .btn-box input {
            display: inline-block;
            background: 0 0;
            border: none;
            color: #fff;
            padding-left: 20px;
            line-height: 50px !important;
            height: 50px;
            box-sizing: border-box;
            vertical-align: 4px;
            font-size: 16px;
            width: 50px;
            transition: all .3s ease-in-out;
            font-style: italic;
            text-transform: uppercase;
            letter-spacing: 5px;
        }
        .btn-box:hover input,
        .btn-box input:not(:placeholder-shown) {
            display: inline-block;
            width: 160px;
            padding-right: 50px
        }

        /* 第三個 */
        .input-boxLine {
            position: relative;
            width: 160px;
            height: 40px;
            margin-top: 10px;
        }
        .input-boxLine input {
            width: 100%;
            height: 100%;
            border: none;
            font-size: 17px;
            border-bottom: 1px solid #afaebd;
            color: #fff;
            font-style: italic;
            text-transform: uppercase;
            letter-spacing: 5px;
        }
        .input-boxLine span {
            position: absolute;
            bottom: 10px;
            left: 0px;
            color: #afaebd;
            pointer-events: none;
            transition: all 0.3s ease;
        }
        .input-boxLine .line {
            position: absolute;
            bottom: 0px;
            height: 2px;
            width: 100%;
            background-color: #ffaa7f;
            transform: scaleX(0);
            transition: all 0.3s ease;
        }
        .input-boxLine input:focus~span,
        .input-boxLine input:valid~span {
            top: -10px;
            font-size: 12px;
            color: #ffaa7f;
            font-style: oblique;
        }
        .input-boxLine input:focus~.line,
        .input-boxLine input:valid~.line {
            transform: scaleX(1);
        }
    </style>

    <body>
        <div>
            <section>
                <div class=" input-box mb20">
                    <input type="text" class="input" />
                    <span class="span">搜</span>
                </div>
            </section>
            <section>
                <div class="btn-box mb20">
                    <span>搜</span>
                    <input type="text" placeholder=" " />
                </div>
            </section>
            <section>
                <div class="input-boxLine" data-span='蘇蘇小蘇蘇'>
                    <input type="text" required />
                    <div class="line"></div>
                    <span>請輸入搜索內(nèi)容</span>
                </div>
            </section>
        </div>
    </body>
</html>

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末缩歪,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子谍憔,更是在濱河造成了極大的恐慌匪蝙,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,591評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件习贫,死亡現(xiàn)場離奇詭異逛球,居然都是意外死亡,警方通過查閱死者的電腦和手機苫昌,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,448評論 3 392
  • 文/潘曉璐 我一進店門颤绕,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人祟身,你說我怎么就攤上這事奥务。” “怎么了袜硫?”我有些...
    開封第一講書人閱讀 162,823評論 0 353
  • 文/不壞的土叔 我叫張陵氯葬,是天一觀的道長。 經(jīng)常有香客問我婉陷,道長帚称,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,204評論 1 292
  • 正文 為了忘掉前任秽澳,我火速辦了婚禮闯睹,結果婚禮上,老公的妹妹穿的比我還像新娘担神。我一直安慰自己楼吃,他們只是感情好,可當我...
    茶點故事閱讀 67,228評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著所刀,像睡著了一般衙荐。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上浮创,一...
    開封第一講書人閱讀 51,190評論 1 299
  • 那天忧吟,我揣著相機與錄音,去河邊找鬼斩披。 笑死溜族,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的垦沉。 我是一名探鬼主播煌抒,決...
    沈念sama閱讀 40,078評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼厕倍!你這毒婦竟也來了寡壮?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 38,923評論 0 274
  • 序言:老撾萬榮一對情侶失蹤讹弯,失蹤者是張志新(化名)和其女友劉穎况既,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體组民,經(jīng)...
    沈念sama閱讀 45,334評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡棒仍,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,550評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了臭胜。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片莫其。...
    茶點故事閱讀 39,727評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖耸三,靈堂內(nèi)的尸體忽然破棺而出乱陡,到底是詐尸還是另有隱情,我是刑警寧澤仪壮,帶...
    沈念sama閱讀 35,428評論 5 343
  • 正文 年R本政府宣布蛋褥,位于F島的核電站,受9級特大地震影響睛驳,放射性物質發(fā)生泄漏烙心。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,022評論 3 326
  • 文/蒙蒙 一乏沸、第九天 我趴在偏房一處隱蔽的房頂上張望淫茵。 院中可真熱鬧,春花似錦蹬跃、人聲如沸匙瘪。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,672評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽丹喻。三九已至薄货,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間碍论,已是汗流浹背谅猾。 一陣腳步聲響...
    開封第一講書人閱讀 32,826評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留鳍悠,地道東北人税娜。 一個月前我還...
    沈念sama閱讀 47,734評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像藏研,于是被迫代替她去往敵國和親敬矩。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,619評論 2 354

推薦閱讀更多精彩內(nèi)容