Vue下拉選擇框Select組件使用詳解(一)
本文實(shí)例為大家分享了Vue下拉選擇框Select組件的使用方法,供大家參考,具體內(nèi)容如下
效果圖如下:

展開圖如下:

①創(chuàng)建組件Select.vue:預(yù)設(shè)兩種主題色,亦可視情況進(jìn)行自定義修改樣式:
<template>
? <div class="m-select-wrap">
? ? <input
? ? ? :class="['u-select-input f16', color === 'blue' ? '' : 'white-color']"
? ? ? type="text"
? ? ? readonly
? ? ? @click="openSelect"
? ? ? @blur="onBlur"
? ? ? v-model="selectName" />
? ? <div :class="['triangle-down', { 'rotate': rotate }]" @click="openSelect"></div>
? ? <div :class="['m-options-panel f16', showOptions ? 'show': 'hidden']" :style="`height: ${selectData.length * 40}px;`">
? ? ? <p class="u-option" @mousedown="getValue(item.name, item.value, index)" v-for="(item, index) in selectData" :key="index">
? ? ? ? {{ item.name }}
? ? ? </p>
? ? </div>
? </div>
</template>
<script>
export default {
? name: 'Select',
? props: {
? ? selectData: {
? ? ? type: Array,
? ? ? default: () => {
? ? ? ? return []
? ? ? }
? ? },
? ? // eslint-disable-next-line vue/require-prop-types
? ? selValue: { // 將該prop值作為selV的初始值
? ? ? default: undefined
? ? },
? ? color: {
? ? ? type: String,
? ? ? default: () => {
? ? ? ? return 'blue'
? ? ? }
? ? }
? },
? computed: {
? ? selectName () {
? ? ? let selName
? ? ? this.selectData.forEach(item => {
? ? ? ? if (item.value === this.selectValue) {
? ? ? ? ? selName = item.name
? ? ? ? }
? ? ? })
? ? ? return selName
? ? },
? ? selectValue: {
? ? ? get () {
? ? ? ? return this.selV
? ? ? },
? ? ? set (newVal) {
? ? ? ? this.selV = newVal
? ? ? }
? ? }
? },
? data () {
? ? return {
? ? ? selV: this.selValue,
? ? ? rotate: false,
? ? ? showOptions: false
? ? }
? },
? methods: {
? ? openSelect () {
? ? ? this.showOptions = !this.showOptions
? ? ? this.rotate = !this.rotate
? ? },
? ? getValue (name, value, index) {
? ? ? this.selectValue = value
? ? ? this.$emit('getValue', name, value, index)
? ? },
? ? onBlur () {
? ? ? this.showOptions = false
? ? ? this.rotate = false
? ? }
? }
}
</script>
<style lang="less" scoped>
.m-select-wrap {
? width: 135px;
? height: 40px;
? line-height: 40px;
? position: relative;
? .u-select-input {
? ? width: 105px;
? ? background: #3A79EE;
? ? color: #FFFFFF;
? ? box-shadow: 0px 10px 20px 0px rgba(144, 119, 222, 0.2);
? ? border-radius: 20px;
? ? height: 40px;
? ? line-height: 40px;
? ? padding: 0 15px;
? ? cursor: pointer;
? ? border: none;
? }
? .white-color {
? ? background: #FFFFFF;
? ? color: #3A79EE;
? }
? .triangle-down { // 下三角樣式
? ? width: 0;
? ? height: 0;
? ? border-left: 5px solid transparent;
? ? border-right: 5px solid transparent;
? ? border-top: 10px solid #333;
? ? position: absolute;
? ? top: 18px;
? ? right: 15px;
? ? transition: transform 0.3s ease-in-out;
? }
? .rotate {
? ? transform: rotate(180deg);
? }
? .m-options-panel {
? ? position: absolute;
? ? background: #FFFFFF;
? ? border-radius: 8px;
? ? width: 133px;
? ? border: 1px solid #E3E3E3;
? ? top: 46px;
? ? left: 0;
? ? color: #706F94;
? ? .u-option {
? ? ? padding: 0 15px;
? ? ? cursor: pointer;
? ? }
? ? .u-option:hover {
? ? ? color: #3A79EE;
? ? ? background: #EEF1FA;
? ? }
? }
? .show {
? ? display: block;
? }
? .hidden {
? ? display: none;
? }
}
</style>②在要使用的頁面引入:
<Select
?? ?:selectData="selectData"
?? ?:selValue="selValue"
?? ?color="white"
?? ?@getValue="getValue" />
import Select from '@/components/Select'
components: {
? ? Select
}
data () {
? ? return {
?? ? ? ?selectData: [
?? ??? ?{
?? ??? ??? ?name: '十一五',
?? ??? ??? ?value: 11
?? ??? ?},
?? ??? ?{
?? ??? ??? ?name: '十二五',
?? ??? ??? ?value: 12
?? ??? ?},
?? ??? ?{
?? ??? ??? ?name: '十三五',
?? ??? ??? ?value: 13
?? ??? ?},
?? ??? ?
?? ? ? ?],
?? ? ? ?selValue: ''
? ? ?}
}
created () {
?? ?// 初始化下拉框
?? ?this.selValue = this.selectData[0].value
}
methods: {
?? ?getValue (name, value, index) {
?? ? ? ? ?console.log('item:', name, value, index)
?? ?}
}以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue?this.$router六種方法使用示例總結(jié)分析
這篇文章主要為大家介紹了vue this.$router六種方法使用示例總結(jié)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
Vue使用正則校驗(yàn)文本框?yàn)檎麛?shù)
這篇文章主要介紹了Vue使用正則校驗(yàn)文本框?yàn)檎麛?shù)問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
創(chuàng)建項(xiàng)目及包管理yarn create vite源碼學(xué)習(xí)
這篇文章主要為大家介紹了創(chuàng)建項(xiàng)目及包管理yarn create vite源碼學(xué)習(xí)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
在Vue中使用Avue、配置過程及實(shí)際應(yīng)用小結(jié)
在項(xiàng)目中遇到通過點(diǎn)擊加號實(shí)現(xiàn)輸入框的增加、以及對該輸入框的輸入內(nèi)容進(jìn)行驗(yàn)證,通過這些誘導(dǎo)因素創(chuàng)作的這篇文章,本文重點(diǎn)給大家介紹在Vue中使用Avue、配置過程以及實(shí)際應(yīng)用,需要的朋友可以參考下2022-10-10
vue使用Print.js打印頁面樣式不出現(xiàn)的解決
這篇文章主要介紹了vue使用Print.js打印頁面樣式不出現(xiàn)的解決,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
vue3?setup語法糖各種語法新特性的使用方法(vue3+vite+pinia)
這篇文章主要介紹了vue3?setup語法糖各種語法新特性的使用(vue3+vite+pinia),本文主要是記錄vue3的setup語法糖的各種新語法的使用方法,需要的朋友可以參考下2022-09-09

