基于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 lazyload延迟加载技术的实现原理分析
Jan 24 Javascript
JQuery为textarea添加maxlength属性并且兼容IE
Apr 25 Javascript
ExtJS实现文件下载的方法实例
Nov 09 Javascript
JQuery动态添加Select的Option元素实现方法
Aug 29 Javascript
浅析JavaScript动画模拟拖拽原理
Dec 09 Javascript
JS遍历对象属性的方法示例
Jan 10 Javascript
浅谈JavaScript find 方法不支持IE的问题
Sep 28 Javascript
薪资那么高的Web前端必看书单
Oct 13 Javascript
vue-cli配置环境变量的方法
Jul 09 Javascript
layui2.0使用table+laypage实现真分页
Jul 27 Javascript
node.js文件操作系统实例详解
Nov 05 Javascript
解读Vue组件注册方式
May 15 Vue.js
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
php使用array_rand()函数从数组中随机选择一个或多个元素
2014/04/28 PHP
Yii使用find findAll查找出指定字段的实现方法
2014/09/05 PHP
php判断类是否存在函数class_exists用法分析
2014/11/14 PHP
PHP CURL post数据报错 failed creating formpost data
2016/10/16 PHP
Yii2实现自定义独立验证器的方法
2017/05/05 PHP
ThinkPHP3.1.x修改成功与失败跳转页面的方法
2017/09/29 PHP
PHP大文件切割上传并带进度条功能示例
2019/07/01 PHP
PHP7创建销毁session的实例方法
2020/02/03 PHP
看了就知道什么是JSON
2007/12/09 Javascript
Tips 带三角可关闭的文字提示
2010/10/06 Javascript
克隆javascript对象的三个方法小结
2011/01/12 Javascript
jquery.ajax的url中传递中文乱码问题的解决方法
2014/02/07 Javascript
jquery JSON的解析方式示例介绍
2014/07/27 Javascript
jQuery实现为图片添加镜头放大效果的方法
2015/06/25 Javascript
js+css实现有立体感的按钮式文字竖排菜单效果
2015/09/01 Javascript
canvas绘制一个常用的emoji表情
2017/03/30 Javascript
angular中使用Socket.io实例代码
2017/06/03 Javascript
解决webpack -p压缩打包react报语法错误的方法
2017/07/03 Javascript
vue2.0 实现导航守卫的具体用法(路由守卫)
2018/05/17 Javascript
微信小程序实现上传图片功能
2018/05/28 Javascript
javascript判断一个变量是数组还是对象
2019/04/10 Javascript
nodejs检测因特网是否断开的解决方案
2019/04/17 NodeJs
vuex 动态注册方法 registerModule的实现
2019/07/03 Javascript
微信小程序项目总结之记账小程序功能的实现(包括后端)
2019/08/20 Javascript
在Python中利用Into包整洁地进行数据迁移的教程
2015/03/30 Python
OpenCV实现机器人对物体进行移动跟随的方法实例
2020/11/09 Python
美国精品地毯网站:Boutique Rugs
2020/03/04 全球购物
在数据文件自动增长时,自动增长是否会阻塞对文件的更新
2014/05/01 面试题
软件测试题目
2013/02/27 面试题
电子信息科学专业自荐信
2014/01/30 职场文书
同学聚会策划方案
2014/06/06 职场文书
教师政风行风评议心得体会
2014/10/21 职场文书
学子宴致辞大全
2015/07/27 职场文书
创业计划书之服装
2019/10/07 职场文书
canvas多重阴影发光效果实现
2021/04/20 Javascript
利用python调用摄像头的实例分析
2021/06/07 Python