We often use x-show
to control the element to show or hide. With x-transition
we can apply the visual effect for x-show
.
Version
Alpine 3.9
x-transition
Toggle Hello World
by CSS transition with duration.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://unpkg.com/alpinejs" defer></script>
<title>Alpine</title>
</head>
<body x-data="{ isShow: true }">
<button @click="isShow = !isShow">Toggle</button>
<div x-show="isShow" x-transition.duration.500ms>Hello World</div>
</body>
</html>
Line 11
<body x-data="{ isShow: true }">
<button @click="isShow = !isShow">Toggle</button>
<div x-show="isShow" x-transition.duration.500ms>Hello World</div>
</body>
x-data
: toggleisShow
state to showHello World
x-show
: show or hide byisShow
statex-transition.duration.500ms
: CSS transition by 500ms duration
Conclusion
- We don’t have to use any CSS or TailwindCSS utilities for CSS transition with duration. Just use
x-transition.duration
with a specified duration time