【发布时间】:2016-01-16 05:13:49
【问题描述】:
我正在使用 vue.js 在我的网页中创建一种通知系统 一切正常,但我想在转换完成后删除元素。 我只让它与 setTimeout 一起工作,但这不是理想的方法
工作jsfiddle: http://jsfiddle.net/yMv7y/1073/
这是我的代码:
Vue:
Vue.transition('notification', {
enter: function(el) {
app.notifications.pop();
},
leave: function(el) {
setTimeout(function(){
el.remove();
},5000);
},
});
Vue.component('notification', {
template: '<div class="notification" v-class="red: !success, green: success" v-transition="notification"><p>{{message}}</p></div>',
data: function() {
return {
success: true,
message: '',
};
},
});
var app = new Vue({
el: 'body',
data: {
notifications : [
]
},
ready: function() {
self = this;
var data = {
'message': 'Thank you',
'success': true
};
self.notifications.push(data);
},
});
HTML:
<div id="notifications-wrapper">
<notification id="notification"
v-repeat="notifications"
>
</notification>
</div>
CSS #notifications 包装器 { 位置:固定; z指数:99; 顶部:0; 左:80%;
overflow: visible;
}
.notification
{
position: relative;
z-index: 100;
overflow: hidden;
width: 250px;
margin-top: 20px;
transform: translate(20px, 0);
animation-fill-mode: forwards;
transition: 1s;
-webkit-backdrop-filter: blur(2px);
backdrop-filter: blur(2px);
background-color: grey;
}
p
{
margin: 10px 20px;
color: $white;
}
.notification-transition
{
animation-delay: 0s, 4.5s;
animation-duration: 4.5s, 0.5s;
animation-name: slide-in-out, hide-notification;
}
@keyframes slide-in-out
{
0%
{
transform: translate(20px, 0);
}
10%
{
transform: translate(-270px, 0);
}
90%
{
transform: translate(-270px, 0);
opacity: 1;
}
100%
{
transform: translate(20px, 0);
opacity: .5;
}
}
@keyframes hide-notification {
1% {
height: auto;
margin-top: 20px;
}
100% {
height: 0;
margin: 0;
}
}
【问题讨论】:
-
要删除
#notifications-wrapper吗? -
不仅是通知本身,它将与 v-repeat 一起显示。我认为从数组中删除它就足够了。
-
您要删除
el对象吗? -
是的,这行得通。但这不是最好的为什么用 setTimeout 来做。动画完成时。
标签: javascript css removechild vue.js