使用 Vue.js 仿百度搜索框的實例代碼
更新時間:2017年05月09日 17:12:24 作者:與蟒唯舞
本篇文章主要介紹了使用 Vue.js 仿百度搜索框的實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
整理文檔,搜刮出一個使用 Vue.js 仿百度搜索框的實例代碼,稍微整理精簡一下做下分享。
<!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="請輸入搜索內(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>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue composition-api 封裝組合式函數(shù)的操作方法
在 Vue 應用的概念中,“組合式函數(shù)”(Composables) 是一個利用 Vue 的組合式 API 來封裝和復用有狀態(tài)邏輯的函數(shù),這篇文章主要介紹了vue composition-api 封裝組合式函數(shù)的操作方法,需要的朋友可以參考下2022-10-10
Unocss(原子化css)?使用及vue3?+?vite?+?ts講解
這篇文章主要介紹了Unocss(原子化css)使用vue3?+?vite?+?ts的方法,簡單介紹了Unocss使用及圖標庫使用,通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2022-11-11
基于Vue3+TypeScript實現(xiàn)圖片預覽組件
在現(xiàn)代的 Web 應用中,圖片預覽是一個常見的需求,本文將介紹如何使用 Vue3 和 TypeScript 開發(fā)一個圖片預覽組件,支持展示單張或多張圖片,并提供了豐富的配置選項,需要的朋友可以參考下2024-04-04
詳解基于vue-router的動態(tài)權(quán)限控制實現(xiàn)方案
本篇文章主要介紹了詳解基于vue-router的動態(tài)權(quán)限實現(xiàn)方案,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09
vue3.0基于views批量實現(xiàn)動態(tài)路由的方法(示例代碼)
以前vue項目中也有很多實現(xiàn)動態(tài)路由的方法,比如有一些項目涉及權(quán)限的可能會使用api請求路由數(shù)據(jù)在來createRouter,或者本地構(gòu)建使用routes.push來動態(tài)構(gòu)建路由, 今天介紹一種新的方式來基于某個文件夾批量構(gòu)建動態(tài)路由的方法,感興趣的朋友一起看看吧2024-12-12
vue父組件數(shù)據(jù)更新子組件相關(guān)內(nèi)容未改變問題(用watch解決)
這篇文章主要介紹了vue父組件數(shù)據(jù)更新子組件相關(guān)內(nèi)容未改變問題(用watch解決),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03

