使用 Vue.js 仿百度搜索框的实例代码
2017-05-09 17:12
1106 查看
整理文档,搜刮出一个使用 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="请输入搜索内容" 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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
相关文章推荐
- 百度判断手机终端并自动跳转uaredirect.js代码及使用实例
- 百度判断手机终端并自动跳转js代码及使用实例
- vue.js中toast用法及使用toast弹框的实例代码
- 百度判断手机终端并自动跳转js代码及使用实例
- 百度判断手机终端并自动跳转uaredirect.js代码及使用实例
- 百度判断手机终端并自动跳转uaredirect.js代码及使用实例
- 关于在vue 中使用百度ueEditor编辑器的方法实例代码
- Spring boot + mybatis + Vue.js + ElementUI 实现数据的增删改查实例代码(二)
- Vue.js组件使用开发实例教程
- js 弹出框插件实例与代码使用方法
- vue.js 上传图片实例代码
- vue.js 左侧二级菜单显示与隐藏切换的实例代码
- php使用正则过滤js脚本代码实例
- vue.js加载新的内容(实例代码)
- vue.js实现条件渲染的实例代码
- Spring boot + mybatis + Vue.js + ElementUI 实现数据的增删改查实例代码(一)
- 使用JS实现图片展示瀑布流效果的实例代码
- vue-cli+webpack在生成的项目中使用bootstrap实例代码
- 判断手机终端并自动跳转js代码及使用实例
- 使用vue.js实现联动效果的示例代码