mirror of
https://gitee.com/vant-contrib/vant-weapp.git
synced 2025-04-06 03:58:05 +08:00
parent
1951941145
commit
d36f5d5118
@ -5,5 +5,36 @@ Page(Object.assign({}, Zan.Toast, {
|
|||||||
|
|
||||||
showToast() {
|
showToast() {
|
||||||
this.showZanToast('toast的内容');
|
this.showZanToast('toast的内容');
|
||||||
|
},
|
||||||
|
|
||||||
|
showIconToast() {
|
||||||
|
this.showZanToast({
|
||||||
|
title: 'toast的内容',
|
||||||
|
icon: 'fail'
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
showImageToast() {
|
||||||
|
this.showZanToast({
|
||||||
|
title: 'toast的内容',
|
||||||
|
image: 'https://b.yzcdn.cn/v2/image/dashboard/secured_transaction/suc_green@2x.png'
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
showLoadingToast() {
|
||||||
|
this.showZanToast({
|
||||||
|
title: 'toast的内容',
|
||||||
|
icon: 'loading'
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
showOnlyIcon() {
|
||||||
|
this.showZanToast({
|
||||||
|
icon: 'fail'
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
showLoading() {
|
||||||
|
this.showZanLoading('加载中');
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
@ -4,10 +4,30 @@
|
|||||||
|
|
||||||
<view class="doc-title zan-hairline--bottom">TOAST</view>
|
<view class="doc-title zan-hairline--bottom">TOAST</view>
|
||||||
|
|
||||||
<view class="zan-btns" style="margin-top: 30vh;">
|
<view class="zan-btns" style="margin-top: 15vh;">
|
||||||
<button class="zan-btn" bindtap="showToast">
|
<button class="zan-btn" bindtap="showToast">
|
||||||
显示toast
|
显示toast
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
|
<button class="zan-btn" bindtap="showIconToast">
|
||||||
|
显示 Icon 图标的toast
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<button class="zan-btn" bindtap="showImageToast">
|
||||||
|
自定义图片作为图标的toast
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<button class="zan-btn" bindtap="showLoadingToast">
|
||||||
|
显示 Loading toast
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<button class="zan-btn" bindtap="showOnlyIcon">
|
||||||
|
只显示图标的toast
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<button class="zan-btn" bindtap="showLoading">
|
||||||
|
显示 Loading
|
||||||
|
</button>
|
||||||
</view>
|
</view>
|
||||||
</view>
|
</view>
|
||||||
|
|
||||||
|
@ -26,4 +26,39 @@ Page(extend({}, Toast, {
|
|||||||
在 js 中直接调用 this.showZanToast 即可
|
在 js 中直接调用 this.showZanToast 即可
|
||||||
```js
|
```js
|
||||||
this.showZanToast('toast的内容');
|
this.showZanToast('toast的内容');
|
||||||
|
|
||||||
|
this.showZanToast({
|
||||||
|
title: 'toast的内容'
|
||||||
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Toast 支持在文字上展示图标,用法如下
|
||||||
|
```js
|
||||||
|
this.showZanToast({
|
||||||
|
title: 'toast的内容',
|
||||||
|
// icon 仅支持 Icon 组件内提供的
|
||||||
|
icon: 'fail'
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
Toast 组件扩展了一个 showZanLoading 的方法,快速展示加载中
|
||||||
|
```js
|
||||||
|
this.showZanLoading('toast的内容');
|
||||||
|
```
|
||||||
|
|
||||||
|
### 参数说明
|
||||||
|
|
||||||
|
#### 方法
|
||||||
|
| 方法名 | 参数 | 返回值 | 介绍 |
|
||||||
|
|-----------|-----------|-----------|-------------|
|
||||||
|
| showZanToast | `title \| options`, `timeout` | - | 展示提示 |
|
||||||
|
| showZanLoading | `title \| options` | - | 展示加载提示 |
|
||||||
|
| clearZanToast | | - | 关闭提示 |
|
||||||
|
|
||||||
|
#### options 具体参数如下
|
||||||
|
| 参数 | 说明 | 类型 | 默认值 | 必须 |
|
||||||
|
|-----------|-----------|-----------|-------------|-------------|
|
||||||
|
| title | toast 显示文案 | String | - | |
|
||||||
|
| icon | toast 显示图标,仅支持 Icon 组件内提供的和 `loading` | String | - | |
|
||||||
|
| image | toast 显示图标,为图片的链接,传入此值后会覆盖 icon 值 | String | - | |
|
||||||
|
| timeout | toast 显示时间,小于0则会一直显示,需要手动调用 clearZanToast 清除 | Number | - | |
|
||||||
|
@ -1,32 +1,66 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
showZanToast(title, timeout) {
|
showZanToast(title, timeout) {
|
||||||
var zanToast = this.data.zanToast || {};
|
const options = formatParameter(title, timeout);
|
||||||
clearTimeout(zanToast.timer);
|
|
||||||
|
// 清除上一轮的计时器
|
||||||
|
const { timer = 0 } = this.data.zanToast || {};
|
||||||
|
clearTimeout(timer);
|
||||||
|
|
||||||
// 弹层设置~
|
// 弹层设置~
|
||||||
zanToast = {
|
const zanToast = {
|
||||||
show: true,
|
show: true,
|
||||||
title
|
icon: options.icon,
|
||||||
|
image: options.image,
|
||||||
|
title: options.title
|
||||||
};
|
};
|
||||||
this.setData({
|
this.setData({
|
||||||
zanToast
|
zanToast
|
||||||
});
|
});
|
||||||
|
|
||||||
var timer = setTimeout(() => {
|
// 传入的显示时长小于0,就认为需要一直显示
|
||||||
|
if (timeout < 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 下一轮计时器
|
||||||
|
const nextTimer = setTimeout(() => {
|
||||||
this.clearZanToast();
|
this.clearZanToast();
|
||||||
}, timeout || 3000);
|
}, timeout || 3000);
|
||||||
|
|
||||||
this.setData({
|
this.setData({
|
||||||
'zanToast.timer': timer
|
'zanToast.timer': nextTimer
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// 清除所有 toast
|
||||||
clearZanToast() {
|
clearZanToast() {
|
||||||
var zanToast = this.data.zanToast || {};
|
const { timer = 0 } = this.data.zanToast || {};
|
||||||
clearTimeout(zanToast.timer);
|
clearTimeout(timer);
|
||||||
|
|
||||||
this.setData({
|
this.setData({
|
||||||
'zanToast.show': false
|
'zanToast.show': false
|
||||||
});
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
// 快捷方法,显示 loading
|
||||||
|
showZanLoading(title) {
|
||||||
|
const options = formatParameter(title);
|
||||||
|
|
||||||
|
this.showZanToast({
|
||||||
|
...options,
|
||||||
|
icon: 'loading'
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function formatParameter(title, timeout = 0) {
|
||||||
|
// 如果传入的 title 是对象,那么认为所有的配置属性都在这个对象中了
|
||||||
|
if (typeof title === 'object') {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
title,
|
||||||
|
timeout
|
||||||
|
};
|
||||||
|
}
|
||||||
|
@ -1,17 +1,48 @@
|
|||||||
.zan-toast {
|
.zan-toast {
|
||||||
position: fixed;
|
position: fixed;
|
||||||
top: 35%;
|
top: 35%;
|
||||||
left: 20%;
|
left: 50%;
|
||||||
transform: translateZ(0) translateY(-100%);
|
transform: translate3d(-50%, -50%, 0);
|
||||||
background: rgba(0, 0, 0, 0.7);
|
background: rgba(0, 0, 0, 0.7);
|
||||||
color: #fff;
|
color: #fff;
|
||||||
font-size: 14px;
|
font-size: 14px;
|
||||||
width: 60%;
|
|
||||||
line-height: 1.5em;
|
line-height: 1.5em;
|
||||||
margin: 0 auto;
|
margin: 0 auto;
|
||||||
box-sizing: border-box;
|
box-sizing: border-box;
|
||||||
padding: 10px;
|
padding: 10px 18px;
|
||||||
text-align: center;
|
text-align: center;
|
||||||
border-radius: 4px;
|
border-radius: 4px;
|
||||||
z-index: 100;
|
z-index: 100;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 没有文字时,需要调整展示大小 */
|
||||||
|
.zan-toast--notitle {
|
||||||
|
padding: 18px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.zan-toast__icon {
|
||||||
|
width: 40px;
|
||||||
|
height: 40px;
|
||||||
|
line-height: 40px;
|
||||||
|
margin: 0 auto;
|
||||||
|
padding: 12px 15px;
|
||||||
|
font-size: 38px;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 加载中的状态 */
|
||||||
|
.zan-toast__icon-loading {
|
||||||
|
line-height: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.zan-toast__icon-loading .zan-loading {
|
||||||
|
width: 40px;
|
||||||
|
height: 40px;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 自定义图标 */
|
||||||
|
.zan-toast__icon-image {
|
||||||
|
background-size: 40px;
|
||||||
|
background-position: center;
|
||||||
|
background-repeat: no-repeat;
|
||||||
|
}
|
||||||
|
@ -1,3 +1,31 @@
|
|||||||
<template name="zan-toast">
|
<template name="zan-toast">
|
||||||
<view class="zan-toast" wx:if="{{ zanToast.show }}" bindtap="clearZanToast">{{ zanToast.title }}</view>
|
<view
|
||||||
|
class="zan-toast {{ !zanToast.title ? 'zan-toast--notitle' : '' }}"
|
||||||
|
wx:if="{{ zanToast.show }}"
|
||||||
|
bindtap="clearZanToast"
|
||||||
|
>
|
||||||
|
<!-- icon 展示 -->
|
||||||
|
<block
|
||||||
|
wx:if="{{ zanToast.icon || zanToast.image }}"
|
||||||
|
>
|
||||||
|
<view
|
||||||
|
wx:if="{{ zanToast.image }}"
|
||||||
|
class="zan-toast__icon zan-toast__icon-image"
|
||||||
|
style="background-image: url({{ zanToast.image }});"
|
||||||
|
></view>
|
||||||
|
<view
|
||||||
|
wx:elif="{{ zanToast.icon === 'loading' }}"
|
||||||
|
class="zan-toast__icon zan-toast__icon-loading"
|
||||||
|
>
|
||||||
|
<view class="zan-loading"></view>
|
||||||
|
</view>
|
||||||
|
<view
|
||||||
|
wx:else
|
||||||
|
class="zan-toast__icon zan-icon zan-icon-{{ zanToast.icon }}"
|
||||||
|
></view>
|
||||||
|
</block>
|
||||||
|
|
||||||
|
<!-- 文案展示 -->
|
||||||
|
<view wx:if="{{ zanToast.title }}">{{ zanToast.title }}</view>
|
||||||
|
</view>
|
||||||
</template>
|
</template>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user