Confirm

Confirm modal 经常在需要确认一些行为时被使用. 打开Alert modal同样也需要调用以下任一一种App方法:$.confirm(text, [title, callbackOk, callbackCancel])
或者
$.confirm(text, [callbackOk, callbackCancel])

该方法返回动态创建的modal的HTML元素。

{% highlight html %}

Confirm

...
... {% endhighlight %} {% highlight js %} $(document).on('click','.confirm-ok', function () { $.confirm('Are you sure?', function () { $.alert('You clicked Ok button'); }); }); $(document).on('click', '.confirm-ok-cancel',function () { $.confirm('Are you sure?', function () { $.alert('You clicked Ok button'); }, function () { $.alert('You clicked Cancel button'); } ); }); $(document).on('click','.confirm-title-ok', function () { $.confirm('Are you sure?', 'Custom Title', function () { $.alert('You clicked Ok button'); }); }); $(document).on('click','.confirm-title-ok-cancel', function () { $.confirm('Are you sure?', 'Custom Title', function () { $.alert('You clicked Ok button'); }, function () { $.alert('You clicked Cancel button'); } ); }); {% endhighlight %}

Prompt

Prompt modal 经常在需要从用户那里得到一些数据/答案时使用。打开 Prompt modal同样也需要调用以下任一一种App方法:$.prompt(text, [title, callbackOk, callbackCancel])
或者
$.prompt(text, [callbackOk, callbackCancel])

该方法返回动态创建的modal的HTML元素。

{% highlight html %}

Prompt

...
... {% endhighlight %} {% highlight js %} $(document).on('click','.prompt-ok', function () { $.prompt('What is your name?', function (value) { $.alert('Your name is "' + value + '". You clicked Ok button'); }); }); $(document).on('click','.prompt-ok-cancel', function () { $.prompt('What is your name?', function (value) { $.alert('Your name is "' + value + '". You clicked Ok button'); }, function (value) { $.alert('Your name is "' + value + '". You clicked Cancel button'); } ); }); $(document).on('click', '.prompt-title-ok',function () { $.prompt('What is your name?', 'Custom Title', function (value) { $.alert('Your name is "' + value + '". You clicked Ok button'); }); }); $(document).on('click', '.prompt-title-ok-cancel',function () { $.prompt('What is your name?', 'Custom Title', function (value) { $.alert('Your name is "' + value + '". You clicked Ok button'); }, function (value) { $.alert('Your name is "' + value + '". You clicked Cancel button'); } ); }); {% endhighlight %}

自定义Modals

Ok, 所有的预定义modals都只是一些适用于特定的场景的方法. 让我们看看如何创建自定义的 modals:

$.modal(parameters) - 显示 custom modal

该方法返回动态创建的modal的HTML元素。

这里有 Modal 的参数列表:

参数 类型 默认值 描述
title 字符串 可选. Modal 标题 (可以是html字符串)
text 字符串 可选. Modal 文本 (可以是html字符串)
afterText 字符串 可选. 将被放在"text"后的文本 (可以是html字符串)
buttons 数组 可选. 按钮数组. 每个按钮应该被表示为带按钮参数的对象 (看下面)
verticalButtons boolean false Optional. Set to true to enable vertical buttons layout
onClick 函数 可选. 回调函数将在用户点击任何Modal按钮后被触发执行. 它接收 modal (Modal的 HTML元素) 和 index作为参数 (被点击按钮的索引号)

让我们一起来看看按钮参数:

参数 类型 默认值 描述
text 字符串 按钮文本 (可以是 HTML 字符串)
bold 布尔值 false 可选. 设置为true会加粗按钮文本
close 布尔值 true 可选. 如果是“true”,点击这个按钮后modal会被关闭
onClick 函数 可选. 用户点击这个按钮后,回调函数会被执行

这样的配置选项允许我们创建灵活的modals。让我们来看一些例子

{% highlight html %}

自定义Modals

...
... {% endhighlight %} {% highlight js %} $(document).on('click','.open-3-modal', function () { $.modal({ title: 'Modal with 3 buttons', text: 'Vivamus feugiat diam velit. Maecenas aliquet egestas lacus, eget pretium massa mattis non. Donec volutpat euismod nisl in posuere. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae', buttons: [ { text: 'B1', onClick: function() { $.alert('You clicked first button!') } }, { text: 'B2', onClick: function() { $.alert('You clicked second button!') } }, { text: 'B3', bold: true, onClick: function() { $.alert('You clicked third button!') } }, ] }) }); $(document).on('click','.open-slider-modal', function () { var modal = $.modal({ title: 'Awesome Photos?', text: 'What do you think about my photos?', afterText: '
'+ '
'+ '
'+ '
' + '
'+ '
'+ '
', buttons: [ { text: 'Bad :(' }, { text: 'Awesome!', bold: true, onClick: function () { $.alert('Thanks! I know you like it!') } }, ] }) $.swiper($$(modal).find('.swiper-container'), {pagination: '.swiper-pagination'}); }); $(document).on('click','.open-tabs-modal', function () { $.modal({ title: '
'+ 'Tab 1'+ 'Tab 2'+ '
', text: '
'+ '
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam convallis nunc non dolor euismod feugiat. Sed at sapien nisl. Ut et tincidunt metus. Suspendisse nec risus vel sapien placerat tincidunt. Nunc pulvinar urna tortor.
'+ '
Vivamus feugiat diam velit. Maecenas aliquet egestas lacus, eget pretium massa mattis non. Donec volutpat euismod nisl in posuere. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae
'+ '
', buttons: [ { text: 'Ok, got it', bold: true }, ] }) }); $(document).on('click','.open-vertical-modal', function () { $.modal({ title: 'Vertical Buttons Layout', text: 'Vivamus feugiat diam velit. Maecenas aliquet egestas lacus, eget pretium massa mattis non. Donec volutpat euismod nisl in posuere. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae', verticalButtons: true, buttons: [ { text: 'Button 1', onClick: function() { $.alert('You clicked first button!') } }, { text: 'Button 2', onClick: function() { $.alert('You clicked second button!') } }, { text: 'Button 3', onClick: function() { $.alert('You clicked third button!') } }, ] }) }); {% endhighlight %}

用 JavaScript 关闭 Modals

任何 Modal 可以用 JavaScript 来关闭,不仅仅是通过点击按钮。因此我们需要看看相关App方法:

$.closeModal(modal) - 关闭任意 modal

Modal 事件

如果你希望能自定义 modal 模板,你可在 初始化APP 的时候传入一个 modalTemplate 参数。这个参数的值是一个 Template7 模板,当编译该模板的时候会传入一个 groups 参数。

这个模板可能是这样的:

{% highlight html %} {% endhighlight %}

带标题的加载指示器

预加载 Modal 用来提示一些后台活动(像Ajax请求)和阻止在这个活动期间的任何用户操作。 打开预加载 modal 我们也需要调用适当的App方法: $.show预加载([title])- 显示 预加载 modal

该方法返回动态创建的modal的HTML元素。

$.hide预加载() - 隐藏/关闭 预加载 modal. 因为 预加载 modal 没有任何按钮, 它应该用 JavaScript 来关闭

{% highlight html %}

预加载 Modal (Preloader Modal)

...
... {% endhighlight %} {% highlight js %} $(document).on('click', '.open-preloader', function () { $.showPreloader(); setTimeout(function () { $.hidePreloader(); }, 2000); }); $(document).on('click','.open-preloader-title', function () { $.showPreloader('Custom Title') setTimeout(function () { $.hidePreloader(); }, 2000); }); {% endhighlight %}

迷你指示器

如果你不需要用像预加载Modal这样如此大的modal 窗口去指示活动, 你可以使用简单并且小的指示器modal:

$.showIndicator() - 显示指示器 modal

$.hideIndicator() - 隐藏/关闭指示器 modal. 因为指示器modal没有任何按钮, 它需要用JavaScript来关闭

{% highlight html %}

指示器(indicator)

...
... {% endhighlight %} {% highlight js %} $(document).on('click','.open-indicator', function () { $.showIndicator(); setTimeout(function () { $.hideIndicator(); }, 2000); }); {% endhighlight %}

toast

toast是一种轻量的提示,在页面中间显示,并且会在2秒(默认值,可修改)之后自动消失。

可以用来显示一些不会打断用户操作的提示。

{% highlight html%} /* msg{string}: toast内容 * duration{number}:toast显示时间,默认2000 * extraclass{string}:给toast根节点附加class,高度自定义属性,方便用户自行控制不同场景的样式。 * 如果使用了第三个参数,请自行在业务css里添加extraclass对应的样式 * / {% endhighlight %} {% highlight js %} $.toast("操作失败"); $.toast('操作成功,正在跳转...', 2345, 'success top'); {% endhighlight %}