使用el-card仿Edge搜索栏和热点标签

转载请注明出处
个人博客:https://maxusun.github.io/

前几天有个需求,写一个搜索栏,下面需要附上常用搜索的标签,然后发现Edge主页的样式很好看,于是便实现了一个类似的。
Edge主页:
在这里插入图片描述

自己实现的:在这里插入图片描述

实现

布局

整体的布局如下:在这里插入图片描述

实现的时候,父组件:Search.vue,search-bar组件:SearchItem.vue,热点话题组件:SearchHot.vue

实现代码

Search.vue
<template>
    <div class="box">
            <div class="searchBox">
                <search-item></search-item>
            </div>
            <div class="searchBox">
                <SearchHot></SearchHot>
            </div>
    </div>
</template>

<script>
// 引入两个组件,this.$t是使用i18n适配多种语言
    import SearchItem from "@/components/SearchItem";
    import SearchHot from "@/components/SearchHot";
    export default {
        name: "Search",
        components: {SearchItem,SearchHot},
        computed:{
            elinputplace(){return this.$t('msg.searchkey')},
        },
        methods:{
        }
    }
</script>

<style>
    .box{
        margin: 5% auto;
        padding-top: 6%;
        height: 60px;
        width: 100%;
    }
    .searchBox{
        margin: 0 auto;
        width: 60%;
        position: relative;
    }
</style>
SearchItem.vue
<template>
    <div>
        <el-input :placeholder=elinputplace v-model="q" class="input-with-select">
            <el-button slot="append" type="primary" plain @click="search" icon="el-icon-search">{{$t('msg.search')}}</el-button>
        </el-input>
    </div>
</template>

<script>
    import Hot from "@/components/Hot";
    export default {
        components:{Hot},
        name: "SearchItem",
        computed:{
            elinputplace(){return this.$t('msg.searchkey')},
            eltitle(){return this.$t('msg.notice')}
        },
        data(){
            return{
               q:'',
            }
        },
        methods:{
            search(){
                // 在这里实现点击搜索事件
            },
        }
    }
</script>

<style scoped>
    .el-dialog{
        width: 60%;
    }
</style>
SearchHot.vue
<template>
    <div>
        <el-card class="my-card"
                 v-for="(item,index) in items"
                 :key="index"
                 >
            <el-image @click="gethot(item.label)"
                    :src=item.url
            ></el-image>
                <span class="my-word" @click="gethot(item.label)">{{item.label}}</span>
        </el-card>
    </div>
</template>

<script>
    export default {
        name: "SearchHot",
        data(){
            return{
                items: [
                    { type: 'info', label: 'Loading',url:require("@/assets/hot/work.png") },
                    { type: 'info', label: 'Loading',url:require("@/assets/hot/miss_work.png")},
                    { type: 'info', label: 'Loading',url:require("@/assets/hot/course.png")},
                    { type: 'info', label: 'Loading',url:require("@/assets/hot/sick.png")},
                    { type: 'info', label: 'Loading',url:require("@/assets/hot/study.png")}
                ],
                currentDate: new Date()
            }
        },
        created:function () {
            // 获取热点话题并加载到items里面
            this.$axios.get('http://xxxxxxxx/hot/').then((response) => {
                let data = response.data.slice(1,-1).split(', ')
                for(let i = 0;i < 5;i++){
                    this.items[i].label = data[i].slice(1,-1)
                }
            }).catch(() => {
            });
        },
        methods:{
            gethot(label){
                // 点击标签时进行相应的处理
            }
        }
    }
</script>

<style scoped>
    .el-card{
        width: 120px;
        height: 100px;
        float: left;
        margin-top: 4%;
        margin-left: 6%;
        background-color: #ecf5ff;
    }
    .my-card >>> .el-card__body{
        padding: 1px;
        text-align: center;
    }
    .my-word{
        display: block;
        font-size: 13px;
    }
    .el-image{
        text-align: center;
        width: 50px;
        height: 50px;
        margin: 10px;
    }

</style>

其他

完整代码和项目可见:完整项目

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MaXuwl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值