微信小程序实现Modal弹框的方法及过程是什么
Admin 2022-08-11 群英技术资讯 971 次浏览
1、在根目录下自定义一个components文件夹,用来存放自定义的组件。
2、再针对每一个组件创建一个文件夹,用来存放这个组件相关的文件。
3、在指定组件的文件夹中右键->新建Component创建组件。这样创建的目的是在json文件中添加"component": true,将其声明为一个组件。
下面开始例子:
1、组件页面 index.wxml
<!-- 模态框 --> <!-- 遮罩层 --> <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> <!-- 关闭图标 --> <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节点 --> <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 选项从基础库版本 2.6.5 开始支持。它支持以下取值: isolated 表示启用样式隔离,在自定义组件内外,使用 class 指定的样式将不会相互影响(一般情况下的默认值); apply-shared 表示页面 wxss 样式将影响到自定义组件,但自定义组件 wxss 中指定的样式不会影响页面; shared 表示页面 wxss 样式将影响到自定义组件,自定义组件 wxss 中指定的样式也会影响页面和其他设置了 apply-shared 或 shared 的自定义组件。(这个选项在插件中不可用。) */ styleIsolation: 'isolated' }, /** * 组件的初始数据 */ data: { windowWidth: 0, windowHeight: 0, }, /** * 生命周期函数 */ ready: function() { var _this = this; wx.getSystemInfo({ success: function(res) { _this.setData({ windowWidth: res.windowWidth, windowHeight: res.windowHeight, }); } }); }, /** * 组件的属性列表 */ properties: { //是否显示关闭图标 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 节点,请确认 selector 及 context 是否正确'); } }; Modal.show = (options) => Modal(options); export default Modal;
6、使用方法
需要用到modal的页面引入modal组件:
{ "usingComponents": { "cus-modal": "../../components/modal/index" } }
页面加入modal节点:
<cus-modal id="testModal"> <!-- 内容 --> <view style="text-align: center;"> <!-- ...... --> </view> </cus-modal>
在页面的js中弹出modal窗口:
//引入modal组件 import Modal from '../../components/modal/modal'; //在代码中调用 Modal.show({ selector: '#testModal' });
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
delete属性是在JavaScript中经常使用的删除对象的一个属性,但是对于新手用户们来说却不是很了解其使用方法,那么接下来我们就一起去看看如何使用JavaScript delete属性的吧。
这篇文章给大家详细的介绍vue的diff算法,对很多新手来说vue的diff算法是比较难理解的内容,下面小编就分享一些实例,对大家学习vue的diff算法有一定的帮助,接下来一起跟随小编看看吧。
JavaScript中求数组平均数的思路和方法是什么?对于求平均数,大家都很熟悉,那么对与不了解数组个数的求平均数,该怎样用代码来实现呢?下文给大家介绍一个方法,有需要的朋友可以参考,接下来就跟随小编来一起学习一下吧!
这篇文章给大家分享的是用JS实现动态的验证码干扰效果,有很多验证码干扰都做了这个效果,小编觉得挺实用的,因此分享给大家做个参考,文中示例代码介绍的非常详细,感兴趣的朋友接下来一起跟随小编看看吧。
在实际项目中,设置时间范围的功能还是比较常见的,在很多数据多,需要做筛选的场景都应用,那么我们实现设置时间范围的功能有什么方法呢?本文给大家分享用JS实现设置时间范围的功能,感兴趣的朋友就继续往下看吧。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008