JavaScript仿京東搜索框?qū)嵗?/h1>
更新時(shí)間:2022年03月07日 10:00:06 作者:Cloud%
這篇文章主要為大家詳細(xì)介紹了JavaScript仿京東搜索框?qū)嵗闹惺纠a介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
馬上就到雙十一了,我們?cè)诰〇|淘寶購(gòu)物,瘋狂剁手的同時(shí),有沒(méi)有注意到京東的搜索框呢,除了能進(jìn)行搜索內(nèi)容以外,它的樣式又是如何實(shí)現(xiàn)的呢?
下面就分析一下如何實(shí)現(xiàn)仿京東的搜索框。
核心分析:
JavaScript部分:
1、當(dāng)文本框獲取焦點(diǎn)的時(shí)候,div中的字體顏色變?yōu)閞gb(200,200,200);
2、當(dāng)文本框失去焦點(diǎn)事件發(fā)生時(shí),div中的字體顏色變成原來(lái)的樣式#989898;
3、當(dāng)文本框輸入內(nèi)容時(shí),div的屬性變?yōu)?none,表現(xiàn)效果為文字消失;
4、當(dāng)清除文本框里面內(nèi)容時(shí),divdiv的屬性變?yōu)?inline-block,表現(xiàn)效果為文字消失;
因?yàn)槭窃谖谋究蚶锩骘@示出來(lái)的內(nèi)容,改變的是表單元素,判斷文本框里面是否有輸入內(nèi)容,判斷的依據(jù)是 表單的value值是否為 空字符串。
實(shí)現(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、輸入框里獲取焦點(diǎn)的狀態(tài)

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

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

5、CSS樣式效果(hover)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:- js實(shí)現(xiàn)搜索框關(guān)鍵字智能匹配代碼
- 基于Vue.js實(shí)現(xiàn)簡(jiǎn)單搜索框
- JavaScript實(shí)現(xiàn)搜索框的自動(dòng)完成功能(一)
- JS實(shí)現(xiàn)仿google、百度搜索框輸入信息智能提示的實(shí)現(xiàn)方法
- JavaScript實(shí)現(xiàn)百度搜索框效果
- JS+Ajax實(shí)現(xiàn)百度智能搜索框
- 自動(dòng)完成的搜索框javascript實(shí)現(xiàn)
- JS實(shí)現(xiàn)京東首頁(yè)之頁(yè)面頂部、Logo和搜索框功能
- JS實(shí)現(xiàn)微信彈出搜索框 多條件查詢功能
- javascript搜索框效果實(shí)現(xiàn)方法
相關(guān)文章
-
uniapp獲取頁(yè)面高度與元素高度簡(jiǎn)單示例
在實(shí)際開發(fā)中我們會(huì)遇到不確定高度的情況,那么在uniapp中我們?nèi)绾潍@取區(qū)域的高度吶?這篇文章主要給大家介紹了關(guān)于uniapp獲取頁(yè)面高度與元素高度的相關(guān)資料,需要的朋友可以參考下 2023-09-09
-
Nest.js快速啟動(dòng)API項(xiàng)目過(guò)程詳解
這篇文章主要為大家介紹了Nest.js快速啟動(dòng)API項(xiàng)目過(guò)程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪 2022-10-10
-
前端進(jìn)行雙重身份驗(yàn)證的實(shí)現(xiàn)與思路詳解
在傳統(tǒng)的身份驗(yàn)證方案之上,雙重身份驗(yàn)證(Double?Token)作為一?種增強(qiáng)型認(rèn)證方法,逐漸成為現(xiàn)代前端應(yīng)用中的關(guān)鍵技術(shù),下面小編就來(lái)為大家詳細(xì)講講雙重身份驗(yàn)證的實(shí)現(xiàn)與思路吧 2025-02-02
-
帝國(guó)cms首頁(yè)列表頁(yè)實(shí)現(xiàn)點(diǎn)贊功能
這篇文章主要介紹了帝國(guó)cms首頁(yè)列表頁(yè)實(shí)現(xiàn)點(diǎn)贊功能的相關(guān)資料,需要的朋友可以參考下 2017-10-10
-
僅用[]()+!等符號(hào)就足以實(shí)現(xiàn)幾乎任意Javascript代碼
僅用一些符號(hào)就足以實(shí)現(xiàn)幾乎任意Javascript代碼的方法,比較怪異,對(duì)于特殊需要可能用得到。 2010-03-03
-
JS實(shí)現(xiàn)多物體運(yùn)動(dòng)
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)多物體運(yùn)動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下 2022-05-05
-
探索export導(dǎo)出一個(gè)字面量會(huì)報(bào)錯(cuò)export?default不會(huì)報(bào)錯(cuò)
這篇文章主要為大家介紹了export導(dǎo)出一個(gè)字面量會(huì)報(bào)錯(cuò)而export?default不會(huì)報(bào)錯(cuò)的問(wèn)題探索解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪 2024-01-01
-
Javascript的表單與驗(yàn)證-非空驗(yàn)證
JavaScript 可用來(lái)在數(shù)據(jù)被送往服務(wù)器前對(duì) HTML 表單中的這些輸入數(shù)據(jù)進(jìn)行驗(yàn)證。本文給大家介紹javascript的表單與驗(yàn)證-非空驗(yàn)證,對(duì)javascript表單驗(yàn)證相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧 2016-03-03
最新評(píng)論
馬上就到雙十一了,我們?cè)诰〇|淘寶購(gòu)物,瘋狂剁手的同時(shí),有沒(méi)有注意到京東的搜索框呢,除了能進(jìn)行搜索內(nèi)容以外,它的樣式又是如何實(shí)現(xiàn)的呢?
下面就分析一下如何實(shí)現(xiàn)仿京東的搜索框。
核心分析:
JavaScript部分:
1、當(dāng)文本框獲取焦點(diǎn)的時(shí)候,div中的字體顏色變?yōu)閞gb(200,200,200);
2、當(dāng)文本框失去焦點(diǎn)事件發(fā)生時(shí),div中的字體顏色變成原來(lái)的樣式#989898;
3、當(dāng)文本框輸入內(nèi)容時(shí),div的屬性變?yōu)?none,表現(xiàn)效果為文字消失;
4、當(dāng)清除文本框里面內(nèi)容時(shí),divdiv的屬性變?yōu)?inline-block,表現(xiàn)效果為文字消失;
因?yàn)槭窃谖谋究蚶锩骘@示出來(lái)的內(nèi)容,改變的是表單元素,判斷文本框里面是否有輸入內(nèi)容,判斷的依據(jù)是 表單的value值是否為 空字符串。
實(shí)現(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、輸入框里獲取焦點(diǎn)的狀態(tài)

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

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

5、CSS樣式效果(hover)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- js實(shí)現(xiàn)搜索框關(guān)鍵字智能匹配代碼
- 基于Vue.js實(shí)現(xiàn)簡(jiǎn)單搜索框
- JavaScript實(shí)現(xiàn)搜索框的自動(dòng)完成功能(一)
- JS實(shí)現(xiàn)仿google、百度搜索框輸入信息智能提示的實(shí)現(xiàn)方法
- JavaScript實(shí)現(xiàn)百度搜索框效果
- JS+Ajax實(shí)現(xiàn)百度智能搜索框
- 自動(dòng)完成的搜索框javascript實(shí)現(xiàn)
- JS實(shí)現(xiàn)京東首頁(yè)之頁(yè)面頂部、Logo和搜索框功能
- JS實(shí)現(xiàn)微信彈出搜索框 多條件查詢功能
- javascript搜索框效果實(shí)現(xiàn)方法
相關(guān)文章
uniapp獲取頁(yè)面高度與元素高度簡(jiǎn)單示例
在實(shí)際開發(fā)中我們會(huì)遇到不確定高度的情況,那么在uniapp中我們?nèi)绾潍@取區(qū)域的高度吶?這篇文章主要給大家介紹了關(guān)于uniapp獲取頁(yè)面高度與元素高度的相關(guān)資料,需要的朋友可以參考下2023-09-09
Nest.js快速啟動(dòng)API項(xiàng)目過(guò)程詳解
這篇文章主要為大家介紹了Nest.js快速啟動(dòng)API項(xiàng)目過(guò)程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
前端進(jìn)行雙重身份驗(yàn)證的實(shí)現(xiàn)與思路詳解
在傳統(tǒng)的身份驗(yàn)證方案之上,雙重身份驗(yàn)證(Double?Token)作為一?種增強(qiáng)型認(rèn)證方法,逐漸成為現(xiàn)代前端應(yīng)用中的關(guān)鍵技術(shù),下面小編就來(lái)為大家詳細(xì)講講雙重身份驗(yàn)證的實(shí)現(xiàn)與思路吧2025-02-02
帝國(guó)cms首頁(yè)列表頁(yè)實(shí)現(xiàn)點(diǎn)贊功能
這篇文章主要介紹了帝國(guó)cms首頁(yè)列表頁(yè)實(shí)現(xiàn)點(diǎn)贊功能的相關(guān)資料,需要的朋友可以參考下2017-10-10
僅用[]()+!等符號(hào)就足以實(shí)現(xiàn)幾乎任意Javascript代碼
僅用一些符號(hào)就足以實(shí)現(xiàn)幾乎任意Javascript代碼的方法,比較怪異,對(duì)于特殊需要可能用得到。2010-03-03
JS實(shí)現(xiàn)多物體運(yùn)動(dòng)
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)多物體運(yùn)動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
探索export導(dǎo)出一個(gè)字面量會(huì)報(bào)錯(cuò)export?default不會(huì)報(bào)錯(cuò)
這篇文章主要為大家介紹了export導(dǎo)出一個(gè)字面量會(huì)報(bào)錯(cuò)而export?default不會(huì)報(bào)錯(cuò)的問(wèn)題探索解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
Javascript的表單與驗(yàn)證-非空驗(yàn)證
JavaScript 可用來(lái)在數(shù)據(jù)被送往服務(wù)器前對(duì) HTML 表單中的這些輸入數(shù)據(jù)進(jìn)行驗(yàn)證。本文給大家介紹javascript的表單與驗(yàn)證-非空驗(yàn)證,對(duì)javascript表單驗(yàn)證相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-03-03

