主题
#[Transition]
#[Transition] 属性为操作方法配置视图过渡行为,允许你设置过渡类型或完全跳过过渡。
基本用法
将 #[Transition] 属性应用于应触发特定过渡动画的操作方法:
php
<?php
use Livewire\Attributes\Transition;
use Livewire\Component;
class Wizard extends Component
{
public $step = 1;
#[Transition(type: 'forward')] // [tl! highlight]
public function next()
{
$this->step++;
}
#[Transition(type: 'backward')] // [tl! highlight]
public function previous()
{
$this->step--;
}
}blade
<div>
<div wire:transition="content">
Step {{ $step }}
</div>
<button wire:click="previous">Back</button>
<button wire:click="next">Next</button>
</div>过渡类型可以在 CSS 中使用 :active-view-transition-type() 选择器进行定向:
css
html:active-view-transition-type(forward) {
&::view-transition-old(content) {
animation: 300ms ease-out slide-out-left;
}
&::view-transition-new(content) {
animation: 300ms ease-in slide-in-right;
}
}
html:active-view-transition-type(backward) {
&::view-transition-old(content) {
animation: 300ms ease-out slide-out-right;
}
&::view-transition-new(content) {
animation: 300ms ease-in slide-in-left;
}
}
@keyframes slide-out-left {
from { transform: translateX(0); opacity: 1; }
to { transform: translateX(-100%); opacity: 0; }
}
@keyframes slide-in-right {
from { transform: translateX(100%); opacity: 0; }
to { transform: translateX(0); opacity: 1; }
}
@keyframes slide-out-right {
from { transform: translateX(0); opacity: 1; }
to { transform: translateX(100%); opacity: 0; }
}
@keyframes slide-in-left {
from { transform: translateX(-100%); opacity: 0; }
to { transform: translateX(0); opacity: 1; }
}跳过过渡
使用 skip: true 为特定操作禁用过渡:
php
#[Transition(skip: true)]
public function reset()
{
$this->step = 1;
}这对于应该立即更新而没有动画的 "重置" 或 "取消" 等操作很有用。
参数
| 参数 | 类型 | 描述 |
|---|---|---|
type | string | 过渡类型名称(例如 'forward'、'backward') |
skip | bool | 设置为 true 以禁用此操作的过渡 |
替代方法
使用 transition()
对于依赖于运行时逻辑的动态过渡类型,请改用 transition() 方法:
php
public function goToStep($step)
{
$this->transition(type: $step > $this->step ? 'forward' : 'backward');
$this->step = $step;
}使用 skipTransition()
你也可以命令式地跳过过渡:
php
public function reset()
{
$this->skipTransition();
$this->step = 1;
}了解更多
有关视图过渡的更多信息,请参阅 wire:transition 文档。