使用JQuery模仿實(shí)現(xiàn)淘寶搜索效果
最終實(shí)現(xiàn)效果如下

1、獲取用戶(hù)輸入的搜索關(guān)鍵詞
需要監(jiān)聽(tīng)輸入框的keyup事件
2、封裝getSuggestList函數(shù)
發(fā)JSONP請(qǐng)求,獲取內(nèi)容

3、渲染建立列表的UI結(jié)構(gòu)(模板引擎)
1、定義搜索建議列表
2、定義模板結(jié)構(gòu)

服務(wù)器響應(yīng)回來(lái)的數(shù)據(jù)(res)

把res直接給模板引擎

要對(duì)res里面的result進(jìn)行遍歷,一開(kāi)始取得

是result里面索引號(hào)為0的數(shù)據(jù)

3、定義渲染模板結(jié)構(gòu)的函數(shù)
4、搜索關(guān)鍵詞為空時(shí)隱藏搜索建議列表
5、實(shí)現(xiàn)輸入框的防抖

實(shí)現(xiàn)核心代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<!-- 導(dǎo)入頁(yè)面的基本樣式 -->
<link rel="stylesheet" href="./css/search.css" rel="external nofollow" />
<!-- 導(dǎo)入 jQuery -->
<script src="./lib/jquery.js"></script>
<script src="./lib/template-web.js"></script>
</head>
<body>
<div class="container">
<!-- Logo -->
<img src="./images/taobao_logo.png" alt="" class="logo" />
<div class="box">
<!-- tab 欄 -->
<div class="tabs">
<div class="tab-active">寶貝</div>
<div>店鋪</div>
</div>
<!-- 搜索區(qū)域(搜索框和搜索按鈕) -->
<div class="search-box">
<input type="text" id='ipt1' class="ipt" placeholder="請(qǐng)輸入要搜索的內(nèi)容" /><button class="btnSearch">
搜索
</button>
</div>
<!-- 搜索建議列表 -->
<div class="suggest-list"></div>
</div>
</div>
<script src="10.js"></script>
<!-- 模板結(jié)構(gòu) -->
<script type="text/html" id="tpl">
{{each result}}
<!-- 循環(huán)服務(wù)器響應(yīng)回來(lái)的數(shù)據(jù) -->
<div class="suggest-item">{{$value[0]}}</div>
<!-- 每循環(huán)一次,渲染一次搜索的建議項(xiàng) -->
{{/each}}
</script>
</body>
</html>$(function() {
//監(jiān)聽(tīng)文本框的keyup事件
$('#ipt1').on('keyup', function() {
//獲取文本框的輸入內(nèi)容,.trim去掉空格內(nèi)容
var keywords = $(this).val().trim()
//判斷輸入內(nèi)容是否為空
if (keywords.length <= 0) {
return $('.suggest-list').empty().hide()
}
//先判斷緩存中是否有數(shù)據(jù)
if (cacheObj[keywords]) {
return renderSuggestList(cacheObj[keywords])
}
//或如搜索建議列表
else {
// getSuggestList(keywords)
clearTimeout(timer) //再觸發(fā)keyup事件時(shí),立即清空timer
debounceSearch(keywords) //防抖+請(qǐng)求+渲染
}
})
//封裝函數(shù)
function getSuggestList(kw) {
//發(fā)起請(qǐng)求
$.ajax({
//指定請(qǐng)求的URL地址,q是用戶(hù)輸入的搜索關(guān)鍵詞
url: 'https://suggest.taobao.com/sug?q=' + kw,
dataType: 'JSONP',
//指定回調(diào)函數(shù),獲取建議列表的數(shù)據(jù)
success: function(res) {
console.log(res);
renderSuggestList(res)
}
})
}
// 定義渲染建議列表
function renderSuggestList(res) {
if (res.result.length <= 0) {
return $('.suggest-list').empty().hide()
}
//渲染模板結(jié)構(gòu)
var htmstr = template('tpl', res)
$('.suggest-list').html(htmstr).show()
//將搜索的結(jié)果,添加到緩存對(duì)象中
var k = $('#ipt1').val().trim() //獲取用戶(hù)輸入的數(shù)據(jù),當(dāng)作鍵
cacheObj[k] = res //需要將數(shù)據(jù)作為值進(jìn)行緩存
console.log(cacheObj);
}
var timer = null //防抖動(dòng)的timer
//1、定義全局緩存對(duì)象
var cacheObj = {}
function debounceSearch(keywords) { //定義防抖的函數(shù)
timer = setTimeout(function() {
//發(fā)起JSONP請(qǐng)求,通過(guò)一個(gè)延遲器之后再發(fā)起JSONP請(qǐng)求
getSuggestList(keywords)
}, 2000)
}
}).container {
display: flex;
flex-direction: column;
align-items: center;
font-size: 12px;
}
.logo {
width: 225px;
height: 65px;
margin: 50px 0;
}
.tabs {
display: flex;
}
.tabs>div {
width: 55px;
height: 23px;
line-height: 23px;
text-align: center;
cursor: pointer;
}
.tabs>div:hover {
text-decoration: underline;
color: #ff5700;
}
.tab-active {
background-color: #ff5700;
font-weight: bold;
color: white;
}
.tabs>.tab-active:hover {
color: white;
text-decoration: none;
}
.search-box {
display: flex;
align-items: center;
}
.search-box .ipt {
box-sizing: border-box;
width: 500px;
height: 34px;
line-height: 30px;
margin: 0;
padding: 0;
padding-left: 5px;
outline: none;
border: 2px solid #ff5700;
}
.btnSearch {
margin: 0;
height: 34px;
border: none;
background-color: #ff5700;
color: white;
letter-spacing: 1em;
text-align: center;
width: 90px;
padding-bottom: 5px;
outline: none;
cursor: pointer;
}
.btnSearch:hover {
opacity: 0.9;
}
.suggest-list {
display: none;
border: 1px solid #ccc;
}
.suggest-item {
height: 30px;
padding-left: 5px;
line-height: 30px;
}
.suggest-item:hover {
cursor: pointer;
background-color: #eee;
}以上就是使用JQuery模仿實(shí)現(xiàn)淘寶搜索效果的詳細(xì)內(nèi)容,更多關(guān)于JQuery淘寶搜索效果的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
jquery實(shí)現(xiàn)員工信息添加與刪除功能
這篇文章主要為大家詳細(xì)介紹了利用jquery制作簡(jiǎn)易的員工信息添加與刪除,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
jQuery實(shí)現(xiàn)簡(jiǎn)單的滑動(dòng)導(dǎo)航代碼(移動(dòng)端)
這篇文章主要介紹了jQuery實(shí)現(xiàn)簡(jiǎn)單的滑動(dòng)導(dǎo)航代碼,適合用于移動(dòng)端。需要的朋友可以參考下2017-05-05
jQuery EasyUI編輯DataGrid用combobox實(shí)現(xiàn)多級(jí)聯(lián)動(dòng)
本文給大家分享jQuery EasyUI編輯DataGrid用combobox實(shí)現(xiàn)多級(jí)聯(lián)動(dòng)效果的實(shí)例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧2016-08-08
jQuery 翻頁(yè)組件yunm.pager.js實(shí)現(xiàn)div局部刷新的思路
翻頁(yè)插件有很多種,做出來(lái)的效果都非常棒,這篇文章主要介紹了jQuery 翻頁(yè)組件yunm.pager.js實(shí)現(xiàn)div局部刷新的思路,非常不錯(cuò),需要的朋友可以參考下2016-08-08
jQuery實(shí)現(xiàn)的一個(gè)自定義Placeholder屬性插件
這篇文章主要介紹了jQuery實(shí)現(xiàn)的一個(gè)自定義Placeholder屬性插件,本文最后附有插件完整源碼,需要的朋友可以參考下2014-08-08
JQuery+Ajax無(wú)刷新分頁(yè)的實(shí)例代碼
本篇文章主要是對(duì)JQuery+Ajax無(wú)刷新分頁(yè)的實(shí)例代碼進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-02-02
jquery validate.js表單驗(yàn)證入門(mén)實(shí)例(附源碼)
這篇文章主要介紹了jquery validate.js表單驗(yàn)證入門(mén)實(shí)例,為大家提供了jquery validate.js表單驗(yàn)證的源碼,特別適合初學(xué)者學(xué)習(xí)validate.js表單驗(yàn)證,感興趣的小伙伴們可以參考一下2015-11-11
jquery實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊后展開(kāi)列表內(nèi)容的導(dǎo)航欄效果
這篇文章主要介紹了jquery實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊后展開(kāi)列表內(nèi)容的導(dǎo)航欄效果,通過(guò)簡(jiǎn)單的jQuery鏈?zhǔn)讲僮鲗?shí)現(xiàn)針對(duì)頁(yè)面元素的遍歷及樣式動(dòng)態(tài)變換功能,需要的朋友可以參考下2015-09-09
jquery實(shí)現(xiàn)點(diǎn)擊展開(kāi)列表同時(shí)隱藏其他列表
這篇文章主要介紹了jquery實(shí)現(xiàn)點(diǎn)擊展開(kāi)列表同時(shí)隱藏其他列表的方法,涉及jquery鼠標(biāo)事件及節(jié)點(diǎn)的遍歷與屬性操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08

