-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforward.html
48 lines (45 loc) · 2.24 KB
/
forward.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://cdn.jsdelivr.net/npm/@hotwired/turbo@8.0.12/dist/turbo.es2017-umd.min.js"></script>
<style>
::view-transition-group(root) {
animation-duration: 1s;
}
</style>
<script>
// 1. Register for swipe navigation events, annotate `html`
window.addEventListener('touchend', () => document.documentElement.setAttribute('data-jch-navigate-touch', true));
window.addEventListener('touchcancel', () => document.documentElement.setAttribute('data-jch-navigate-touch', true));
window.addEventListener('wheel', () => document.documentElement.setAttribute('data-jch-navigate-touch', true)); // Mac two finger swipe
document.addEventListener('turbo:visit', (event) => {
document.documentElement.setAttribute('data-turbo-visit-action', event.detail.action);
});
// 2. `turbo:before-cache` removes <meta name=‘view-transition’ … /> on the current page to disable view transition
// Comment out to see the double animation
document.addEventListener('xturbo:before-cache', (event) => {
const action = document.documentElement.getAttribute('data-turbo-visit-action');
const touch = document.documentElement.getAttribute('data-jch-navigate-touch');
if (action === 'restore' && touch) {
console.log('turbo:before-cache removing view-transition meta tag');
document.documentElement.querySelector("meta[name='view-transition']").remove();
}
});
// 3. `turbo:load` clean up annotations
document.addEventListener('turbo:load', () => {
document.documentElement.removeAttribute('data-turbo-visit-action');
document.documentElement.removeAttribute('data-jch-navigate-touch');
});
</script>
</head>
<meta name="view-transition" content="same-origin">
<body>
<h2>Example</h2>
<p><a href="javascript:history.back()">Click this link to go back</a></p>
<p>Or swipe with two finger on Mac Safari</p>
<p>Or swipe with iOS</p>
<p>Or other browsers with a default back/forward animation</p>
<h2>To see the double animation</h2>
<p>View source, and disable the added event listener for `turbo:before-cache` in `head`.</p>
</body>
</html>