使用 Vue.js 仿百度搜索框的實(shí)例代碼
整理文檔,搜刮出一個(gè)使用 Vue.js 仿百度搜索框的實(shí)例代碼,稍微整理精簡(jiǎn)一下做下分享。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue demo</title>
<style type="text/css">
.bg {
background: #ccc;
}
</style>
<script src="https://cdn.bootcss.com/vue/2.1.7/vue.min.js"></script>
<script src="https://cdn.bootcss.com/vue-resource/1.3.1/vue-resource.min.js"></script>
<script type="text/javascript">
window.onload = function() {
new Vue({
el: '#box',
data: {
inputText: '',
text: '',
nowIndex: -1,
result: []
},
methods: {
show: function(ev) {
if (ev.keyCode == 38 || ev.keyCode == 40) {
if (this.nowIndex < -1){
return;
}
if (this.nowIndex != this.result.length && this.nowIndex != -1) {
this.inputText = this.result[this.nowIndex];
}
return;
}
if (ev.keyCode == 13) {
window.open('https://www.baidu.com/s?wd=' + this.inputText, '_blank');
this.inputText = '';
}
this.text = this.inputText;
this.$http.jsonp('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su', {
params: {
wd: this.inputText
},
jsonp: 'cb'
}).then(res => {
this.result = res.data.s;
})
},
down: function() {
this.nowIndex++;
if (this.nowIndex == this.result.length) {
this.nowIndex = -1;
this.inputText = this.text;
}
},
up: function() {
this.nowIndex--;
if (this.nowIndex < -1){
this.nowIndex = -1;
return;
}
if (this.nowIndex == -1) {
this.nowIndex = this.result.length;
this.inputText = this.text;
}
}
}
});
}
</script>
</head>
<body>
<div id="box">
<input type="text" placeholder="請(qǐng)輸入搜索內(nèi)容" v-model='inputText' @keyup='show($event)' @keydown.down='down()' @keydown.up.prevent='up()'>
<ul>
<li v-for="(item, index) in result" :class='{bg: index==nowIndex}'>
{{item}}
</li>
</ul>
</div>
</body>
</html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue composition-api 封裝組合式函數(shù)的操作方法
在 Vue 應(yīng)用的概念中,“組合式函數(shù)”(Composables) 是一個(gè)利用 Vue 的組合式 API 來封裝和復(fù)用有狀態(tài)邏輯的函數(shù),這篇文章主要介紹了vue composition-api 封裝組合式函數(shù)的操作方法,需要的朋友可以參考下2022-10-10
Unocss(原子化css)?使用及vue3?+?vite?+?ts講解
這篇文章主要介紹了Unocss(原子化css)使用vue3?+?vite?+?ts的方法,簡(jiǎn)單介紹了Unocss使用及圖標(biāo)庫(kù)使用,通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11
基于Vue3+TypeScript實(shí)現(xiàn)圖片預(yù)覽組件
在現(xiàn)代的 Web 應(yīng)用中,圖片預(yù)覽是一個(gè)常見的需求,本文將介紹如何使用 Vue3 和 TypeScript 開發(fā)一個(gè)圖片預(yù)覽組件,支持展示單張或多張圖片,并提供了豐富的配置選項(xiàng),需要的朋友可以參考下2024-04-04
詳解基于vue-router的動(dòng)態(tài)權(quán)限控制實(shí)現(xiàn)方案
本篇文章主要介紹了詳解基于vue-router的動(dòng)態(tài)權(quán)限實(shí)現(xiàn)方案,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09
vue3.0基于views批量實(shí)現(xiàn)動(dòng)態(tài)路由的方法(示例代碼)
以前vue項(xiàng)目中也有很多實(shí)現(xiàn)動(dòng)態(tài)路由的方法,比如有一些項(xiàng)目涉及權(quán)限的可能會(huì)使用api請(qǐng)求路由數(shù)據(jù)在來createRouter,或者本地構(gòu)建使用routes.push來動(dòng)態(tài)構(gòu)建路由, 今天介紹一種新的方式來基于某個(gè)文件夾批量構(gòu)建動(dòng)態(tài)路由的方法,感興趣的朋友一起看看吧2024-12-12
vue父組件數(shù)據(jù)更新子組件相關(guān)內(nèi)容未改變問題(用watch解決)
這篇文章主要介紹了vue父組件數(shù)據(jù)更新子組件相關(guān)內(nèi)容未改變問題(用watch解決),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03

