JavaScript仿京東搜索框?qū)嵗?/h1>
更新時間:2022年03月07日 10:00:06 作者:Cloud%
這篇文章主要為大家詳細(xì)介紹了JavaScript仿京東搜索框?qū)嵗?,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
馬上就到雙十一了,我們在京東淘寶購物,瘋狂剁手的同時,有沒有注意到京東的搜索框呢,除了能進(jìn)行搜索內(nèi)容以外,它的樣式又是如何實現(xiàn)的呢?
下面就分析一下如何實現(xiàn)仿京東的搜索框。
核心分析:
JavaScript部分:
1、當(dāng)文本框獲取焦點的時候,div中的字體顏色變?yōu)閞gb(200,200,200);
2、當(dāng)文本框失去焦點事件發(fā)生時,div中的字體顏色變成原來的樣式#989898;
3、當(dāng)文本框輸入內(nèi)容時,div的屬性變?yōu)?none,表現(xiàn)效果為文字消失;
4、當(dāng)清除文本框里面內(nèi)容時,divdiv的屬性變?yōu)?inline-block,表現(xiàn)效果為文字消失;
因為是在文本框里面顯示出來的內(nèi)容,改變的是表單元素,判斷文本框里面是否有輸入內(nèi)容,判斷的依據(jù)是 表單的value值是否為 空字符串。
實現(xiàn)代碼:
<!DOCTYPE html>
<html lang="en">
<head>
? ? <meta charset="UTF-8">
? ? <title>仿京東搜索框</title>
? ? <style>
? ? *{
? ? ? ? margin: 0;padding:0;
? ? }
? ? .from{
? ? ? ? border:2px solid #e2231a;
? ? ? ? width:490px;
? ? ? ? height:36px;
? ? ? ? position:relative;
? ? ? ? margin:100px auto;
? ? ? ? font-size: 12px;
? ? }
? ? .text{
? ? ? ? position:absolute;
? ? ? ? line-height: 36px;
? ? ? ? left:27px;
? ? ? ? color:#989898;
? ? ? ? z-index:-1;
? ? }
? ? .search{
? ? ? ? position:absolute;
? ? ? ? left:22px;
? ? ? ? width:430px;
? ? ? ? height:34px;
? ? ? ? outline:none;
? ? ? ? border:1px solid transparent;
? ? ? ? background:transparent;
? ? ? ? line-height: 34px;
? ? ? ? overflow: hidden;
? ? }
? ? .button{
? ? ? ? position:absolute;
? ? ? ? right:0px;
? ? ? ? width:58px;
? ? ? ? height:36px;
? ? ? ? background-color: #e2231a;
? ? ? ? border:1px solid transparent;
? ? ? ? margin:auto;
? ? ? ? outline:none;
? ? ? ? cursor: pointer;
? ? }
? ? button:hover{
? ? ? ? background-color: #c81623;
? ? }
? ? span img{
? ? ? ? position:absolute;
? ? ? ? right:65px;
? ? }
? ? </style>
</head>
? ? <div class='from'>
? ? ? ? <div class='text'>暗夜游戲本</div>
? ? ? ? <input type="text" class="search" value=''>
? ? ? ? <span class='photo' title="未選擇取任何文件">
? ? ? ? ? ? <img src="camera.png" alt="">
? ? ? ? </span>
? ? ? ? <button class='button'>
? ? ? ? ? ? <i><img src="search.png" ?alt=""></i>
? ? ? ? </button>
? ? </div>
<body>
? ? <script>
? ? var div = document.querySelector('.from');
? ? var input = document.querySelector('.search');
? ? var text = document.querySelector('.text');
? ? input.onfocus = function(){
? ? ? ? text.style.color = 'rgb(200,200,200)'
? ? }
? ? input.onblur = function(){
? ? ? ? text.style.color = '#989898'
? ? }
? ? input.oninput = function(){
? ? ? ? text.style.display = 'none';
? ? if (input.value == '') {
? ? ? ? text.style.display = 'inline-block';
? ? }; ? ?}
? ? </script>
</body>
</html>
顯示效果:
1、未觸發(fā)事件的狀態(tài)

2、輸入框里獲取焦點的狀態(tài)

3、輸入框里輸入內(nèi)容

4、刪除里面內(nèi)容后

5、CSS樣式效果(hover)

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
-
僅用[]()+!等符號就足以實現(xiàn)幾乎任意Javascript代碼
僅用一些符號就足以實現(xiàn)幾乎任意Javascript代碼的方法,比較怪異,對于特殊需要可能用得到。 2010-03-03
-
探索export導(dǎo)出一個字面量會報錯export?default不會報錯
這篇文章主要為大家介紹了export導(dǎo)出一個字面量會報錯而export?default不會報錯的問題探索解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪 2024-01-01
最新評論
馬上就到雙十一了,我們在京東淘寶購物,瘋狂剁手的同時,有沒有注意到京東的搜索框呢,除了能進(jìn)行搜索內(nèi)容以外,它的樣式又是如何實現(xiàn)的呢?
下面就分析一下如何實現(xiàn)仿京東的搜索框。
核心分析:
JavaScript部分:
1、當(dāng)文本框獲取焦點的時候,div中的字體顏色變?yōu)閞gb(200,200,200);
2、當(dāng)文本框失去焦點事件發(fā)生時,div中的字體顏色變成原來的樣式#989898;
3、當(dāng)文本框輸入內(nèi)容時,div的屬性變?yōu)?none,表現(xiàn)效果為文字消失;
4、當(dāng)清除文本框里面內(nèi)容時,divdiv的屬性變?yōu)?inline-block,表現(xiàn)效果為文字消失;
因為是在文本框里面顯示出來的內(nèi)容,改變的是表單元素,判斷文本框里面是否有輸入內(nèi)容,判斷的依據(jù)是 表單的value值是否為 空字符串。
實現(xiàn)代碼:
<!DOCTYPE html>
<html lang="en">
<head>
? ? <meta charset="UTF-8">
? ? <title>仿京東搜索框</title>
? ? <style>
? ? *{
? ? ? ? margin: 0;padding:0;
? ? }
? ? .from{
? ? ? ? border:2px solid #e2231a;
? ? ? ? width:490px;
? ? ? ? height:36px;
? ? ? ? position:relative;
? ? ? ? margin:100px auto;
? ? ? ? font-size: 12px;
? ? }
? ? .text{
? ? ? ? position:absolute;
? ? ? ? line-height: 36px;
? ? ? ? left:27px;
? ? ? ? color:#989898;
? ? ? ? z-index:-1;
? ? }
? ? .search{
? ? ? ? position:absolute;
? ? ? ? left:22px;
? ? ? ? width:430px;
? ? ? ? height:34px;
? ? ? ? outline:none;
? ? ? ? border:1px solid transparent;
? ? ? ? background:transparent;
? ? ? ? line-height: 34px;
? ? ? ? overflow: hidden;
? ? }
? ? .button{
? ? ? ? position:absolute;
? ? ? ? right:0px;
? ? ? ? width:58px;
? ? ? ? height:36px;
? ? ? ? background-color: #e2231a;
? ? ? ? border:1px solid transparent;
? ? ? ? margin:auto;
? ? ? ? outline:none;
? ? ? ? cursor: pointer;
? ? }
? ? button:hover{
? ? ? ? background-color: #c81623;
? ? }
? ? span img{
? ? ? ? position:absolute;
? ? ? ? right:65px;
? ? }
? ? </style>
</head>
? ? <div class='from'>
? ? ? ? <div class='text'>暗夜游戲本</div>
? ? ? ? <input type="text" class="search" value=''>
? ? ? ? <span class='photo' title="未選擇取任何文件">
? ? ? ? ? ? <img src="camera.png" alt="">
? ? ? ? </span>
? ? ? ? <button class='button'>
? ? ? ? ? ? <i><img src="search.png" ?alt=""></i>
? ? ? ? </button>
? ? </div>
<body>
? ? <script>
? ? var div = document.querySelector('.from');
? ? var input = document.querySelector('.search');
? ? var text = document.querySelector('.text');
? ? input.onfocus = function(){
? ? ? ? text.style.color = 'rgb(200,200,200)'
? ? }
? ? input.onblur = function(){
? ? ? ? text.style.color = '#989898'
? ? }
? ? input.oninput = function(){
? ? ? ? text.style.display = 'none';
? ? if (input.value == '') {
? ? ? ? text.style.display = 'inline-block';
? ? }; ? ?}
? ? </script>
</body>
</html>顯示效果:
1、未觸發(fā)事件的狀態(tài)

2、輸入框里獲取焦點的狀態(tài)

3、輸入框里輸入內(nèi)容

4、刪除里面內(nèi)容后

5、CSS樣式效果(hover)

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
僅用[]()+!等符號就足以實現(xiàn)幾乎任意Javascript代碼
僅用一些符號就足以實現(xiàn)幾乎任意Javascript代碼的方法,比較怪異,對于特殊需要可能用得到。2010-03-03
探索export導(dǎo)出一個字面量會報錯export?default不會報錯
這篇文章主要為大家介紹了export導(dǎo)出一個字面量會報錯而export?default不會報錯的問題探索解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01

