const NotificationBar = {
name: 'notification-bar',
template: `
<div
:class="{
'notification-bar': true,
'notification-bar--error': isError,
'notification-bar--warning': isWarning,
'notification-bar--info': isInfo,
'notification-bar--visible': isVisible,
}"
@click="dismiss"
@transitionend="transitionEnd($event)">
{{ message }}
</div>
`,
props: {
message: {
type: String,
required: true,
},
type: {
type: String,
required: true,
validator(value) {
const valid = ['error', 'warning', 'info'];
return valid.includes(value);
},
},
dismissable: {
type: Boolean,
default: false,
},
timeout: {
type: Number,
default: 0,
},
},
data() {
return {
isVisible: false,
};
},
computed: {
isError() {
return this.type === 'error';
},
isWarning() {
return this.type === 'warning';
},
isInfo() {
return this.type === 'info';
},
},
methods: {
clear() {
const event = 'cleared';
let done;
if (this.isVisible) {
this.$once('transitionend', () => {
done = true;
this.$emit(event, done);
});
this.isVisible = false;
} else {
done = false;
this.$emit(event, done);
}
},
dismiss() {
const event = 'dismissed';
let done;
if (this.dismissable) {
done = true;
this.$emit(event, done);
this.clear();
} else {
done = false;
this.$emit(event, done);
}
},
show() {
if (!this.isVisible) {
this.isVisible = true;
this.$emit('show', this.clear);
if (this.timeout) {
setTimeout(() => {
this.$emit('timeout');
this.clear();
}, this.timeout);
}
}
},
transitionEnd(event) {
this.$emit('transitionend', event);
},
},
mounted() {
window.requestAnimationFrame(this.show);
},
};
const NotificationCenter = {
name: 'notification-center',
components: {
NotificationBar,
},
template: `
<div>
<notification-bar
v-for="notification in active"
:message="notification.message"
:type="notification.type"
:dismissable="notification.dismissable"
:timeout="notification.timeout"
@cleared="clear">
</notification-bar>
</div>
`,
props: {
queue: {
type: Array,
required: true,
},
},
data() {
return {
active: [],
};
},
computed: {
hasActiveNotification() {
return this.active.length > 0;
},
hasQueuedNotification() {
return this.queue.length > 0;
},
},
watch: {
queue() {
if (this.hasQueuedNotification && !this.hasActiveNotification) {
this.setNextActive();
}
},
},
methods: {
setNextActive() {
this.setActive(this.queue.shift());
},
setActive(notification) {
this.active.push(notification);
},
removeActive() {
this.active.pop();
},
clear() {
this.active.pop();
if (this.hasQueuedNotification) {
this.$nextTick(this.setNextActive);
}
},
},
};
window.vm = new Vue({
components: {
NotificationCenter,
},
el: '#app',
template: `
<div>
<notification-center
:queue="notifications">
</notification-center>
<label>
<strong>Type</strong> <br>
Error <input v-model="type" type="radio" name="type" value="error"> <br>
Warning <input v-model="type" type="radio" name="type" value="warning"> <br>
Info <input v-model="type" type="radio" name="type" value="info"> <br>
</label>
<label>
<strong>Message</strong>
<input v-model="message" type="text">
</label>
<label>
<strong>Dismissable</strong>
<input v-model="dismissable" type="checkbox">
</label>
<label>
<strong>Timeout</strong>
<input v-model="timeout" type="number" step="100" min="0">
</label>
<button @click="generateNotification">Generate notification</button>
</div>
`,
data: {
notifications: [],
type: null,
message: null,
dismissable: null,
timeout: null,
},
methods: {
generateNotification() {
const {
type,
message,
dismissable,
timeout,
} = this;
this.notifications.push({
type,
message,
dismissable,
timeout,
});
this.type = this.message = this.dismissable = this.timeout = null;
},
},
});
&#13;
.notification-bar {
box-sizing: border-box;
position: absolute;
top: -3.2rem;
right: 0;
left: 0;
z-index: 9999;
width: 100%;
height: 3.2rem;
color: #fff;
font-family: 'Avenir Next', sans-serif;
font-size: 1.2em;
line-height: 3.2rem;
text-align: center;
transition: top 266ms ease;
}
.notification-bar--error {
background-color: #f02a4d;
}
.notification-bar--warning {
background-color: #ffc107;
}
.notification-bar--info {
background-color: #2196f3;
}
.notification-bar--visible {
top: 0;
}
label {
display: block;
margin: 2rem 0;
font-size: 1.4rem;
}
label:first-of-type {
margin-top: 5rem;
}
&#13;
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
html {
font-size: 62.5%;
}
body {
font-family: sans-serif;
}
</style>
</head>
<body>
<div id="app"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.0.3/vue.js"></script>
</body>
</html>
&#13;
通知栏平滑地动画到可见视口中。您可以通过在Firefox中执行上述步骤来观察此事。
以下是GIF演示Chrome中的正确行为。点击生成通知后,您可以看到该栏平滑过渡。
以下是Chrome正常运行时间线的屏幕截图:
以下是Chrome正常运行时的调用树:
通知栏在大多数情况下无法平滑地进入可见视口。在Chrome Devtools中捕获时间轴显示在显示通知栏时没有动画正在运行。当条形图在屏幕上动画时,动画始终正确运行。动画始终在Firefox中正确运行。
以下是GIF演示Chrome中的错误行为。点击生成通知后,您会看到该栏突然出现。
以下是Chrome无法正常运行的时间轴的屏幕截图:
以下是Chrome行为不正确时的调用树:
正在执行的代码概述:
NotificationCenter
接受queue
道具。这是一个对象数组,其中数组表示通知队列,而对象表示单个通知。
queue
更改后,观察者会检查队列中是否有通知以及是否有活动通知。如果是这种情况,则将下一个通知设置为活动通知。
NotificationCenter
的模板有一个指令循环active
中的项目并呈现NotificationBar
。在上一步中,设置了新的活动通知,因此将创建一个新的通知栏并将其挂载到DOM。
在NotificationBar
上安装show
后,window.requestAnimationFrame
方法会在isExecuted
内运行。
答案 0 :(得分:0)
问题在于这一行:
this.type = this.message = this.dismissable = this.timeout = null;
如果删除它,它可以正常工作。当执行此操作时,props变为NULL,并且您已验证props不应为null。
const NotificationBar = {
name: 'notification-bar',
template: `
<div
:class="{
'notification-bar': true,
'notification-bar--error': isError,
'notification-bar--warning': isWarning,
'notification-bar--info': isInfo,
'notification-bar--visible': isVisible,
}"
@click="dismiss"
@transitionend="transitionEnd($event)">
{{ message }}
</div>
`,
props: {
message: {
type: String,
required: true,
},
type: {
type: String,
required: true,
validator(value) {
const valid = ['error', 'warning', 'info'];
return valid.includes(value);
},
},
dismissable: {
type: Boolean,
default: false,
},
timeout: {
type: Number,
default: 0,
},
},
data() {
return {
isVisible: false,
};
},
computed: {
isError() {
return this.type === 'error';
},
isWarning() {
return this.type === 'warning';
},
isInfo() {
return this.type === 'info';
},
},
methods: {
clear() {
const event = 'cleared';
let done;
if (this.isVisible) {
this.$once('transitionend', () => {
done = true;
this.$emit(event, done);
});
this.isVisible = false;
} else {
done = false;
this.$emit(event, done);
}
},
dismiss() {
const event = 'dismissed';
let done;
if (this.dismissable) {
done = true;
this.$emit(event, done);
this.clear();
} else {
done = false;
this.$emit(event, done);
}
},
show() {
if (!this.isVisible) {
this.isVisible = true;
this.$emit('show', this.clear);
if (this.timeout) {
setTimeout(() => {
this.$emit('timeout');
this.clear();
}, this.timeout);
}
}
},
transitionEnd(event) {
this.$emit('transitionend', event);
},
},
mounted() {
window.requestAnimationFrame(this.show);
},
};
const NotificationCenter = {
name: 'notification-center',
components: {
NotificationBar,
},
template: `
<div>
<notification-bar
v-for="notification in active"
:message="notification.message"
:type="notification.type"
:dismissable="notification.dismissable"
:timeout="notification.timeout"
@cleared="clear">
</notification-bar>
</div>
`,
props: {
queue: {
type: Array,
required: true,
},
},
data() {
return {
active: [],
};
},
computed: {
hasActiveNotification() {
return this.active.length > 0;
},
hasQueuedNotification() {
return this.queue.length > 0;
},
},
watch: {
queue() {
if (this.hasQueuedNotification && !this.hasActiveNotification) {
this.setNextActive();
}
},
},
methods: {
setNextActive() {
this.setActive(this.queue.shift());
},
setActive(notification) {
this.active.push(notification);
},
removeActive() {
this.active.pop();
},
clear() {
this.active.pop();
if (this.hasQueuedNotification) {
this.$nextTick(this.setNextActive);
}
},
},
};
window.vm = new Vue({
components: {
NotificationCenter,
},
el: '#app',
template: `
<div>
<notification-center
:queue="notifications">
</notification-center>
<label>
<strong>Type</strong> <br>
Error <input v-model="type" type="radio" name="type" value="error"> <br>
Warning <input v-model="type" type="radio" name="type" value="warning"> <br>
Info <input v-model="type" type="radio" name="type" value="info"> <br>
</label>
<label>
<strong>Message</strong>
<input v-model="message" type="text">
</label>
<label>
<strong>Dismissable</strong>
<input v-model="dismissable" type="checkbox">
</label>
<label>
<strong>Timeout</strong>
<input v-model="timeout" type="number" step="100" min="0">
</label>
<button @click="generateNotification">Generate notification</button>
</div>
`,
data: {
notifications: [],
type: null,
message: null,
dismissable: null,
timeout: null,
},
methods: {
generateNotification() {
const {
type,
message,
dismissable,
timeout,
} = this;
this.notifications.push({
type,
message,
dismissable,
timeout,
});
//this.type = this.message = this.dismissable = this.timeout = null;
},
},
});
.notification-bar {
box-sizing: border-box;
position: absolute;
top: -3.2rem;
right: 0;
left: 0;
z-index: 9999;
width: 100%;
height: 3.2rem;
color: #fff;
font-family: 'Avenir Next', sans-serif;
font-size: 1.2em;
line-height: 3.2rem;
text-align: center;
transition: top 266ms ease;
}
.notification-bar--error {
background-color: #f02a4d;
}
.notification-bar--warning {
background-color: #ffc107;
}
.notification-bar--info {
background-color: #2196f3;
}
.notification-bar--visible {
top: 0;
}
label {
display: block;
margin: 2rem 0;
font-size: 1.4rem;
}
label:first-of-type {
margin-top: 5rem;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
html {
font-size: 62.5%;
}
body {
font-family: sans-serif;
}
</style>
</head>
<body>
<div id="app"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.0.3/vue.js"></script>
</body>
</html>
你必须在setActive
函数中进行一些验证,就像你将空项目置于活动状态一样,你所做的一些验证都失败了。
setActive(notification) {
if(notification.message){
this.active.push(notification);
}
},
检查此fiddle。
答案 1 :(得分:0)
在与LinusBorg进行一些讨论后,作为Vue的撰稿人,Vue forum我们可能会遇到此问题:
[...]问题可能是Vue异步修补DOM,所以当调用
child_process
时,组件的元素存在,但不保证它们在DOM中。现在,根据不同的浏览器如何处理普通任务,微任务和动画框架的优先级,可能只是在Chrome中,当您通过{{更改类时,该元素尚未在DOM中1}}
在这种情况下,动画效果自然不会出现。
我建议尝试使用
mounted()
(这可以保证元素已经在DOM中),或者只使用Vue为此提供的工具,即show()
组件。
最初尝试使用this.$nextTick()
但在Firefox和Chrome中都失败了。
最终我能够使用<transition>
组件实现这一切。
this.$nextTick
<transition>
const NotificationBar = {
name: 'notification-bar',
template: `
<transition
name="visible"
mode="out-in"
@after-enter="show">
<div
:class="{
'notification-bar': true,
'notification-bar--error': isError,
'notification-bar--warning': isWarning,
'notification-bar--info': isInfo,
'notification-bar--visible': isVisible,
}"
:key="id"
@click="dismiss">
{{ message }}
</div>
</transition>
`,
props: {
message: {
type: String,
required: true,
},
type: {
type: String,
required: true,
validator(value) {
const valid = ['error', 'warning', 'info'];
return valid.includes(value);
},
},
id: {
type: [Number, String],
required: true,
},
dismissable: {
type: Boolean,
default: false,
},
timeout: {
type: Number,
default: 0,
},
},
data() {
return {
isVisible: false,
};
},
computed: {
isError() {
return this.type === 'error';
},
isWarning() {
return this.type === 'warning';
},
isInfo() {
return this.type === 'info';
},
},
methods: {
clear() {
const event = 'clear';
let done;
if (this.isVisible) {
done = true;
this.$emit(event, done);
this.isVisible = false;
} else {
done = false;
this.$emit(event, done);
}
},
dismiss() {
const event = 'dismissed';
let done;
if (this.dismissable) {
done = true;
this.$emit(event, done);
this.clear();
} else {
done = false;
this.$emit(event, done);
}
},
show() {
if (!this.isVisible) {
this.isVisible = true;
this.$emit('show', this.clear);
if (this.timeout) {
setTimeout(() => {
this.$emit('timeout');
this.clear();
}, this.timeout);
}
}
},
},
};
const NotificationCenter = {
name: 'notification-center',
template: `
<div>
<notification-bar
v-if="hasQueuedNotification"
:message="activeNotification.message"
:type="activeNotification.type"
:dismissable="activeNotification.dismissable"
:timeout="activeNotification.timeout"
:id="activeNotification.id"
@clear="clear">
</notification-bar>
</div>
`,
components: {
NotificationBar,
},
props: {
queue: {
type: Array,
required: true,
},
},
computed: {
hasQueuedNotification() {
return this.queue.length > 0;
},
activeNotification() {
return this.queue[0];
},
},
methods: {
clear() {
this.queue.shift();
},
},
};
window.vm = new Vue({
components: {
NotificationCenter,
},
el: '#app',
template: `
<div>
<notification-center
:queue="notifications">
</notification-center>
<label>
<strong>Type</strong> <br>
Error <input v-model="type" type="radio" name="type" value="error"> <br>
Warning <input v-model="type" type="radio" name="type" value="warning"> <br>
Info <input v-model="type" type="radio" name="type" value="info"> <br>
</label>
<label>
<strong>Message</strong>
<input v-model="message" type="text">
</label>
<label>
<strong>Dismissable</strong>
<input v-model="dismissable" type="checkbox">
</label>
<label>
<strong>Timeout</strong>
<input v-model="timeout" type="number" step="100" min="0">
</label>
<button @click="generateNotification">Generate notification</button>
</div>
`,
data: {
notifications: [],
type: null,
message: null,
dismissable: null,
timeout: null,
dismissIndex: null,
dismissMessage: null,
},
methods: {
generateNotification() {
const {
type,
message,
dismissable,
timeout,
} = this;
const id = Date.now();
this.notifications.push({
type,
message,
dismissable,
timeout,
id,
});
this.type = this.message = this.dismissable = this.timeout = null;
},
},
});