Modal的显示与隐藏

核心思想

页面完成加载后,为所有含有data-toggle="modal"的元素绑定点击事件

模态框出现的逻辑:先出现遮罩层,再出现模态框

  1. 为body增加"modal-open"类,作用是给body增加overflow:hidden隐藏页面的滚动条。给模态框增加overflow-x:hidden;overflow-y:auto美化样式
  2. 调用dropback函数,变量shown为true,所以插入出现遮罩层(若遮罩层需要过渡,过渡的逻辑也在这里)
  3. 在传给dropback的回调里(即完成遮罩层出现后),调用$el.show()方法使模态框出现(display:block),再增加"in"类使他过渡的出现

模态框的隐藏逻辑:先隐藏模态框,再隐藏遮罩层

  1. 先去除"in“类,使模态框opacity变为0先去除”in"类,使模态框opacity变为0
  2. 在过渡效果的完成后用$el.hide()隐藏模态框 调用dropback函数,变量shown为false,所以移除遮罩层
  3. 在出给dropback的回调里(即完成遮罩层的隐藏后),清除"modal-open"类
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个大体的实现思路。 首先,在vue2+ts+sass环境下,可以通过在组件中定义props属性,来接收组件需要的参数。对于高度和宽度的设置,可以分别定义一个props属性: ```typescript props: { modalHeight: { type: String, default: 'auto' }, modalMaxWidth: { type: String, default: 'none' }, modalMinWidth: { type: String, default: 'none' }, modalShow: { type: Boolean, default: false }, title: { type: String, default: '' }, confirmText: { type: String, default: '确定' }, cancelText: { type: String, default: '取消' } } ``` 接着,在组件的模板中,可以使用flex布局来实现对话框的自适应宽度。具体实现如下: ```html <template> <div class="modal-wrapper" v-show="modalShow"> <div class="modal-overlay"></div> <div class="modal-content" :style="{ 'max-width': modalMaxWidth, 'min-width': modalMinWidth, 'height': modalHeight, 'width': '100%' }" > <div class="modal-header">{{ title }}</div> <div class="modal-body"> <slot></slot> </div> <div class="modal-footer"> <button class="modal-cancel" @click="$emit('update:modalShow', false)">{{ cancelText }}</button> <button class="modal-confirm" @click="$emit('update:modalShow', false)">{{ confirmText }}</button> </div> </div> </div> </template> ``` 在上述代码中,我们在modal-content元素上设置了max-width和min-width属性,同时设置了height和width属性。height属性可以通过props接收到的modalHeight属性来设置,而width属性则设置为100%。这样子,modal-content元素的宽度就会自适应其父元素的宽度了。 接下来,我们可以在modal-body元素上设置overflow-y属性,使得内容区域超出modal-height时可以出现纵向滚动条。而对于遮罩层和显示隐藏的逻辑,可以使用v-show指令和CSS来实现。 最后,在底部的“取消”和“确定”按钮上,我们可以通过slots插槽来实现。 ```html <template> <div class="modal-wrapper" v-show="modalShow"> <div class="modal-overlay"></div> <div class="modal-content" :style="{ 'max-width': modalMaxWidth, 'min-width': modalMinWidth, 'height': modalHeight, 'width': '100%' }" > <div class="modal-header">{{ title }}</div> <div class="modal-body" :style="{'overflow-y': modalHeight === 'auto' ? 'visible' : 'auto'}"> <slot></slot> </div> <div class="modal-footer"> <button class="modal-cancel" @click="$emit('update:modalShow', false)">{{ cancelText }}</button> <button class="modal-confirm" @click="$emit('update:modalShow', false)">{{ confirmText }}</button> </div> </div> </div> </template> ``` 综上所述,我们可以通过定义props属性和使用flex布局来实现一个支持高度、宽度自适应,以及遮罩层、标题、底部按钮等功能的对话框modal组件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值