Giter Site home page Giter Site logo

posva / vue-router-view-transition Goto Github PK

View Code? Open in Web Editor NEW
40.0 4.0 3.0 159 KB

Properly time out-in transitions with scrollBehavior

Home Page: https://vue-router-transition-scrollbehavior.esm.dev

JavaScript 69.86% TypeScript 30.14%
vue vue-router transition animation

vue-router-view-transition's Introduction

vue-router-view-transition Build Status npm package thanks

A transition wrapper for router-view that is compatible with scrollBehavior

Installation

npm install vue-router-view-transition
# or
yarn add vue-router-view-transition

Usage

This library exports one component and one function. You need to use both:

First, you can either globally install the component RouterViewTransition:

import { RouterViewTransition } from 'vue-router-view-transition'

Vue.component('RouterViewTransition', RouterViewTransition)

or import it locally in any Vue component:

import { RouterViewTransition } from 'vue-router-view-transition'

export default {
  components: { RouterViewTransition },
}

Then you should replace your router-view with this router-view-transition:

<router-view-transition transition="fade" mode="out-in" />

Here the transition prop refers to the name of the transition (prop named name for the transition component) while the mode is exactly the same as the prop with the same name in the transition component.

Then you must import the waitForTransition function and wrap your scrollBehavior function:

// probably your router.js file
const router = new Router({
  mode: 'history',
  routes: [
    // your routes
  ],
  scrollBehavior: waitForTransition((to, from, savedPosition) => {
    // this code will get executed only once the transition wrapping router-view is finished
    // this ensures
    if (savedPosition) {
      return savedPosition
    } else {
      // this ensures we go to the top of the page when navigating to a new page
      return { x: 0, y: 0 }
    }
  }),
})

Note: You may also want to enable manual scroll restoration:

history.scrollRestoration = 'manual'

Related

License

MIT

vue-router-view-transition's People

Contributors

posva avatar sixl-daniel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

vue-router-view-transition's Issues

How can this be applied to Nuxt3?

The demo seems quite old - I'm getting this error:

Error: error:0308010C:digital envelope routines::unsupported
    at new Hash (node:internal/crypto/hash:71:19)
    at Object.createHash (node:crypto:133:10)
    at module.exports (<..path>/vue-router-view-transition/node_modules/webpack/lib/util/createHash.js:135:53)

I'd like to apply the delay in the route to a Nuxt 3 project, where I use navigateTo() from one page to another. The landing page has a transition in it, so it can't scroll to the content of these elements with #hash-based navigation until that transition is finished. How can I therefore apply this package to Nuxt3? Or is there anything newer available?

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.