基于Vue.js 2.0实现百度搜索框效果


Posted in Javascript onDecember 28, 2020

使用Vue.js 2.0 模仿百度搜索框效果,供大家参考,具体内容如下

<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=2.0, maximum-scale=1.0, minimum-scale=1.0">
 <title>Vue模拟百度搜索</title>
 <style type="text/css">
 body, html{
 padding: 0;
 margin: 0;
 }
 #box{
 margin-top: 80px;
 display: flex;
 flex-direction: column;
 align-items: center;
 justify-content: center;
 }
 .input{
 width: 500px;
 height: 30px;
 text-indent: 4px;
 }
 .baidu input{
 height: 30px;
 cursor: pointer;
 color: #fff;
 letter-spacing: 1px;
 background: #3385ff;
 border: 1px solid #2d78f4;
 }
 ul{
 padding: 0;
 margin-top: 6px;
 }
 li{
 list-style: none;
 margin: 4px;
 }
 li:hover{
 background: #ccc;
 }
 .bgcolor {
 background: #ccc;
 }
 </style>
 <script src="https://cdn.bootcss.com/vue/2.4.2/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 (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;
   })
  },
  goto () {
   window.open('https://www.baidu.com/s?wd=' + this.inputText, '_blank');
   this.inputText = '';
  },
  gotoItem(item) {
   window.open('https://www.baidu.com/s?wd=' + item, '_blank');
   this.inputText = '';
  },
  down () {
   this.nowIndex++;
   if (this.nowIndex == this.result.length) {
   this.nowIndex = -1;
   this.inputText = this.text;
   }
  },
  up () {
   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">
 <img src="https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png" width="270" height="129">
 <div>
  <div>
  <input 
   type="text" 
   class="input" 
   placeholder="请输入搜索内容 " 
   v-model='inputText' 
   @keyup='show($event)' 
   @keydown.down='down()' 
   @keydown.up.prevent='up()'
  >
  <span class="baidu" @click="goto()">
   <input type="submit" value="百度一下" >
  </span>
  </div>
  
  <ul>
  <li v-for="(item, index) in result" :class='{bgcolor: index==nowIndex}' @click="gotoItem(item)">
   {{item}}
  </li>
  </ul>
 </div>

 </div>
</body> 
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
jQuery实现的Tab滑动选项卡及图片切换(多种效果)小结
Sep 14 Javascript
Bootstrap滚动监听(Scrollspy)插件详解
Apr 26 Javascript
详解基于vue的移动web app页面缓存解决方案
Aug 03 Javascript
基于daterangepicker日历插件使用参数注意的问题
Aug 10 Javascript
ES6中数组array新增方法实例总结
Nov 07 Javascript
angular6的table组件开发的实现示例
Dec 26 Javascript
更强大的vue ssr实现预取数据的方式
Jul 19 Javascript
解决layui表格内文本超出隐藏的问题
Sep 12 Javascript
JavaScript 作用域实例分析
Oct 02 Javascript
详解Vue的watch中的immediate与watch是什么意思
Dec 30 Javascript
使用Vue+Django+Ant Design做一个留言评论模块的示例代码
Jun 01 Javascript
Vue 3.0中jsx语法的使用
Nov 13 Javascript
JavaScript闭包的简单应用
Sep 01 #Javascript
写给vue新手们的vue渲染页面教程
Sep 01 #Javascript
深入理解Vue 的条件渲染和列表渲染
Sep 01 #Javascript
js禁止Backspace键使浏览器后退的实现方法
Sep 01 #Javascript
JavaScript中数组常见操作技巧
Sep 01 #Javascript
js禁止浏览器页面后退功能的实例(推荐)
Sep 01 #Javascript
使用JS和canvas实现gif动图的停止和播放代码
Sep 01 #Javascript
You might like
一个用于mysql的数据库抽象层函数库
2006/10/09 PHP
php基础知识:类与对象(1)
2006/12/13 PHP
示例详解Laravel的注册重构
2016/08/14 PHP
document 和 document.all 分别什么时候用
2006/06/22 Javascript
HTML TO JavaScript 转换
2006/06/26 Javascript
InnerHtml和InnerText的区别分析
2009/03/13 Javascript
为jquery.ui.dialog 增加“自动记住关闭时的位置”的功能
2009/11/24 Javascript
使用js修改客户端注册表的方法
2013/08/09 Javascript
Nodejs+express+html5 实现拖拽上传
2014/08/08 NodeJs
javascript解三阶幻方(九宫格)
2015/04/22 Javascript
nodejs导出excel的方法
2015/06/30 NodeJs
JavaScript+html5 canvas制作的百花齐放效果完整实例
2016/01/26 Javascript
jquery获取复选框checkbox的值的简单实现方法
2016/05/26 Javascript
如何处理JSON中的特殊字符
2016/11/30 Javascript
javascript数据类型详解
2017/02/07 Javascript
Three.js 再探 - 写一个微信跳一跳极简版游戏
2018/01/04 Javascript
vue 基于element-ui 分页组件封装的实例代码
2018/12/10 Javascript
Vue-cli项目部署到Nginx服务器的方法
2019/11/01 Javascript
JavaScript 接口原理与用法实例详解
2020/05/12 Javascript
Python中asyncore的用法实例
2014/09/29 Python
python中lambda与def用法对比实例分析
2015/04/30 Python
简单介绍Python中的round()方法
2015/05/15 Python
Python去除字符串两端空格的方法
2015/05/21 Python
Python中函数及默认参数的定义与调用操作实例分析
2017/07/25 Python
python IDLE 背景以及字体大小的修改方法
2019/07/12 Python
Python如何基于rsa模块实现非对称加密与解密
2020/01/03 Python
英国网上购买门:Direct Doors
2018/06/07 全球购物
澳大利亚在线性感内衣商店:Fantasy Lingerie
2021/02/07 全球购物
学前教育毕业生自荐信
2013/10/29 职场文书
国际经济贸易专业推荐信
2013/11/06 职场文书
预备党员入党思想汇报
2014/01/04 职场文书
年会活动策划方案
2014/01/23 职场文书
2015高考寄语集锦
2015/02/27 职场文书
2015年公司工作总结
2015/04/25 职场文书
详解python中[-1]、[:-1]、[::-1]、[n::-1]使用方法
2021/04/25 Python
Python使用PyYAML库读写yaml文件的方法
2022/04/06 Python