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.

Add Scroll to top before transition start

See original GitHub issue

Js newbie here, so excuse moi the dumb question ;D

Trying to add scrollTop before the transition start in the fade example below (got it on http://barbajs.org/transition.html). Right now this code works but the new page opens on random content.

var FadeTransition = Barba.BaseTransition.extend({
  start: function() {
    /**
     * This function is automatically called as soon the Transition starts
     * this.newContainerLoading is a Promise for the loading of the new container
     * (Barba.js also comes with an handy Promise polyfill!)
     */

    // As soon the loading is finished and the old page is faded out, let's fade the new page
    Promise
      .all([this.newContainerLoading, this.fadeOut()])
      .then(this.fadeIn.bind(this));
  },

  fadeOut: function() {
    /**
     * this.oldContainer is the HTMLElement of the old Container
     */

    return $(this.oldContainer).animate({ opacity: 0 }).promise();
  },

  fadeIn: function() {
    /**
     * this.newContainer is the HTMLElement of the new Container
     * At this stage newContainer is on the DOM (inside our #barba-container and with visibility: hidden)
     * Please note, newContainer is available just after newContainerLoading is resolved!
     */

    var _this = this;
    var $el = $(this.newContainer);

    $(this.oldContainer).hide();

    $el.css({
      visibility : 'visible',
      opacity : 0
    });

    $el.animate({ opacity: 1 }, 400, function() {
      /**
       * Do not forget to call .done() as soon your transition is finished!
       * .done() will automatically remove from the DOM the old Container
       */

      _this.done();
    });
  }
});

/**
 * Next step, you have to tell Barba to use the new Transition
 */

Barba.Pjax.getTransition = function() {
  /**
   * Here you can use your own logic!
   * For example you can use different Transition based on the current page or link...
   */

  return FadeTransition;
};

any idea??

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5

github_iconTop GitHub Comments

4reactions
factorzerocommented, Jul 4, 2017

I also recommend adding this too:

if ('scrollRestoration' in history) {
    history.scrollRestoration = 'manual';
  }

What this does is it prevent Chrome from jumping to the last scroll position when pressing the back button. Without it, even when scrolling to the top of the page with jquery Chrome will remember the last scroll position causing issues with your animation.

4reactions
factorzerocommented, Jul 4, 2017

@yelapijo You can scroll to the top of the page before the fade in like this:

  fadeIn: function() {

    $(window).scrollTop(0); // scroll to top here

    var _this = this;
    var $el = $(this.newContainer);

    $(this.oldContainer).hide();

    $el.css({
      visibility : 'visible',
      opacity : 0
    });

    $el.animate({ opacity: 1 }, 400, function() {
      /**
       * Do not forget to call .done() as soon your transition is finished!
       * .done() will automatically remove from the DOM the old Container
       */

      _this.done();
    });

As long as you put $(window).scrollTop(0); before the animate function you should be fine.

Read more comments on GitHub >

github_iconTop Results From Across the Web

react-router scroll to top on every transition - Stack Overflow
I have an issue when navigating into another page, its position will remain like the page before. So it won ...
Read more >
How to Make an Unobtrusive Scroll-to-Top Button | CSS-Tricks
A button to return to the top of the page allows the user to quickly return to the top of the page without...
Read more >
How to Make a Back to Top Button and Page Progress Bar ...
How to scroll to top whenever the user clicks the Back To Top Button ... The scrollIntoView() (MDN Reference) function scrolls the page...
Read more >
Pure CSS Smooth-Scroll "Back to Top &quot
We also add id="top" to the <header> and use that anchor as the href value for the back to top link. If you...
Read more >
How To Make a Cool Animated Scroll-to-Top Button - Anyday Inc
Let's first hide this button by adding margin-bottom: -12rem; to hide the button. We'll also add a transition: margin-bottom 0.2s; to ensure that...
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