使用Bootrap和Vue实现仿百度搜索功能

用Vue调用百度的搜索接口,实现简单的搜索功能。
搜索框的样式是基于Bootstrap,当然对样式做了简单的调整,
使之类似于百度搜索。代码如下

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>百度搜索</title>
    <style type="text/css">
        .gray{
            background-color: #eee;
        }
        .listyle{
            font-size: 16px;
            line-height: 35px;
            padding-left: 16px;
        }
        .ulstyle{
            border:1px solid #ccc;
            border-top: none;
        }
    </style>
    <link rel="stylesheet" type="text/css" href="bootstrap.min.css">
    <script type="text/javascript" src="vue.js"></script>
    <script type="text/javascript" src="vue-resource.js"></script>
    <script type="text/javascript">
        window.onload = function(){
            new Vue({
                el: ".container",
                data: {
                    myData:[],
                    txt:"",
                    nowIndex:-1
                },
                methods:{
                    get:function(event){
                        if(event.keyCode==38 || event.keyCode==40){
                            return;
                        }
                        if(event.keyCode==13){
                            window.open("https://www.baidu.com/s?wd="+this.txt);
                            this.txt="";
                        }
                        this.$http.jsonp("https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su",{
                            wd:this.txt
                        },{
                            jsonp:"cb"
                        }).then(function(res){
                            this.myData=res.data.s
                        },function(res){
                            alert(res.status);
                        });
                    },
                    changeDown:function(){
                        this.nowIndex++;
                        if(this.nowIndex==this.myData.length){
                            this.nowIndex=0;
                            this.txt=this.myData[0];
                        }else{
                            this.txt=this.myData[this.nowIndex];
                        }
                    },
                    changeUp:function(){
                        this.nowIndex--;
                        if(this.nowIndex==-1){
                            this.nowIndex=this.myData.length-1;
                            this.txt=this.myData[this.nowIndex];
                        }else{
                            this.txt=this.myData[this.nowIndex];
                        }
                    },
                    mouseOver:function(n){
                        this.nowIndex=n;
                        this.txt=this.myData[this.nowIndex];
                    },
                    getMsg:function(){
                        window.open("https://www.baidu.com/s?wd="+this.txt);
                        this.txt="";
                    }
                }
            });
        }
    </script>
</head>
<body>
    <br>
    <div class="container">
        <div class="input-group">
            <input type="text" class="form-control input-lg" placeholder="请输入关键字" v-model="txt" @keyup="get($event)" @keydown.down="changeDown()" @keydown.up="changeUp()">
            <span class="input-group-btn">
                <button class="btn btn-default btn-lg" type="button" @click="getMsg()">搜索</button>
            </span>
        </div>
        <ul class="list-unstyled ulstyle" v-show="myData.length!=0">
            <li v-for="item in myData" :class={gray:$index==nowIndex,listyle:true} @mouseover="mouseOver($index)" @click="getMsg()">{{item}}</li>
        </ul>
    </div>
</body>
</html>   

实现效果如下

这里写图片描述


这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值