transition when Route change #4051
Unanswered
shellRaining
asked this question in
Q&A
Replies: 2 comments 4 replies
-
Maybe you can use it like this 🤔 import { ref, watch, onMounted, onUnmounted, nextTick } from "vue";
import { useRoute } from "vitepress";
export function useBrowserNativeTransitions() {
const route = useRoute();
const currentPathname = ref(route.path);
const currentViewTransition = ref<null | [Promise<void>, () => void]>(null);
const startViewTransition = () => {
if (!("startViewTransition" in document)) {
return Promise.resolve();
}
let pendingViewTransitionResolve: () => void;
const pendingViewTransition = new Promise<void>((resolve) => {
pendingViewTransitionResolve = resolve;
});
const pendingStartViewTransition = new Promise<void>((resolve) => {
// @ts-ignore
document.startViewTransition(() => {
resolve();
return pendingViewTransition;
});
});
currentViewTransition.value = [
pendingStartViewTransition,
pendingViewTransitionResolve!,
];
return pendingStartViewTransition;
};
const onPopState = () => {
startViewTransition();
};
onMounted(() => {
if (!("startViewTransition" in document)) {
return;
}
window.addEventListener("popstate", onPopState);
});
onUnmounted(() => {
if (!("startViewTransition" in document)) {
return;
}
window.removeEventListener("popstate", onPopState);
});
watch(
() => route.path,
async (newPath, oldPath) => {
if (currentViewTransition.value && currentPathname.value !== newPath) {
await currentViewTransition.value[0];
}
await nextTick();
currentPathname.value = newPath;
if (currentViewTransition.value) {
currentViewTransition.value[1]();
currentViewTransition.value = null;
}
}
);
} usage: // in layout component...
<script>
useBrowserNativeTransitions()
</script> |
Beta Was this translation helpful? Give feedback.
2 replies
-
今天闲逛vitepress,发现了这个问题。我这边看链接没失效,之前得到的回答是这样的。 |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
#2508 (comment)
This discussion has mentioned, but the Discord link within it has expired. Could you please provide more information?
My current solution is as follows
However, the transition effect only occurs after clicking the link. If I use the back command, there is no transition effect. How can I monitor events like history.back() and provide a transition effect?
transition.mp4
Beta Was this translation helpful? Give feedback.
All reactions