From a7ec4367f588a601272962ec58857469d7b2a78f Mon Sep 17 00:00:00 2001 From: James Homer Date: Fri, 19 Jan 2018 00:15:10 +0000 Subject: [PATCH] add custom scroll behavior example --- examples/custom-scroll-behavior/README.md | 3 + .../custom-scroll-behavior/assets/main.css | 54 +++++++++++++ .../custom-scroll-behavior/nuxt.config.js | 64 ++++++++++++++++ examples/custom-scroll-behavior/package.json | 12 +++ .../custom-scroll-behavior/pages/about.vue | 12 +++ .../custom-scroll-behavior/pages/index.vue | 9 +++ .../custom-scroll-behavior/pages/long.vue | 22 ++++++ .../custom-scroll-behavior/pages/users.vue | 76 +++++++++++++++++++ 8 files changed, 252 insertions(+) create mode 100644 examples/custom-scroll-behavior/README.md create mode 100644 examples/custom-scroll-behavior/assets/main.css create mode 100644 examples/custom-scroll-behavior/nuxt.config.js create mode 100644 examples/custom-scroll-behavior/package.json create mode 100644 examples/custom-scroll-behavior/pages/about.vue create mode 100644 examples/custom-scroll-behavior/pages/index.vue create mode 100644 examples/custom-scroll-behavior/pages/long.vue create mode 100644 examples/custom-scroll-behavior/pages/users.vue diff --git a/examples/custom-scroll-behavior/README.md b/examples/custom-scroll-behavior/README.md new file mode 100644 index 0000000000..a4cb7c2884 --- /dev/null +++ b/examples/custom-scroll-behavior/README.md @@ -0,0 +1,3 @@ +# Custom scroll behavior with Nuxt.js + +https://nuxtjs.org/examples/custom-scroll-behavior diff --git a/examples/custom-scroll-behavior/assets/main.css b/examples/custom-scroll-behavior/assets/main.css new file mode 100644 index 0000000000..1a0d0af818 --- /dev/null +++ b/examples/custom-scroll-behavior/assets/main.css @@ -0,0 +1,54 @@ +body { + font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif; +} + +.container { + text-align: center; + /* padding-top: 200px; */ + font-size: 20px; + transition: all .5s cubic-bezier(.55,0,.1,1); +} + +.page-enter-active, .page-leave-active { + transition: opacity .5s +} +.page-enter, .page-leave-active { + opacity: 0 +} + +.bounce-enter-active { + animation: bounce-in .8s; +} +.bounce-leave-active { + animation: bounce-out .5s; +} +@keyframes bounce-in { + 0% { transform: scale(0) } + 50% { transform: scale(1.5) } + 100% { transform: scale(1) } +} +@keyframes bounce-out { + 0% { transform: scale(1) } + 50% { transform: scale(1.5) } + 100% { transform: scale(0) } +} + +.slide-left-enter, +.slide-right-leave-active { + opacity: 0; + transform: translate(30px, 0); +} +.slide-left-leave-active, +.slide-right-enter { + opacity: 0; + transform: translate(-30px, 0); +} + +.fade-enter, +.fade-leave-active { + opacity: 0; +} +.fade-leave-active, +.fade-enter { + opacity: 0; +} diff --git a/examples/custom-scroll-behavior/nuxt.config.js b/examples/custom-scroll-behavior/nuxt.config.js new file mode 100644 index 0000000000..460ddc5775 --- /dev/null +++ b/examples/custom-scroll-behavior/nuxt.config.js @@ -0,0 +1,64 @@ +const scrollBehavior = function (to, from, savedPosition, ...args) { + if (savedPosition) { + // savedPosition is only available for popstate navigations. + return savedPosition + } else { + const position = {} + + // scroll to anchor by returning the selector + if (to.hash) { + position.selector = to.hash + + if (document.querySelector(to.hash)) { + return position + } + + // if the returned position is falsy or an empty object, + // will retain current scroll position. + return false + } + + return new Promise(resolve => { + // check if any matched route config has meta that requires scrolling to top + if (to.matched.some(m => m.meta.scrollToTop)) { + // coords will be used if no selector is provided, + // or if the selector didn't match any element. + position.x = 0 + position.y = 0 + } + + // if no children detected + if (to.matched.length < 2) { + // scroll to the top of the page + position.x = 0 + position.y = 0 + } else if (to.matched.some((r) => r.components.default.options.scrollToTop)) { + // if one of the children has scrollToTop option set to true + position.x = 0 + position.y = 0 + } + + // wait for the out transition to complete (if necessary) + this.app.$root.$once('triggerScroll', () => { + // if the resolved position is falsy or an empty object, + // will retain current scroll position. + resolve(position) + }) + }) + } +} + +module.exports = { + build: { + vendor: ['axios'] + }, + css: ['~/assets/main.css'], + transition: { + afterLeave() { + this.$root.$emit('triggerScroll') + } + }, + router: { + scrollBehavior + } +} diff --git a/examples/custom-scroll-behavior/package.json b/examples/custom-scroll-behavior/package.json new file mode 100644 index 0000000000..c61dd88174 --- /dev/null +++ b/examples/custom-scroll-behavior/package.json @@ -0,0 +1,12 @@ +{ + "name": "nuxt-routes-transitions", + "dependencies": { + "axios": "^0.15.3", + "nuxt": "latest" + }, + "scripts": { + "dev": "nuxt", + "build": "nuxt build", + "start": "nuxt start" + } +} diff --git a/examples/custom-scroll-behavior/pages/about.vue b/examples/custom-scroll-behavior/pages/about.vue new file mode 100644 index 0000000000..bfeea07ec2 --- /dev/null +++ b/examples/custom-scroll-behavior/pages/about.vue @@ -0,0 +1,12 @@ + + + diff --git a/examples/custom-scroll-behavior/pages/index.vue b/examples/custom-scroll-behavior/pages/index.vue new file mode 100644 index 0000000000..c29449ef3b --- /dev/null +++ b/examples/custom-scroll-behavior/pages/index.vue @@ -0,0 +1,9 @@ + diff --git a/examples/custom-scroll-behavior/pages/long.vue b/examples/custom-scroll-behavior/pages/long.vue new file mode 100644 index 0000000000..2d4f9f5d40 --- /dev/null +++ b/examples/custom-scroll-behavior/pages/long.vue @@ -0,0 +1,22 @@ + + + diff --git a/examples/custom-scroll-behavior/pages/users.vue b/examples/custom-scroll-behavior/pages/users.vue new file mode 100644 index 0000000000..81056d4759 --- /dev/null +++ b/examples/custom-scroll-behavior/pages/users.vue @@ -0,0 +1,76 @@ + + + + +