Vue.js 中制作自定義選擇組件的代碼附演示demo
定制 select 標(biāo)簽的設(shè)計(jì)非常困難。有時(shí)候,如果不使用樣式化的 div 和自定義 JavaScript 的結(jié)合來(lái)構(gòu)建自己的腳本,那是不可能的。在本文中,你將學(xué)習(xí)如何構(gòu)建使用完全自定義 CSS 設(shè)置樣式的 Vue.js 組件。
Demo: https://codesandbox.io/s/custom-vuejs-select-component-8nqgd
HTML
<template> <div class="custom-select" :tabindex="tabindex" @blur="open = false" > <div class="selected" :class="{open: open}" @click="open = !open" > {{ selected }} </div> <div class="items" :class="{selectHide: !open}" > <div class="item" v-for="(option, i) of options" :key="i" @click="selected=option; open=false; $emit('input', option)" > {{ option }} </div> </div> </div> </template>
需要注意以下幾點(diǎn):
- tabindex 屬性使我們的組件能夠得到焦點(diǎn),從而使它變得模糊。當(dāng)用戶在組件外部單擊時(shí), blur 事件將關(guān)閉我們的組件。
- input 參數(shù)發(fā)出選定的選項(xiàng),父組件可以輕松地對(duì)更改做出反應(yīng)。
JavaScript
<script> export default { props:{ options:{ type: Array, required: true }, tabindex:{ type: Number, required: false, default: 0 } }, data() { return { selected: this.options.length > 0 ? this.options[0] : null, open: false }; }, mounted(){ this.$emit('input', this.selected); } }; </script>
另外,要注意的重要事項(xiàng):
我們還會(huì)在 mount 上發(fā)出選定的值,以便父級(jí)不需要顯式設(shè)置默認(rèn)值。如果我們的 select 組件是較大表單的一部分,那么我們希望能夠設(shè)置正確的 tabindex 。
CSS
<style scoped> .custom-select { position: relative; width: 100%; text-align: left; outline: none; height: 47px; line-height: 47px; } .selected { background-color: #080D0E; border-radius: 6px; border: 1px solid #858586; color: #ffffff; padding-left: 8px; cursor: pointer; user-select: none; } .selected.open{ border: 1px solid #CE9B2C; border-radius: 6px 6px 0px 0px; } .selected:after { position: absolute; content: ""; top: 22px; right: 10px; width: 0; height: 0; border: 4px solid transparent; border-color: #fff transparent transparent transparent; } .items { color: #ffffff; border-radius: 0px 0px 6px 6px; overflow: hidden; border-right: 1px solid #CE9B2C; border-left: 1px solid #CE9B2C; border-bottom: 1px solid #CE9B2C; position: absolute; background-color: #080D0E; left: 0; right: 0; } .item{ color: #ffffff; padding-left: 8px; cursor: pointer; user-select: none; } .item:hover{ background-color: #B68A28; } .selectHide { display: none; } </style>
該 CSS只是一個(gè)示例,你可以按照你的需求隨意修改樣式。
我希望這可以幫助你創(chuàng)建自己的自定義選擇組件,以下是完整組件要點(diǎn)的鏈接:
最后,在線演示的示例:https://codesandbox.io/s/custom-vuejs-select-component-8nqgd
總結(jié)
到此這篇關(guān)于Vue.js 中制作自定義選擇組件的代碼附演示demo的文章就介紹到這了,更多相關(guān)vuejs自定義選擇組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue實(shí)現(xiàn)裁切圖片同時(shí)實(shí)現(xiàn)放大、縮小、旋轉(zhuǎn)功能
這篇文章主要介紹了vue實(shí)現(xiàn)裁切圖片同時(shí)實(shí)現(xiàn)放大、縮小、旋轉(zhuǎn)功能,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03vue動(dòng)態(tài)設(shè)置路由權(quán)限的主要思路
這篇文章主要給大家介紹了關(guān)于vue動(dòng)態(tài)設(shè)置路由權(quán)限的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01vue/cli3.0腳手架部署到nginx時(shí)頁(yè)面空白的問(wèn)題及解決
這篇文章主要介紹了vue/cli3.0腳手架部署到nginx時(shí)頁(yè)面空白的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10vue執(zhí)行配置選項(xiàng)npm?run?serve的本質(zhì)圖文詳解
本地開(kāi)發(fā)一般通過(guò)執(zhí)行npm run serve命令來(lái)啟動(dòng)項(xiàng)目,那這行命令到底存在什么魔法?下面這篇文章主要給大家介紹了關(guān)于vue執(zhí)行配置選項(xiàng)npm?run?serve的本質(zhì)的相關(guān)資料,需要的朋友可以參考下2023-05-05十分鐘帶你快速上手Vue3過(guò)渡動(dòng)畫(huà)
在開(kāi)發(fā)中我們想要給一個(gè)組件的顯示和消失添加某種過(guò)渡動(dòng)畫(huà),可以很好的增加用戶體驗(yàn),下面這篇文章主要給大家介紹了關(guān)于如何快速上手Vue3過(guò)渡動(dòng)畫(huà)的相關(guān)資料,需要的朋友可以參考下2022-02-02