uni-app彈出層uni-popup使用及修改默認(rèn)樣式的方法實(shí)例
我這里的背景是彈出一個(gè)選擇規(guī)格的菜單。使用vue3+ts,使用組合式api
首先看看在vue3+ts+setup下的使用:
<template> <!-- 定義一個(gè)按鈕,用于打開彈出層 --> <view style="width: 200px"> <button @click="openSpecs">彈出</button> </view> <!-- 彈出層視圖,注意這里的ref="popup",這里背景特意標(biāo)紅了,方便看到效果 --> <uni-popup class="updatePopup" background-color="red" ref="popup" type="center"> 你好 </uni-popup> </template> <script setup lang="ts"> import { ref } from "vue"; // 這里的這個(gè)變量名,必須和上面的ref="popup"的變量名相同,這樣就相當(dāng)于綁定了這個(gè)元素了 const popup = ref(null); // 按鈕調(diào)用該方法打開彈出層 function openSpecs() { popup.value.open(); } </script> <style> </style>
從這里可以看出來,彈出層的整體是透明的,而主界面是隨著內(nèi)部的元素大小自適應(yīng)的。
現(xiàn)在在彈出層中編寫復(fù)雜的效果
我新建了一個(gè)selectSpecs.vue:
<template> <view class="background"> <!-- 標(biāo)題 --> <view style="text-align: center"> <text class="title">{{ title }}</text> </view> <!-- 區(qū)域滾動(dòng)視圖 --> <scroll-view scroll-y="true" :style="'height:' + scrollH + 'px'"> <!-- 結(jié)合分組和數(shù)據(jù)選擇器展示數(shù)據(jù),不是本文的重點(diǎn) --> <uni-group mode="card" top="0" v-for="item in specsList" :key="item.key" :title="item.text" id="group"> <uni-data-checkbox mode="tag" :v-model="item.value" :localdata="item.option"></uni-data-checkbox> </uni-group> </scroll-view> </view> </template> <script setup lang="ts"> import { onMounted, ref } from "vue"; // 下面都是一些生成數(shù)據(jù)的邏輯,可以不關(guān)心。 let title = "標(biāo)題測試"; interface iOption { key: number; text: string; disable: boolean; value: number; } interface iSpecs { key: number; text: string; value: number; option: iOption[]; } const specsList: iSpecs[] = new Array<iSpecs>(3); for (let i = 0; i < 10; i++) { const optionList: iOption[] = new Array<iOption>(i + 1); for (let j = 0; j < i + 1; j++) { optionList[j] = { key: j, text: "屬性" + j, disable: false, value: j, }; } specsList[i] = { key: i, text: "規(guī)格" + i, value: i, option: optionList, }; } // 這里是為了確定滾動(dòng)區(qū)域的高度,不給高度不滾動(dòng)的,這里的效果或許還有點(diǎn)問題。 const scrollH = ref(0); onMounted(() => { let group = uni.createSelectorQuery().select("#group"); //根據(jù)id獲取一個(gè)規(guī)格組的高度 // prettier-ignore group.boundingClientRect((data) => { //計(jì)算高度:高度控制在3個(gè)規(guī)格組的高度以內(nèi),即3個(gè)組以內(nèi)不需要滾動(dòng),+10是因?yàn)閷?shí)測獲取的高度比真實(shí)高度少一點(diǎn),不加的話就會(huì)需要小范圍的滾動(dòng)一下 if (specsList.length<3){ scrollH.value=(data.height+10)* specsList.length }else{ scrollH.value=(data.height+10)*3 } }).exec(); }); </script> <style> .background { background-color: white; border-radius: 10px; } .title { font-size: x-large; font-family: "Times New Roman", Times, Cursive; text-align: center; } </style>
這里內(nèi)容看似很長,其實(shí)都是為了展示效果,可以不看細(xì)節(jié)??梢钥纯次覍L動(dòng)區(qū)域的高度的設(shè)置方法。
引入的方式是將上面“你好”改為如下:
<uni-popup background-color="red" ref="popup" type="center"> <SelectSpecs></SelectSpecs> </uni-popup>
當(dāng)然組件也是要import的:
<script setup lang="ts"> import { ref } from "vue"; import SelectSpecs from "../goods/view/selectSpecs.vue"; //導(dǎo)入組件 ....... </script>
當(dāng)我數(shù)據(jù)小的時(shí)候,他的效果是這樣的:
那個(gè)四個(gè)紅角就是背景的顏色。這個(gè)效果還可以
當(dāng)我數(shù)據(jù)多了之后:
因?yàn)閷挾茸赃m應(yīng),所以寬度是占滿了整個(gè)屏幕的寬度。你看屬性9都換行了。而我希望我的彈出層上下左右都留出一點(diǎn)空間,這樣才好看。
我首先想到的是,給這個(gè)寬度限制一個(gè)最大百分比,用max-width: 80%;
,加在background 樣式的里面,因?yàn)檫@是整個(gè)背景的視圖嘛,結(jié)果效果是這樣的:
紅色背景依然是整個(gè)屏幕的寬度,但內(nèi)容跑左邊去了,這不是我想要的效果。再看看數(shù)據(jù)量小的效果:
這看起來是白色框框占紅色背景的80%呀,并不是內(nèi)容占屏幕的80%呀。還不會(huì)居中了。
看來是我的樣式加的位置不對,但是uni-popup也沒提供這個(gè)參數(shù)。在uni-popup嵌套一層 view來控制樣式:
<template> <!-- 定義一個(gè)按鈕,用于打開彈出層 --> <view style="width: 200px"> <button @click="openSpecs">彈出</button> </view> <!-- 彈出層視圖,注意這里的ref="popup",這里背景特意標(biāo)紅了,方便看到效果 --> <view style="max-width: 80%"> <uni-popup background-color="red" ref="popup" type="center"> <SelectSpecs></SelectSpecs> </uni-popup> </view> </template>
這樣也沒效果。
于是debug發(fā)現(xiàn),真正控制彈出層主窗體大小的樣式是“.uni-popup__wrapper”,難道我要去修改源代碼嗎?這樣不太好,不利于代碼移植。
于是就想著有沒有辦法從子組件修改父組件的樣式的方法,結(jié)果沒找到,倒是找到了父組件修改子組件的樣式的方法,于是代碼稍微修改一下:
<uni-popup class="updatePopup" background-color="red" ref="popup" type="center"> <SelectSpecs></SelectSpecs> </uni-popup>
增加了一個(gè)樣式:
<style scoped> .updatePopup :deep(.uni-popup__wrapper) { max-width: 80%; } </style>
這里注意.vue文件允許有多組<style>
,我這里就是新建的一個(gè)<style scoped>
,然后用深度選擇器去修改子組件的樣式。
其實(shí)父組件與.uni-popup__wrapper
中間還隔著一兩層view節(jié)點(diǎn),但是:deep
也是可以找到這個(gè)樣式去修改的。
最后效果是這樣的:
即使數(shù)據(jù)多的時(shí)候,它也不會(huì)把屏幕寬度占滿。
其實(shí)也是學(xué)會(huì)了一招修改子組件的樣式。
補(bǔ)充:uniapp uni-popup彈出層組件 彈窗 無法彈出 自定義樣式問題
之前用到彈出層組件時(shí)發(fā)現(xiàn)彈出時(shí)會(huì)有一層灰色的遮罩,后來調(diào)試了幾次發(fā)現(xiàn)是因?yàn)闆]有自定義給彈出層寬高,嘗試了幾次終于做到自己想要的效果,話不多說,直接附上完整代碼:
效果圖:
<template> <view> <view class="sign_text" @tap="toggle('center')"> <text style="line-height: 60px;margin-top: 12px;">點(diǎn)擊彈出</text> </view> <!-- 普通彈窗 --> <uni-popup ref="popup" background-color="#fff"> <view class="popup-content" :class="{ 'popup-height': type === 'left' || type === 'right' }"> <view style="color: green;"> 這是一個(gè)彈窗 </view> <view style="color: red;" @click="close()"> 關(guān)閉彈窗 </view> </view> </uni-popup> </view> </template> <script> export default { data() { return { } }, methods: { //彈窗開啟 toggle(type) { this.type = type this.$refs.popup.open(type) }, //彈窗關(guān)閉 close() { this.$refs.popup.close() }, } } </script> <style> .popup-content { display: flex; flex-direction: row; align-items: center; justify-content: center; padding: 15px; height: 200px; width: 280px; background-color: #fff; border-radius: 10px; } </style>
下面教一下不會(huì)靈活使用的同學(xué)這個(gè)組件應(yīng)該怎么用
一、uni-popup彈出層
官網(wǎng)組件地址:https://ext.dcloud.net.cn/pluginid=329
可以先翻看一下官網(wǎng)的介紹,很有用。
二、使用步驟
.首先要去官網(wǎng)把這個(gè)組件導(dǎo)入到項(xiàng)目中或者下載到本地,常規(guī)放在components文件目錄下:
最好是這三個(gè)都要一起下載,這樣用的時(shí)候比較方便,使用的時(shí)候按需引入,自定義組件樣式就好了
總結(jié)
到此這篇關(guān)于uni-app彈出層uni-popup使用及修改默認(rèn)樣式的文章就介紹到這了,更多相關(guān)uni-app彈出層uni-popup使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
firefox TBODY 用js顯示和隱藏時(shí)出現(xiàn)錯(cuò)位的解決方法
今天幫別人寫一個(gè)網(wǎng)頁,發(fā)現(xiàn):當(dāng)用javascript動(dòng)態(tài)設(shè)置tr.style.display = "block"顯示某行時(shí),使用IE瀏覽沒有問題,但使用firefox瀏覽時(shí)該行被移到了其它行的后面,很是詫異。2008-12-12ECharts柱狀圖過多添加滾動(dòng)條的步驟(親測可用)
這篇文章主要介紹了ECharts柱狀圖過多添加滾動(dòng)條的步驟(親測可用),添加echarts柱狀圖滾動(dòng)條,首先添加js用來判斷當(dāng)前視圖要顯示幾個(gè)及是否顯示滾動(dòng)條,本文結(jié)合實(shí)例代碼介紹的非常詳細(xì),需要的朋友參考下吧2024-01-01javascript的propertyIsEnumerable()方法使用介紹
propertyIsEnumerable()是用來檢測屬性是否屬于某個(gè)對象的,如果檢測到了,返回true,否則返回false,下面為大家介紹下其具體的使用2014-04-04JS/jquery實(shí)現(xiàn)一個(gè)網(wǎng)頁內(nèi)同時(shí)調(diào)用多個(gè)倒計(jì)時(shí)的方法
這篇文章主要介紹了JS/jquery實(shí)現(xiàn)一個(gè)網(wǎng)頁內(nèi)同時(shí)調(diào)用多個(gè)倒計(jì)時(shí)的方法,涉及js與jQuery基于定時(shí)器的時(shí)間相關(guān)操作技巧,需要的朋友可以參考下2017-04-04setTimeout內(nèi)不支持jquery的選擇器的解決方案
在JS中無論是setTimeout還是setInterval,在使用函數(shù)名作為調(diào)用句柄時(shí)都不能帶參數(shù),而在許多場合必須要帶參數(shù),這就需要想方法解決。2015-04-04解決javascript:window.close()在chrome,Firefox下失效的問題
本篇文章是對javascript:window.close()在chrome,Firefox下失效問題的解決方法進(jìn)行了分析介紹。需要的朋友參考下2013-05-05javascript for循環(huán)從入門到偏門(效率優(yōu)化+奇特用法)
for循環(huán)是非?;A(chǔ)的javascript知識,但由于JS太靈活了,所以可能出現(xiàn)一些讓初學(xué)者崩潰的寫法。我決定由淺入深的解釋一下for循環(huán),算是給比我還新手的新手解惑吧,少走彎路2012-08-08For循環(huán)中分號隔開的3部分的執(zhí)行順序探討
這篇文章主要探討了For循環(huán)中分號隔開的3部分的執(zhí)行順序,需要的朋友可以參考下2014-05-05