modal-jngcs 简单的移动端modal插件

modal.js Guidance

项目地址
功能体验
modal.js is a mobile-browser plugin build with HTML CSS and JavaScript.

It provide the ability to show the model in your mobile-browser, and listen the basic events.

You can also make your own style modal whether the plugin’s style.

The modal.js rely on pure js,you can run it without any frame.

Quick Start

  1. import js

    <!-- v0需要粘贴html,v1不需要粘贴html,两者不互相兼容 -->
    <!-- v0.x.x -->
    <script src="https://jngcs.top/common/modalv0/modal.min.js"></script>
    <!-- v1.x.x -->
    <script src="https://jngcs.top/common/modalv1/modal.min.js"></script>
    
  2. import css

    <link href="https://jngcs.top/common/modal/modal.min.css" rel="stylesheet" />
    
  3. if you use modalv0, copy code below, then paste under the <body> element and must under the <body>

    if you use modalv1, you needn’t copy the html.

    <!-- modal -->
    <div id="mask" style="display: none;"></div>
    <div id="modal" style="display: none;">
        <div class="modal-content">
    	<div class="modal-title"></div>
    	    <div class="modal-msg">哈哈哈</div>
    	</div>
    	<div class="modal-button"></div>
    </div>
    

Function

  • Modal.set()

    Initialize the modal.

  • Modal.set().show()

    show the modal. must call set() before show() like Modal.set().show();

  • Modal.hide()

    hide the modal.

Options

function set()
PropertyTypeDefault valueDescription
typestringtoastmodal type.
content_titlestringnonemodal title.
content_msgstringmodal content message.
cancel_textstring取消text of cancel button.
confirm_textstring确定text of confirm button.
need_rebuildnumber0is modal need rebuild.
toast_timenumber1500show time while type = ‘toast’.
animationstringfadeanimation.
  • legal value of type

    ValueDescription
    toastno button, timeout with 1500ms.
    alertwith only confirm button, no timeout.
    confirmwith cancel and confirm buttons, no timeout.
  • legal value of content_title

    ValueDescription
    nonedon’t need modal title
  • legal value of need_rebuild

    ValueDescription
    0use modal default style.
    1rebuild your style modal, you can rewrite HTML, CSS.
  • legal value of animation

    ValueDescription
    fademodal fade slowly.
    popmodal pop up and fall down slowly.
function show()
PropertyTypeDefault valueDescription
successfunctioncallback after success
failfunctioncallback after fail

How To Listen

If you use the default style, modal.js provider the className for you.

alert type

modal.js provide className .modal-btn-confirm in confirm button.

confirm type

modal.js provide classNmae .modal-btn-cancel and .modal-btn-confirm .

You can listen the events by className in your business logic like this:

var confirmBtn = document.getElementsByClassName('modal-btn-confirm')[0];
confirmBtn.addEventListener('click', function() {
    // DO SOMETHING HERE
    Modal.hide();
})

If you use mui to listen like this:

mui('#modal').on('tap', '.modal-btn-cancel', function() {
	console.log('cancel');
	Modal.hide();
});

Remember to use mui().off()to cancel the listener after your work, or the code in listener will be called many times.

Example

// 在业务逻辑中显示模态框
Modal.set({
	type: 'alert',
	content_title: 'title',
	content_msg: 'message',
    animation: 'fade'
}).show({
	success: function(res) {
		console.log(res);
        // 监听modal.js提供的按钮class,并执行业务,最后关闭modal
        var confirmBtn = document.getElementsByClassName('modal-btn-confirm')[0];
		confirmBtn.addEventListener('click', function() {
    		// DO SOMETHING HERE
    		Modal.hide();
		})
	},
    fail: function(res) {
        console.log(res);
        // ...
        Modal.hide();
    }
});

Attention ☆

  • The Id and className modal.js used:

    #mask
    #modal
    .modal-content
    .modal-title
    .modal-msg
    .modal-button
    .modal-btn-cancel
    .modal-btn-confirm
    @keyframes fade
    @keyframes pop
    @keyframes mask-show
    

    make sure not to use them in other place.

  • If you use the default style, don’t modify the html and css in Quict Start.

  • Call function set() before use show(), otherwise the modal will not work.

  • If you use mui to listen, remember to use mui().off()to cancel the listener, or the code in listener will be called many times.

FAQ

  1. Q: The animation didn’t work?

    A: modal.js is still in Alpha edition, something maybe is testing.

  2. Q: How to rebuild?

    A: Use the property need_rebuild: 1 in set(). Then you can modify html and css you like.

Update Notes

v0.0.2 Alpha(13/3/2020):

  • 新增错误捕获功能
  • 删除show()函数的默认成功回调函数内部功能

v0.0.3 Alpha(13/3/2020):

  • 新增链式调用顺序要求

v0.1.1 Alpha(14/3/2020):

  • 新增动画效果
  • 修复同一页面下存在有或无标题的两种模态框情况下,导致消息高度变化的问题

v0.1.2 Alpha(15/3/2020):

  • 修复mask动画短暂黑屏的问题
  • 修复由于未对对象进行深度复制导致内容异常的问题
  • 优化show()函数成功后回调函数的执行顺序

v0.1.3 Alpha(18/3/2020)

  • 新增modal离场动画
  • 优化modal圆角尺寸
  • 删除hide()函数的回调函数

v0.1.4 Alpha(19/3/2020)

  • 修复真机情况下modal离场动画不起效的问题

v0.2.0 Beta(22/3/2020)

  • 修复了show()函数中回调函数深度复制后出错的问题
  • 版本迭代到Beta版

v1.0.0 Beta (22/4/2020)

  • 新增页面dom监听,动态插入modal元素,不用再粘贴html代码。
  • 由于dom的监听事件,不再支持IE8以下浏览器。
  • 1.0.0版本为大版本更新,不向前兼容。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Bootstrap 3 的模态框中,可以通过 JavaScript 动态设置 modal-body 的高度。具体的实现方法如下: 1. 首先,为 modal-body 设置一个固定的最大高度,例如: ``` .modal-body { max-height: 400px; overflow-y: auto; } ``` 这里设置了最大高度为 400px,当 modal-body 的内容超过 400px 时,modal-body 就会出现滚动条。 2. 在 JavaScript 中获取 modal-body 的高度和模态框的高度,然后计算出 modal-body 的实际高度,例如: ``` var modalBody = document.querySelector('.modal-body'); var modalDialog = document.querySelector('.modal-dialog'); var modalHeight = modalDialog.clientHeight - 60; // 60 是 modal-body 的 padding-top 和 padding-bottom 之和 var modalBodyHeight = Math.min(modalBody.scrollHeight, modalHeight); ``` 这里首先获取了 modal-body 和 modal-dialog 的元素,然后通过 clientHeight 属性获取了 modal-dialog 的高度,并减去了 modal-body 的 padding-top 和 padding-bottom 的值,得到了 modal-body 可用的高度。然后通过 scrollHeight 属性获取了 modal-body 的内容实际高度,使用 Math.min 方法将这两个高度的较小值作为 modal-body 的高度。 3. 最后,将计算出来的 modal-body 高度赋值给 modal-body 的样式中: ``` modalBody.style.height = modalBodyHeight + 'px'; ``` 这样就可以动态设置 modal-body 的高度了。完整的 JavaScript 代码如下: ``` var modalBody = document.querySelector('.modal-body'); var modalDialog = document.querySelector('.modal-dialog'); var modalHeight = modalDialog.clientHeight - 60; var modalBodyHeight = Math.min(modalBody.scrollHeight, modalHeight); modalBody.style.height = modalBodyHeight + 'px'; ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值