当前位置: 移动技术网 > IT编程>脚本编程>vue.js > vue实现百度搜索下拉提示功能实例

vue实现百度搜索下拉提示功能实例

2017年12月12日  | 移动技术网IT编程  | 我要评论

紫云谷,长沙焊工招聘,炫舞称号亲爱的你在哪里

这段代码用到vuejs和vue-resouece。实现对接智能提示接口,并通过上下键选择提示项,按enter进行搜索

<!doctype html>
<html lang="en">

<head>
 <meta charset="utf-8">
 <title>document</title>
 <script type="text/javascript" src="vue.js"></script>
 <script type="text/javascript" src="vue-resource.js"></script>
 <script type="text/javascript">
 window.onload = function() {
  var app = new vue({
   el: '#box',
   data: {
    mydata: [],
    tt: '',
    now: -1
   },
   methods: {
    get: function(e) {

     // 请求限制 按了上下箭头
     if (e.keycode === 38 || e.keycode === 40) {
      return
     }
     // enter跳转
     if (e.keycode === 13) {
      window.open('https://www.baidu.com/s?wd=' + this.tt);
      this.tt = '';
     }
     this.$http.jsonp('https://sp0.baidu.com/5a1fazu8aa54nxgko9wtanf6hhy/su', {
      wd: this.tt
     }, {
      jsonp: 'cb'
     }).then(function(res) {
      // 请求成功
      this.mydata = res.data.s;
        this.now = -1;
     }, function(res) {
      // 请求失败
      console.log(res.status)
     })
    },
    changedown: function() {
     this.now++;
     // 到了最后一个选项
     if (this.now === this.mydata.length) {
      this.now = -1;
     }
     this.tt = this.mydata[this.now]
    },
    changeup: function() {
     this.now--;
     // 到了第一个选项
     if (this.now === -2) {
      this.now = this.mydata.length - 1;
     }
     this.tt = this.mydata[this.now]

    }
   }
  })
 }
 </script>
 <style type="text/css">
 .gray {
  background: gray
 }
 </style>
</head>

<body>
 <!-- 百度下拉接口 -->
 <div id="box">
  <input type="text" v-model="tt" name="" @keyup="get($event)" @keydown.down="changedown()" @keydown.up="changeup()">
  <ul>
   <li v-for="(item, index) in mydata" :class="{gray:index===now}">{{item}}</li>
  </ul>
 </div>
</body>

</html>

效果图:

这个ajax请求没有做节流,很多时候需要限制ajax频繁请求,可以小改一下:

<!doctype html>
<html lang="en">

<head>
 <meta charset="utf-8">
 <title>document</title>
 <script type="text/javascript" src="vue.js"></script>
 <script type="text/javascript" src="vue-resource.js"></script>
 <script type="text/javascript">
 window.onload = function() {
  var app = new vue({
   el: '#box',
   data: {
    mydata: [],
    tt: '',
    now: -1
   },
   methods: {
    get: function(e) {

     // 请求限制 按了上下箭头
     if (e.keycode === 38 || e.keycode === 40) {
      return
     }
     // enter跳转
     if (e.keycode === 13) {
      window.open('https://www.baidu.com/s?wd=' + this.tt);
      this.tt = '';
     }
     // 限制频繁请求
     this.throttle(this.getdata,window)
    },
    changedown: function() {
     this.now++;
     // 到了最后一个选项
     if (this.now === this.mydata.length) {
      this.now = -1;
     }
     this.tt = this.mydata[this.now]
    },
    changeup: function() {
     this.now--;
     // 到了第一个选项
     if (this.now === -2) {
      this.now = this.mydata.length - 1;
     }
     this.tt = this.mydata[this.now]

    },
    // 把请求单独拿出来
    getdata() {
     this.$http.jsonp('https://sp0.baidu.com/5a1fazu8aa54nxgko9wtanf6hhy/su', {
      wd: this.tt
     }, {
      jsonp: 'cb'
     }).then(function(res) {
      // 请求成功
      this.mydata = res.data.s;
        this.now = -1;
     }, function(res) {
      // 请求失败
      console.log(res.status)
     })
    },
    // 节流函数
    throttle(method,context){
      cleartimeout(method.tid);
      method.tid=settimeout(function(){
        method.call(context);
      },300);
    }
   }
  })
 }
 </script>
 <style type="text/css">
 .gray {
  background: gray
 }
 </style>
</head>

<body>
 <!-- 百度下拉接口 -->
 <div id="box">
  <input type="text" v-model="tt" name="" @keyup="get($event)" @keydown.down="changedown()" @keydown.up="changeup()">
  <ul>
   <li v-for="(item, index) in mydata" :class="{gray:index===now}">{{item}}</li>
  </ul>
 </div>
</body>

</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网