Giter VIP home page Giter VIP logo

Comments (4)

matthew-whitfield-uk avatar matthew-whitfield-uk commented on May 12, 2024 2

I also experience the flickering issue with scroll position restoration - I think it is due to the restoration logic being wrapped in a settimeout (this commit).

When starting from a non-zero vertical scroll position and navigating forward I often see one frame of the new page at the current scroll position before it sets the scroll position to 0.

On backwards navigation, I often see one frame at the current scroll position before the restoration logic kicks in and restores the previous scroll position.

I think the settimeout was added to allow Angular to figure out the size of page before starting the scroll position logic. I understand why this is necessary when going backwards to a non-zero scroll position (full DOM height needs to be set before scrolling) but on a forward navigation going to a 0 scroll position I would have thought the setttimeout was unnecessary.

Also it seems other SPAs such as Github and Youtube are able to do the scroll position restoration synchronously and have no flickering issues even on backwards navigation.

from angular.

kim-hanho avatar kim-hanho commented on May 12, 2024 1

The workaround I did for the UX is, make the body opacity zero while the angular is calculating the scroll position(very very short time).
Caution: It doesn't solve the root cause of the problem, it's just deceiving the user's eye. And not good for when using auxiliary routes.

this.router.events
      .subscribe((event: any) => {
            if (event instanceof NavigationEnd || event instanceof NavigationCancel || event instanceof NavigationError) {
                    document.body.style.opacity = '0';
                    setTimeout(()=>{
                          document.body.style.opacity = '';
                    },1)
            }
      })

from angular.

surajchopra1234 avatar surajchopra1234 commented on May 12, 2024
import { ViewportScroller, Location } from '@angular/common';
import { Injectable } from '@angular/core';
import { NavigationStart, Router } from '@angular/router';
import { filter } from 'rxjs/operators';

@Injectable({
    providedIn: 'root'
})
export class RouterScrollService {
    // Scroll positions
    private scrollPositions = new Map();

    //
    // Constructor

    constructor(
        private router: Router,
        private viewportScroller: ViewportScroller,
        private location: Location
    ) {
        // Subscribe to router events

        this.router.events
            .pipe(filter((event) => event instanceof NavigationStart))
            .subscribe((event: any) => {
                if (event.navigationTrigger === 'imperative') {
                    // Get current scroll position
                    const currentScrollPosition = this.viewportScroller.getScrollPosition();

                    // Get current url
                    const currentUrl = this.location.path();

                    // Scroll to the top
                    this.viewportScroller.scrollToPosition([0, 0]);

                    // Get current navigation object
                    const navObject = this.router.getCurrentNavigation();

                    // If the navigation is done with (NavigationExtras) replaceUrl: true
                    if (navObject?.extras.replaceUrl) {
                        this.scrollPositions.set(event.url, currentScrollPosition);

                        this.scrollPositions.delete(currentUrl);
                    }

                    // If the navigation is normal
                    else {
                        // Save the url and scroll position
                        this.scrollPositions.set(currentUrl, currentScrollPosition);
                    }
                } else if (event.navigationTrigger === 'popstate') {
                    // If scroll position is saved

                    if (this.scrollPositions.has(event.url)) {
                        // Scroll to the saved scroll position
                        this.viewportScroller.scrollToPosition(this.scrollPositions.get(event.url));
                    } else {
                        // Scroll to the top
                        this.viewportScroller.scrollToPosition([0, 0]);
                    }
                }
            });
    }
}

I've implemented a temporary solution that involves scrolling to the top during imperative route transitions and restoring the saved scroll position during popstate back transitions.

This is very temporary solution.

Flickering introduces by scrollPositionRestoration: 'enabled' is significantly impacting user experience. It's imperative that we address this issue as soon as possible to ensure optimal user satisfaction

from angular.

surajchopra1234 avatar surajchopra1234 commented on May 12, 2024

@atscott

Is there any progress about this issue? I appreciate you working on it.

from angular.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.