最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

微信小程序自定義Modal彈框

 更新時間:2022年07月08日 07:38:02   作者:lluohuih  
這篇文章主要為大家詳細(xì)介紹了微信小程序自定義Modal彈框效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了微信小程序自定義Modal彈框的具體代碼,供大家參考,具體內(nèi)容如下

一、創(chuàng)建組件

1、在根目錄下自定義一個components文件夾,用來存放自定義的組件。
2、再針對每一個組件創(chuàng)建一個文件夾,用來存放這個組件相關(guān)的文件。

3、在指定組件的文件夾中右鍵->新建Component創(chuàng)建組件。這樣創(chuàng)建的目的是在json文件中添加"component": true,將其聲明為一個組件。

下面開始例子:

1、組件頁面 index.wxml

<!-- 模態(tài)框 -->
<!-- 遮罩層 -->
<view class="components-modal-overlay-view" style="width: {{ windowWidth }}px; height: {{ windowHeight }}px; display: {{ show ? 'block' : 'none' }};"></view>

<view class="col-center" style="width: {{ windowWidth }}px; height: {{ windowHeight }}px; display: {{ show ? 'flex' : 'none' }};">
? ? <view>
? ? ? ? <!-- 關(guān)閉圖標(biāo) -->
? ? ? ? <view class="components-modal-close-view" style="display: {{ showCloseIcon ? 'block' : 'none' }};">
? ? ? ? ? ? <image bindtouchend="hideCusModal" src="./images/close-white2x.png" style="width: 24px; height: 24px;"></image>
? ? ? ? </view>
? ? ? ? <view class="{{ showContentStyle ? 'components-modal-content-view' : '' }}">
? ? ? ? ? ? <!-- slot表示可以插入wxml節(jié)點 -->
? ? ? ? ? ? <slot></slot>
? ? ? ? </view>
? ? </view>
</view>

2、組件樣式 index.wxss

/* components/modal/index.wxss */
.components-modal-overlay-view {
? ? background-color: #000000;
? ? opacity: 0.5;
? ? position: fixed;
? ? z-index: 10;
}
.components-modal-close-view ?{
? ? text-align: right;
? ? margin-bottom: 5px;
}
.col-center {
? ? position: fixed;
? ? z-index: 11;
? ? display: flex;
? ? flex-direction: column;
? ? justify-content: center;
? ? align-items: center;
}
.components-modal-content-view {
? ? background: #FFFFFF;
? ? border-radius: 8px;
? ? display: flex;
? ? flex-direction: column;
? ? justify-content: center;
? ? padding: 20px;
}

3、組件json配置 index.json

{
? ? "component": true,
? ? "usingComponents": {}
}

4、組件頁面的js index.js

// components/modal/index.js
Component({
? ? options: {
? ? ? ? /**
? ? ? ? ? ? styleIsolation 選項從基礎(chǔ)庫版本 2.6.5 開始支持。它支持以下取值:
? ? ? ? ? ? ? ? isolated 表示啟用樣式隔離,在自定義組件內(nèi)外,使用 class 指定的樣式將不會相互影響(一般情況下的默認(rèn)值);
? ? ? ? ? ? ? ? apply-shared 表示頁面 wxss 樣式將影響到自定義組件,但自定義組件 wxss 中指定的樣式不會影響頁面;
? ? ? ? ? ? ? ? shared 表示頁面 wxss 樣式將影響到自定義組件,自定義組件 wxss 中指定的樣式也會影響頁面和其他設(shè)置了 apply-shared 或 shared 的自定義組件。(這個選項在插件中不可用。)
? ? ? ? ?*/
? ? ? ? styleIsolation: 'isolated'
? ? },
? ? /**
? ? ?* 組件的初始數(shù)據(jù)
? ? ?*/
? ? data: {
? ? ? ? windowWidth: 0,
? ? ? ? windowHeight: 0,
? ? },
? ? /**
? ? ?* 生命周期函數(shù)
? ? ?*/
? ? ready: function() {
? ? ? ? var _this = this;
? ? ? ? wx.getSystemInfo({
? ? ? ? ? ? success: function(res) {
? ? ? ? ? ? ? ? _this.setData({
? ? ? ? ? ? ? ? ? ? windowWidth: res.windowWidth,
? ? ? ? ? ? ? ? ? ? windowHeight: res.windowHeight,
? ? ? ? ? ? ? ? });
? ? ? ? ? ? }
? ? ? ? });
? ? },
? ? /**
? ? ?* 組件的屬性列表
? ? ?*/
? ? properties: {
? ? ? ? //是否顯示關(guān)閉圖標(biāo)
? ? ? ? showCloseIcon: {
? ? ? ? ? ? type: Boolean,
? ? ? ? ? ? value: true
? ? ? ? },
? ? ? ? //是否顯示Content樣式(白色底色,圓角等)
? ? ? ? showContentStyle: {
? ? ? ? ? ? type: Boolean,
? ? ? ? ? ? value: true
? ? ? ? },
? ? ? ? show: {
? ? ? ? ? ? type: Boolean,
? ? ? ? ? ? value: false
? ? ? ? },
? ? },
? ? /**
? ? ?* 組件的方法列表
? ? ?*/
? ? methods: {
? ? ? ? /**
? ? ? ? ?* 隱藏Modal
? ? ? ? ?*/
? ? ? ? hideCusModal: function(){
? ? ? ? ? ? this.setData({
? ? ? ? ? ? ? ? show: false,
? ? ? ? ? ? });
? ? ? ? }
? ? }
})

5、組件js modal.js

const defaultOptions = {
? ? show: false,
? ? selector: '#cus-modal',
? ? showCloseIcon: true,
? ? showContentStyle: true,
};
let currentOptions = Object.assign({}, defaultOptions);
function getContext() {
? ? const pages = getCurrentPages();
? ? return pages[pages.length - 1];
}
const Modal = (options) => {
? ? options = Object.assign(Object.assign({}, currentOptions), options);
? ? const context = options.context || getContext();
? ? const modal = context.selectComponent(options.selector);
? ? delete options.context;
? ? delete options.selector;
? ? if (modal) {
? ? ? ? modal.setData(options);
? ? ? ? wx.nextTick(() => {
? ? ? ? ? ? modal.setData({ show: true });
? ? ? ? });
? ? }
? ? else {
? ? ? ? console.warn('未找到 cus-modal 節(jié)點,請確認(rèn) selector 及 context 是否正確');
? ? }
};
Modal.show = (options) => Modal(options);
export default Modal;

6、使用方法

需要用到modal的頁面引入modal組件:

{
? ? "usingComponents": {
? ? ? ? "cus-modal": "../../components/modal/index"
? ? }
}

頁面加入modal節(jié)點:

<cus-modal id="testModal">
? ? <!-- 內(nèi)容 -->
? ? <view style="text-align: center;">
? ? ? ? <!-- ...... -->
? ? </view>
</cus-modal>

在頁面的js中彈出modal窗口:

//引入modal組件
import Modal from '../../components/modal/modal';

//在代碼中調(diào)用
Modal.show({
? ? selector: '#testModal'
});

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

肇庆市| 上杭县| 建阳市| 永济市| 井冈山市| 交城县| 平凉市| 乌拉特前旗| 海原县| 怀来县| 焉耆| 泰和县| 青阳县| 克什克腾旗| 翼城县| 曲沃县| 琼结县| 赤壁市| 弋阳县| 榆社县| 东光县| 乌拉特后旗| 湘潭市| 敦煌市| 宁国市| 思茅市| 齐河县| 密云县| 门源| 珠海市| 铅山县| 吴旗县| 永州市| 铁岭县| 治多县| 乐山市| 桦甸市| 华蓥市| 西充县| 江川县| 二连浩特市|