最近來個(gè)需求,要求給小程序的 modal 增加個(gè)關(guān)閉按鈕,上網(wǎng)一查發(fā)現(xiàn)原來 2018 年就有人給出解決方案了,于是總結(jié)下微信小程序自定義組件的思路:一句話,用 wxml + css實(shí)現(xiàn)和原生組件類似的樣式和效果,之后用 JS 實(shí)現(xiàn)類似原生組件的功能。
比如 modal 組件,觀察可以得出就是個(gè)在頁面頂層顯示的 mask + div。modal 的顯示與否可以通過 wx-if 控制,需要注意的點(diǎn)是 modal 顯示的時(shí)候要有動(dòng)畫效果,這個(gè)功能可以通過 css animation 實(shí)現(xiàn)。
自定義 modal 的示例代碼:
WXML:
<view wx-if="{{cusModalShow}}" bindtap="handleCusModalMaskTap" class="cus-modal">
<view catchtap class="cus-modal__content">
<view class="cus-modal__close-btn" bindtap="handleCusModalMaskTap">
×
</view>
<view class="cus-modal__content-title">
<text>提示</text>
</view>
<view class="cus-modal__content-text">
<text>請(qǐng)確認(rèn)操作</text>
</view>
<view class="cus-modal__content-buttons">
<text bindtap="handleCusModalCancel" class="cus-modal__content-button-cancel">取消</text>
<text bindtap="handleCusModalConfirm" class="cus-modal__content-button-confirm">確認(rèn)</text>
</view>
</view>
</view>
CSS:
@keyframes fade-in {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
.cus-modal {
display: flex;
align-items: center;
justify-content: center;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.6);
animation: fade-in 0.3s ease;
}
.cus-modal__content {
background-color: white;
border-radius: 15px;
width: 80%;
position: relative;
}
.cus-modal__close-btn {
color: rgb(179, 179, 179);
font-size: 1.5rem;
position: absolute;
right: 0.5rem;
top: 0.1rem;
}
.cus-modal__content-title,
.cus-modal__content-text {
font-size: 1.1rem;
text-align: center;
}
.cus-modal__content-title {
font-weight: bold;
margin: 40px 0 20px 0;
}
.cus-modal__content-text {
color: rgb(179, 179, 179);
margin-bottom: 40px;
}
.cus-modal__content-buttons {
display: flex;
}
.cus-modal__content-buttons {
border-top: 1px solid rgb(245, 245, 245);
}
.cus-modal__content-buttons>text {
flex: 1;
text-align: center;
padding: 20px 30px;
border-right: 1px solid rgb(245, 245, 245);
box-sizing: border-box;
height: 60px;
}
.cus-modal__content-buttons>text:last-child {
border-right: none;
}
.cus-modal__content-button-cancel {
font-weight: bold;
}
.cus-modal__content-button-confirm {
font-weight: bold;
color: rgb(90, 117, 155);
}
自定義 Picker:
這里實(shí)際只是模擬了原生 Picker 從底部彈入的效果,具體內(nèi)容可以通過放在里面的組件實(shí)現(xiàn)。
WXML文章來源:http://www.zghlxwxcb.cn/news/detail-666213.html
<view bindtap="closeBottomPicker" wx-if="{{cusPickerShow}}" class="cus-picker">
<view class="cus-picker__mask"></view>
<view catchtap class="cus-picker__content">
<!-- <view class="cus-picker__header">
<text class="cus-pick__header__btn-cancel">取消</text>
<text class="cus-pick__header__btn-confirm">確定</text>
</view> -->
<some-component bindclose="handleCarFilterClose"></some-component>
</view>
</view>
CSS:文章來源地址http://www.zghlxwxcb.cn/news/detail-666213.html
@keyframes slide-up-from-bottom {
0% {
transform: translateY(100%);
}
100% {
transform: translateY(0);
}
}
@keyframes fade-in {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
.cus-picker {
z-index: 114514;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.cus-picker__mask {
position: absolute;
width: 100%;
height: 100%;
left: 0;
top: 0;
background-color: rgba(0, 0, 0, 0.5);
animation: fade-in 0.3s ease;
}
.cus-picker__content {
box-sizing: border-box;
position: absolute;
height: 50%;
width: 100%;
bottom: 0;
background-color: white;
animation: slide-up-from-bottom 0.3s ease;
}
.cus-picker__header {
display: flex;
justify-content: space-between;
padding: 20px;
border-bottom: 1px solid rgb(245, 245, 245);
font-size: 16px;
}
.cus-pick__header__btn-cancel {
color: rgb(127, 127, 127);
}
.cus-pick__header__btn-confirm {
color: rgb(0, 196, 105);
}
到了這里,關(guān)于【微信小程序】記一次自定義微信小程序組件的思路的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!