[Improvement] Popup: support leave animation (#498)

This commit is contained in:
neverland 2018-09-05 14:33:18 +08:00 committed by GitHub
parent c71a198fb2
commit 57f8e54b19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 177 additions and 110 deletions

View File

@ -0,0 +1,52 @@
module.exports = Behavior({
properties: {
customStyle: String,
show: {
value: true,
type: Boolean,
observer(value) {
if (value) {
this.show();
} else {
this.setData({
type: 'leave'
});
}
}
},
duration: {
type: Number,
value: 300
}
},
data: {
type: '',
inited: false,
display: false
},
attached() {
if (this.data.show) {
this.show();
}
},
methods: {
show() {
this.setData({
inited: true,
display: true,
type: 'enter'
});
},
onAnimationEnd() {
if (!this.data.show) {
this.setData({
display: false
});
}
}
}
});

View File

@ -49,8 +49,9 @@ Page({
| show | 当前组件是否显示 | `Boolean` | `false` | | show | 当前组件是否显示 | `Boolean` | `false` |
| overlay | 是否显示背景蒙层 | `Boolean` | `true` | | overlay | 是否显示背景蒙层 | `Boolean` | `true` |
| position | 可选值为 `top` `bottom` `right` `left` | `String` | - | | position | 可选值为 `top` `bottom` `right` `left` | `String` | - |
| overlay-style | 自定义蒙层样式 | `Object` | `` | | overlay-style | 自定义蒙层样式 | `String` | `` |
| close-on-click-overlay | 点击蒙层是否关闭 Popup | `Boolean` | `true` | | close-on-click-overlay | 点击蒙层是否关闭 Popup | `Boolean` | `true` |
| duration | 动画时长,单位为毫秒 | `Number` | `300` |
### Event ### Event
@ -64,4 +65,3 @@ Page({
| 类名 | 说明 | | 类名 | 说明 |
|-----------|-----------| |-----------|-----------|
| custom-class | 根节点样式类 | | custom-class | 根节点样式类 |
| overlay-class | 蒙层样式类 |

View File

@ -1,3 +1,5 @@
const transitionBehaviors = require('../behaviors/transition');
Component({ Component({
options: { options: {
addGlobalClass: true addGlobalClass: true
@ -8,9 +10,23 @@ Component({
'overlay-class' 'overlay-class'
], ],
behaviors: [transitionBehaviors],
properties: { properties: {
show: Boolean,
overlayStyle: String, overlayStyle: String,
show: {
value: false,
type: Boolean,
observer(value) {
if (value) {
this.show();
} else {
this.setData({
type: 'leave'
});
}
}
},
overlay: { overlay: {
type: Boolean, type: Boolean,
value: true value: true

View File

@ -1,3 +1,6 @@
{ {
"component": true "component": true,
"usingComponents": {
"van-overlay": "../overlay/index"
}
} }

View File

@ -4,27 +4,16 @@
top: 50%; top: 50%;
left: 50%; left: 50%;
z-index: 11; z-index: 11;
position: fixed;
max-height: 100%; max-height: 100%;
overflow-y: auto; overflow-y: auto;
box-sizing: border-box; box-sizing: border-box;
background-color: $white; background-color: $white;
-webkit-overflow-scrolling: touch; -webkit-overflow-scrolling: touch;
transform: translate3d(-50%, -50%, 0); animation: ease both;
&, &--center {
&__overlay { transform: translate3d(-50%, -50%, 0);
display: none;
position: fixed;
animation: van-fade-in .3s ease;
}
&__overlay {
width: 100%;
height: 100%;
top: 0;
left: 0;
z-index: 10;
background-color: rgba(0, 0, 0, 0.7);
} }
&--top { &--top {
@ -33,8 +22,6 @@
right: auto; right: auto;
bottom: auto; bottom: auto;
left: 50%; left: 50%;
transform: translate3d(-50%, 0, 0);
animation-name: van-popup-top;
} }
&--right { &--right {
@ -42,8 +29,6 @@
right: 0; right: 0;
bottom: auto; bottom: auto;
left: auto; left: auto;
transform: translate3d(0, -50%, 0);
animation-name: van-popup-right;
} }
&--bottom { &--bottom {
@ -52,8 +37,6 @@
bottom: 0; bottom: 0;
right: auto; right: auto;
left: 50%; left: 50%;
transform: translate3d(-50%, 0, 0);
animation-name: van-popup-bottom;
} }
&--left { &--left {
@ -61,40 +44,10 @@
right: auto; right: auto;
bottom: auto; bottom: auto;
left: 0; left: 0;
transform: translate3d(0, -50%, 0);
animation-name: van-popup-left;
}
&--show {
display: block;
} }
} }
@keyframes van-popup-top { @keyframes van-center-enter {
from {
transform: translate3d(-50%, -100%, 0);
}
}
@keyframes van-popup-bottom {
from {
transform: translate3d(-50%, 100%, 0);
}
}
@keyframes van-popup-left {
from {
transform: translate3d(-100%, -50%, 0);
}
}
@keyframes van-popup-right {
from {
transform: translate3d(100%, -50%, 0);
}
}
@keyframes van-fade-in {
from { from {
opacity: 0; opacity: 0;
} }
@ -103,3 +56,85 @@
opacity: 1; opacity: 1;
} }
} }
@keyframes van-center-leave {
from {
opacity: 1;
}
to {
opacity: 0;
}
}
@keyframes van-bottom-enter {
from {
transform: translate3d(-50%, 100%, 0);
}
to {
transform: translate3d(-50%, 0, 0);
}
}
@keyframes van-bottom-leave {
from {
transform: translate3d(-50%, 0, 0);
}
to {
transform: translate3d(-50%, 100%, 0);
}
}
@keyframes van-top-enter {
from {
transform: translate3d(-50%, -100%, 0);
}
to {
transform: translate3d(-50%, 0, 0);
}
}
@keyframes van-top-leave {
from {
transform: translate3d(-50%, 0, 0);
}
to {
transform: translate3d(-50%, -100%, 0);
}
}
@keyframes van-left-enter {
from {
transform: translate3d(-100%, -50%, 0);
}
to {
transform: translate3d(0, -50%, 0);
}
}
@keyframes van-left-leave {
from {
transform: translate3d(0, -50%, 0);
}
to {
transform: translate3d(-100%, -50%, 0);
}
}
@keyframes van-right-enter {
from {
transform: translate3d(100%, -50%, 0);
}
to {
transform: translate3d(0, -50%, 0);
}
}
@keyframes van-right-leave {
from {
transform: translate3d(0, -50%, 0);
}
to {
transform: translate3d(100%, -50%, 0);
}
}

View File

@ -1,9 +1,14 @@
<view <van-overlay
wx:if="{{ overlay }}" mask
class="overlay-class van-popup__overlay {{ show ? 'van-popup--show' : '' }}" show="{{ overlay && show }}"
style="{{ overlayStyle }}" custom-style="{{ overlayStyle }}"
bind:tap="onClickOverlay" bind:click="onClickOverlay"
/> />
<view class="custom-class van-popup {{ position ? 'van-popup--' + position : '' }} {{ show ? 'van-popup--show' : '' }}"> <view
wx:if="{{ inited }}"
class="custom-class van-popup {{ position ? 'van-popup--' + position : '' }}"
style="animation-name: van-{{ position }}-{{ type }}; animation-duration: {{ duration }}ms; {{ display ? '' : 'display: none;' }}"
bind:animationend="onAnimationEnd"
>
<slot /> <slot />
</view> </view>

View File

@ -1,3 +1,5 @@
const transitionBehaviors = require('../behaviors/transition');
Component({ Component({
options: { options: {
addGlobalClass: true addGlobalClass: true
@ -5,58 +7,12 @@ Component({
externalClasses: ['custom-class'], externalClasses: ['custom-class'],
behaviors: [transitionBehaviors],
properties: { properties: {
customStyle: String,
show: {
value: true,
type: Boolean,
observer(value) {
if (value) {
this.show();
} else {
this.setData({
type: 'leave'
});
}
}
},
name: { name: {
type: String, type: String,
value: 'fade' value: 'fade'
},
duration: {
type: Number,
value: 300
}
},
data: {
type: '',
inited: false,
display: false
},
attached() {
if (this.data.show) {
this.show();
}
},
methods: {
show() {
this.setData({
inited: true,
display: true,
type: 'enter'
});
},
onAnimationEnd() {
if (!this.data.show) {
this.setData({
display: false
});
}
} }
} }
}); });