How can I detect if user moved to previous page or page after?

So I want to make a site that has transition effects whenever entering the page.
This works when the page is initially loaded or reloaded. However, if I try to go back to the previous page or go forward, it doesn’t work.
Can anyone tell me how it can be done in JS?

This is the example site I want to implement.
:https://www.ethanchng.com/