question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Animated page transitions

See original GitHub issue

Are you requesting a feature, reporting a bug or asking a question?

Feature

I’m working on animating page transitions but am running into some issues that make the solution quite hacky. test

What is the current behavior?

The event to detect a page change even is onCurrentPageChanging. After that returns the element is immediately removed from the DOM, meaning any animations won’t show.

What I do now when the event triggers is:

  • Add a CSS class to trigger the animation.
  • Prevent the page change (options.allowChange)
  • Call survey.nextPage() after a delay
  • Allow the page to change if the container already has the css class.

For the entrance transition, I store the direction we’re moving in (important to make sure a slide-in occurs from the correct window side) and use that to add a CSS class in the onAfterRenderPage event.

What is the expected behavior?

Ideally the library would support animations a bit better by allowing us to control when elements get removed from the DOM.

Create a callback that can be used to hide the page container for example like this:

survey.hidePage = (htmlElement, cb) => {
// This code should hide the page, when it is done it should call `cb()` to notify SurveyJS that it may remove the element from the DOM.

// No animation:
cb();

// CSS Animation:
htmlElement.classList.add('fade-out-cool');
setTimeout(cb, 1000);

};

Fade-ins are inherently easier because we already have an event for elements that are added to the DOM.

Added benefit of my sample above is that it allows for some asynchronous behavior that can make the library appear smoother.

  1. Before removing the page we start the CSS animation.
  2. While the animation runs library code can already do the calculations required and therefore immediately load the next page when cb() is called.

While my example uses callbacks, ofcourse anno ~ 2021 it probably makes more sense to use promises. For example, hidePage could return a promise and when it resolves you remove the element:

survey.hidePage = (element) => {
    element.classList.add('fade-out-cool');
    return new Promise((resolve, reject) => {
        setTimeout(resolve, 1000);
    });
};

// Imaginary code:
... 
    async nextPageInternal() => {
        if (!currentPage.validate()) {
            return currentPage.highlightErrors();
        }
        // This is the existing event
        trigger(beforeChanging);
        if (!changeAllowed) { 
            // Exit early
            return;
        }
        let element = getDOMElementForPage(currentPage);
        let hidePage = survey.hidePage(element);
        let nextPage = calculateNextPage();
        let renderedElement = renderPage(nextPage);

        await hidePage;
        // Try to do heavy lifting before awaiting the promise
        activateAndInsertIntoDOM(nextPage, renderedElement);


    }

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
bakdakonusuruzcommented, May 26, 2021

Thx for your comment @SamMousa. I’m doing the same thing, even with the same library ( good old animate.css 🙂 ) But nextPage() triggers couple of times. I’m using React, maybe that is effecting. That means I’m missing something. Okay, I’ll look into it, thx. 👍

1reaction
SamMousacommented, Apr 13, 2021

Bump?

Read more comments on GitHub >

github_iconTop Results From Across the Web

24 CSS Page Transitions - Free Frontend
Collection of hand-picked free HTML and CSS page transition effect code examples from Codepen, GitHub and other resources.
Read more >
19+ Amazing CSS Page Transitions ( With Beautiful Examples! )
Here's a collection with some great CSS Page Transitions and animations to use in your websites. We added CodePen examples so you can...
Read more >
A Collection of Page Transitions - Codrops
A Collection of Page Transitions with CSS Animations. ... Show next page transition. Choose a transition. Move. back; Move to left/ from right ......
Read more >
10 CSS & JavaScript Snippets for Creating Page Transition ...
10 CSS & JavaScript Snippets for Creating Page Transition Effects · Vue. · Old School Television by Mergim Ujkani · Morphing SVG by...
Read more >
10 Bootstrap Page Transitions - free examples & tutorial
A stunning collection of page transitions built with Bootstrap 5. Templates for transitions on scroll, slide transitions, animations on click, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found