Mic*_*ael 8 javascript vue.js vuejs2
我正在尝试在html表行(vue.js)上进行转换(动画)但没有成功.这是完整的例子
new Vue({
el: '#data',
data: {
items: [
{
data: 'd1',
more: false
},
{
data: 'd2',
more: false
},
]
}
});Run Code Online (Sandbox Code Playgroud)
.fade-enter-active, .fade-leave-active {
transition: opacity 2s
}
.fade-enter, .fade-leave-to {
opacity: 0
}Run Code Online (Sandbox Code Playgroud)
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.min.js"></script>
<div class="container-fluid" id="data">
<br>
<br>
<table border="1" class="table table-bordered">
<thead class="thead-inverse">
<tr>
<th>anim</th>
</tr>
</thead>
<tbody>
<template v-for="item, k in items">
<tr>
<td><button @click="item.more = !item.more" type="button"
v-bind:class="[item.more ? 'btn-danger' : 'btn-primary']" class="btn">Show the hidden row</button></td>
</tr>
<transition name="fade" >
<tr v-bind:key="item" v-if="item.more">
<td><p >{{k + 1}} - {{item.data}}</p></td>
</tr>
</transition>
</template>
</tbody>
</table>
</div>Run Code Online (Sandbox Code Playgroud)
我期待隐藏的表行在出现时在opacity属性上显示转换/动画,但没有发生任何事情,我该怎么做?这完全适用于span或其他元素.
Ber*_*ert 12
所以,首先我要指出,如果您使用了字符串模板,那么您问题中的代码将按原样运行.
console.clear()
new Vue({
el: '#data',
template: `
<div>
<br>
<br>
<table border="1" class="table table-bordered">
<thead class="thead-inverse">
<tr>
<th>anim</th>
</tr>
</thead>
<tbody>
<template v-for="item, k in items">
<tr>
<td><button @click="item.more = !item.more" type="button"
v-bind:class="[item.more ? 'btn-danger' : 'btn-primary']" class="btn">Show the hidden row</button></td>
</tr>
<transition name="fade" >
<tr v-bind:key="item" v-if="item.more">
<td><p >{{k + 1}} - {{item.data}}</p></td>
</tr>
</transition>
</template>
</tbody>
</table>
</div>
`,
data: {
items: [
{
data: 'd1',
more: false
},
{
data: 'd2',
more: false
},
]
}
});Run Code Online (Sandbox Code Playgroud)
.fade-enter-active, .fade-leave-active {
transition: opacity 2s
}
.fade-enter, .fade-leave-to {
opacity: 0
}Run Code Online (Sandbox Code Playgroud)
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.min.js"></script>
<div class="container-fluid" id="data">
</div>Run Code Online (Sandbox Code Playgroud)
请注意,此示例中唯一的更改是我将Vue的模板转换为字符串,而不是在DOM中定义模板.这个单一更改的原因是因为当您使用"在DOM"模板中时,您需要在 Vue将模板转换为渲染函数之前解析HTML .当您使用table元素时,浏览器非常挑剔它们允许在表格内呈现的元素类型; 通常,只与表(元素thead,tbody,tr,td,等等).transition它不是一个与它相处的元素(虽然,有点令人惊讶,它并没有窒息template).
但是,字符串模板永远不会被浏览器解析,它们会直接转换为渲染函数,因此它们将在您编写它们时起作用.所以我的第一个建议是,只需使用一个字符串模板.
但是,如果您想继续使用in DOM模板,我们需要对代码进行一些更改.首先,我们需要将过渡转移到浏览器会满意的地方.使用表格,我们可以通过将其移动到tbody标签并使用Vue的特殊is指令轻松完成.其次,因为我们的转换现在将应用于多个元素,我们需要将其切换为transition-group.
因为我们使用transition-group过渡中的每个元素都必须有一个键.因此,对于每一行,我们只需为该行添加一个键.
console.clear()
new Vue({
el: '#data',
data: {
items: [{
data: 'd1',
more: false
},
{
data: 'd2',
more: false
},
]
}
});Run Code Online (Sandbox Code Playgroud)
.fade-enter-active,
.fade-leave-active {
transition: opacity 2s
}
.fade-enter,
.fade-leave-to {
opacity: 0
}Run Code Online (Sandbox Code Playgroud)
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.js"></script>
<div class="container-fluid" id="data">
<br>
<br>
<table border="1" class="table table-bordered">
<thead class="thead-inverse">
<tr>
<th>anim</th>
</tr>
</thead>
<tbody name="fade" is="transition-group">
<template v-for="item, k in items">
<tr v-bind:key="`button-${item.data}`">
<td>
<button @click="item.more = !item.more"
type="button"
v-bind:class="[item.more ? 'btn-danger' : 'btn-primary']"
class="btn">Show the hidden row
</button>
</td>
</tr>
<tr v-bind:key="`detail-${item.data}`" v-if="item.more">
<td><p >{{k + 1}} - {{item.data}}</p></td>
</tr>
</template>
</tbody>
</table>
</div>Run Code Online (Sandbox Code Playgroud)