【IMWeb训练营作业】 - 作业2:Select

demo: http://hiijack.github.io/customselect/index.html

github: https://github.com/hiijack/vue-select


截图



核心代码:

CustomSelect.vue

<template>
    <div>
        <div class="searchBox">
            <input class="searchInput" type="text" placeholder="点一下" v-on:click="showList" v-bind:value="text"/>
        </div>
        <SelectList v-show="listShow" v-bind:list="listValue" v-on:changeValue="changeValue"></SelectList>
    </div>
</template>

<script>
import SelectList from './SelectList'
export default {
    name: 'customList',
    components: {
        SelectList
    },
    data() {
        return {
            listShow: false,
            text: '',
            listValue: ['vue', 'react', 'angular']
        }
    },
    methods: {
        showList() {
            this.listShow = true;
        },
        hideList() {
            this.listShow = false;
        },
        changeValue(val) {
            this.text = val;
            this.listShow = false;
        }
    }
}
</script>

<style scoped>
    .searchBox {
        border-width: 1px;
        border-style: solid;
        border-color: #ccc;
        width: 85%;
        display: inline-block;
    }
    
    .searchInput {
        float: left;
        width: 95%;
        height: 35px;
        margin: 3px 1px 0 15px;
        padding: 0 10px 0 0;
        font-size: 16px;
        outline: none;
        border: 0;
        box-sizing: content-box;
    }
    
</style>

SelectList.vue

    <template>
        <ul class="list-group">
            <li class="list-group-item" v-for="item of list" @click="selectItem(item)">
                <!--<a href='#' v-on:click="selectItem(item)">{{item}}</a>-->
                {{item}}
            </li>
        </ul>
    </template>
    <script>
    export default {
        name: 'SelectList',
        props: ['list'],
        methods: {
            selectItem(item) {
                this.$emit('changeValue', item);
            }
        }
    }
    </script>
    <style scoped>
        .list-group {
            margin: auto;
            width: 85%;
            padding-left: 0px;
        }
        
        .list-group-item {
            position: relative;
            display: block;
            padding: 10px 15px;
            margin-bottom: -1px;
            background-color: #fff;
            border: 1px solid #ddd;
        }
        
        ul {
            list-style-type: none;
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        li {
            cursor: pointer;
            text-align: left;
            border: 0;
            border-collapse: collapse;
            border-spacing: 0;
            list-style: none;
            margin: 0;
            padding: 0;
        }
        
        li:hover {
            background-color: #ccc;
        }
    </style>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值