Vue.js 中制作自定義選擇組件的代碼附演示demo
定制 select 標(biāo)簽的設(shè)計非常困難。有時候,如果不使用樣式化的 div 和自定義 JavaScript 的結(jié)合來構(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>
需要注意以下幾點:
- tabindex 屬性使我們的組件能夠得到焦點,從而使它變得模糊。當(dāng)用戶在組件外部單擊時, blur 事件將關(guān)閉我們的組件。
- input 參數(shù)發(fā)出選定的選項,父組件可以輕松地對更改做出反應(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>
另外,要注意的重要事項:
我們還會在 mount 上發(fā)出選定的值,以便父級不需要顯式設(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只是一個示例,你可以按照你的需求隨意修改樣式。
我希望這可以幫助你創(chuàng)建自己的自定義選擇組件,以下是完整組件要點的鏈接:
最后,在線演示的示例:https://codesandbox.io/s/custom-vuejs-select-component-8nqgd
總結(jié)
到此這篇關(guān)于Vue.js 中制作自定義選擇組件的代碼附演示demo的文章就介紹到這了,更多相關(guān)vuejs自定義選擇組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue實現(xiàn)裁切圖片同時實現(xiàn)放大、縮小、旋轉(zhuǎn)功能
這篇文章主要介紹了vue實現(xiàn)裁切圖片同時實現(xiàn)放大、縮小、旋轉(zhuǎn)功能,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03vue動態(tài)設(shè)置路由權(quán)限的主要思路
這篇文章主要給大家介紹了關(guān)于vue動態(tài)設(shè)置路由權(quán)限的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01vue/cli3.0腳手架部署到nginx時頁面空白的問題及解決
這篇文章主要介紹了vue/cli3.0腳手架部署到nginx時頁面空白的問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10vue執(zhí)行配置選項npm?run?serve的本質(zhì)圖文詳解
本地開發(fā)一般通過執(zhí)行npm run serve命令來啟動項目,那這行命令到底存在什么魔法?下面這篇文章主要給大家介紹了關(guān)于vue執(zhí)行配置選項npm?run?serve的本質(zhì)的相關(guān)資料,需要的朋友可以參考下2023-05-05