Modal
Modal 是从App的主要内容区域上弹出的一小块内容块,是“临时视图”的一部分。
多个Modal类组件(包括toast)同时被呼起时,会按先后顺序被缓存在队列中,前一个modal关闭后,下一个modal才会打开
Modals 可以只用JavaScript打开。所以让我们来看看使用modals的相关APP方法
预定义的 Modals
- 1.注意,如果你没有指定预定义的modal标题,它讲使用默认的标题,这个可以在App 初始化时,通过传递 modalTitle 参数改变。
- 2.预定义modals的按钮文本(如 "Ok" 和 "Cancel") 也可以在 App 初始化时,通过传递 modalButtonOk 和 modalButtonCancel 参数改变。
首先,让我们看看已经预定义的最常用的modal:
Alert
我们需要调用以下任一一种App方法来打开Alert modal :$.alert(text, [title, callbackOk])
或者 $.alert(text, [callbackOk])
text
- string. Alert文本
title
- string. Optional. Alert modal 标题
callbackOk
- function. Optional.在Alert modal下,当用户点击“Ok”按钮时,回调函数将被执行。
该方法返回动态创建的modal的HTML元素。
{% highlight html %}
...
...
{% endhighlight %}
{% highlight js %}
$(function(){
$(document).on('click','.alert-text',function () {
$.alert('Here goes alert text');
});
$(document).on('click','.alert-text-title', function () {
$.alert('Here goes alert text', 'Custom Title!');
});
$(document).on('click', '.alert-text-title-callback',function () {
$.alert('Here goes alert text', 'Custom Title!', function () {
$.alert('Button clicked!')
});
});
$(document).on('click', '.alert-text-callback',function () {
$.alert('Here goes alert text', function () {
$.alert('Button clicked!')
});
});
})
{% endhighlight %}
Confirm
Confirm modal 经常在需要确认一些行为时被使用. 打开Alert modal同样也需要调用以下任一一种App方法:$.confirm(text, [title, callbackOk, callbackCancel])
或者
$.confirm(text, [callbackOk, callbackCancel])
text - string. Confirm 文本
title - string. Optional
. Confirm modal 标题
- callbackOk - function. Optional. 在 Confirm modal下,当用户点击“Ok”按钮时,回调函数将被执行。(当用户确认操作)
callbackCancel - function. Optional
. 在 Confirm modal下,当用户点击“Cancel”按钮时,回调函数将被执行。(当用户不想进行操作)
该方法返回动态创建的modal的HTML元素。
{% highlight html %}
...
...
{% 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])
text - string
. Prompt 文本/问题
title - string
. Optional. Prompt modal 标题
callbackOk - function
. Optional. 在 Prompt modal下,当用户点击“Ok”按钮时,回调函数将被执行。回调函数的参数是输入框的值
callbackCancel - function
. Optional. 在 Prompt modal下,当用户点击“Cancel”按钮时,回调函数将被执行。回调函数的参数是输入框的值
该方法返回动态创建的modal的HTML元素。
{% highlight html %}
...
...
{% 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
parameters - object
. Modal 的 parameters/options对象
该方法返回动态创建的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 %}
...
...
{% 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: '',
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 - HTMLElement or string ( CSS 选择器)
. 可选. 除了指定的,任何被打开modal都将被关闭。
Modal 事件
如果你希望能自定义 modal 模板,你可在 初始化APP 的时候传入一个 modalTemplate 参数。这个参数的值是一个 Template7 模板,当编译该模板的时候会传入一个 groups 参数。
这个模板可能是这样的:
{% highlight html %}
{% endhighlight %}
带标题的加载指示器
预加载 Modal 用来提示一些后台活动(像Ajax请求)和阻止在这个活动期间的任何用户操作。 打开预加载 modal 我们也需要调用适当的App方法: $.show预加载([title]
)- 显示 预加载 modal
title - string. Optional
. 预加载 modal 标题. 默认(没有指定)的时候,它等同于 "Loading...". 你可以在App初始化时通过 modal预加载Title 参数改变默认的 预加载 标题。
该方法返回动态创建的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 %}
...
...
{% 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 %}