Replies: 5 comments 7 replies
-
I didn't check but you can probably use a generic listener on document and a listener for the load event on window. <div x-show="loader" x-on:click.document="if($event.target & && $event.target.name == 'A') loader = true" x-on:load.window="loader = false" x-transition.opacity.duration.1000ms class="w-screen h-screen absolute left-0 top-0 z-50 bg-lead">...</div> |
Beta Was this translation helpful? Give feedback.
-
thanks for the response, I like the Ok so if I do |
Beta Was this translation helpful? Give feedback.
-
Is there a way to check if the page load stops? The problem I'm now running into is if I click the browser stop button during page load the load screen animation stays stuck on the page. I came across onabort but not sure how I would use it or if its exactly what I need, |
Beta Was this translation helpful? Give feedback.
-
Hi there, thanks for exploring this topic ! It really helped. I was looking for a similar effect, and here is my implementation, where :
Note that i use Statamic, hence the double staches, and Tailwind. <body
x-data="{
{{# The content is loading #}}
loading: true,
{{# Method to determine whether a clicked element has a parent A node #}}
isLinkToPage () {
let element = this.$event.target;
{{# Recursively look for a parent until A or no parent #}}
while (element.parentNode) {
element = element.parentNode;
{{# console.log(element); #}}
if (element.tagName === 'A') {
const href = element.getAttribute('href');
const protocol = href && href.startsWith('http') ? 'http' : href.startsWith('https') ? 'https' : null;
const hostname = href && href.match(/^https?:\/\/[^\/]+/i);
const isInternalLink = protocol === null || hostname === window.location.hostname;
const isNotAnchorLink = !href.includes('#');
{{# If it is an internal link but not an anchor link, validate the test #}}
if ( isInternalLink && isNotAnchorLink ) return true;
return null;
}
}
return null;
}
}"
x-on:load.window="loading = false; {{# The window has finished loading #}}"
x-on:click.document="if($event.target && isLinkToPage() ) loading = true; {{# Set loading mode when clicking on a link #}}"
class="">
{{# Loader #}}
<div class="fixed top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 transition-opacity"
x-bind:class="loading ? 'opacity-100' : 'opacity-0' {{# Show the loader when loading #}}" >
<svg>Some spinning icon</svg>
</div>
{{# Content #}}
<div x-bind:class="!loading ? 'opacity-100' : '' {{# Show the content when finished loading #}}"
class="opacity-0 transition-opacity duration-300">
{{ template_content }}
</div>
</body> Hope that helps further the conversation! Cheers, and thanks to various sources for helping me coming up with this code. |
Beta Was this translation helpful? Give feedback.
-
You should just use the ViewTransitions API to handle this. |
Beta Was this translation helpful? Give feedback.
-
I made an animated transition during page loads on my site, so when you click on a link a splash screen fades in with a loader icon and then when the next page loads the splash screen fades out.
I accomplished this like this...
I have an absolutely positioned div with full screen width and height for the splash screen that is hidden by default with x-show:
On my page links I add
x-on:click="loader = true"
Something like:
So when I click the link the splash screen fades in immediately before the other page has time to load.
Then I have another div with the same css as the loader splash that fades out on x-init
So when the following page is loaded it appears to be the same splash screen that finally fades out.
This works, but it seems a bit convoluted, first off it means I have to add
x-on:click="loader = true"
to every link.(maybe not bad if I want control over which links use the transition)
Also the second screen doesn't fade out once the new page is done loading, it fades out immediately. So instead of x-init is there something like "on finished loading" ?
Anyway I feel like this sort of page transition isn't necessarily unusual so I'm curious to see how else this could be approached with alpine.
Beta Was this translation helpful? Give feedback.
All reactions