|
| 1 | +# VueNextLevelScroll - Bring your scroll game to the next level! |
| 2 | + |
| 3 | +<p align="center"> |
| 4 | + <a href="https://travis-ci.org/Developmint/vue-next-level-scroll"><img src="https://img.shields.io/travis/Developmint/vue-next-level-scroll/master.svg" alt="Build Status"></a> |
| 5 | + <a href="https://codecov.io/gh/Developmint/vue-next-level-scroll"><img src="https://img.shields.io/codecov/c/github/Developmint/vue-next-level-scroll/master.svg" alt="Coverage Status"></a> |
| 6 | + <a href="https://www.npmjs.com/package/vue-next-level-scroll"><img src="https://img.shields.io/npm/dm/vue-next-level-scroll.svg" alt="Downloads"></a> |
| 7 | + <a href="https://www.npmjs.com/package/vue-next-level-scroll"><img src="https://img.shields.io/npm/v/vue-next-level-scroll.svg" alt="Version"></a> |
| 8 | + <a href="https://www.npmjs.com/package/vue-next-level-scroll"><img src="https://img.shields.io/npm/l/vue-next-level-scroll.svg" alt="License"></a> |
| 9 | + <a href="https://conventionalcommits.org"><img src="https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg" alt="We use Conventional Commits"></a> |
| 10 | +</p> |
| 11 | + |
| 12 | +> "Click to scroll" **component** using the modern Browser API. |
| 13 | +
|
| 14 | +## 🔥 Features |
| 15 | + |
| 16 | +- Just **one tiny file** |
| 17 | +- Component based (great for **async loading** and code splitting) |
| 18 | +- Universal code/SSR-safe |
| 19 | +- Well tested and **documented** |
| 20 | +- Compatible with Node 8.0+ |
| 21 | +- Vue as the only dependency |
| 22 | +- Highly customizable |
| 23 | + |
| 24 | +## 🔎 Getting started |
| 25 | + |
| 26 | + |
| 27 | +### 📦️ Through NPM |
| 28 | + |
| 29 | +``` |
| 30 | +$ npm install vue-next-level-scroll |
| 31 | +``` |
| 32 | + |
| 33 | +#### Synchronous import |
| 34 | + |
| 35 | +```js |
| 36 | +import VueNextLevelScroll from 'vue-next-level-scroll' |
| 37 | + |
| 38 | + |
| 39 | +export default { |
| 40 | + components: { |
| 41 | + VueNextLevelScroll |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +``` |
| 46 | + |
| 47 | +#### Async import |
| 48 | + |
| 49 | +```js |
| 50 | +import VueNextLevelScroll from 'vue-next-level-scroll' |
| 51 | + |
| 52 | + |
| 53 | +export default { |
| 54 | + components: { |
| 55 | + VueNextLevelScroll |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +``` |
| 60 | + |
| 61 | +### 🔗❌ Using a CDN |
| 62 | + |
| 63 | +Sorry! You can't use *VueNextLevelScroll* with a CDN by now. |
| 64 | + |
| 65 | +## 🔐 Usage |
| 66 | + |
| 67 | +### You might like to go for a Polyfill |
| 68 | + |
| 69 | +VueNextLevelScroll uses the new [`ScrollBehavior specification`](https://developer.mozilla.org/en-US/docs/Web/CSS/scroll-behavior) by default. |
| 70 | +Unfortunately, Firefox is the only browser that has it built-in (by now). |
| 71 | +For this reason, you might like to go for [this polyfill](https://github.com/iamdustan/smoothscroll) (don't worry, less than 2kB after GZIP). |
| 72 | + |
| 73 | + |
| 74 | +### The component based approach |
| 75 | + |
| 76 | +As you likely have seen, there are various *Vue directives* out their that handle scrolling as well. |
| 77 | +You might have used one or two of them already or built one yourself. |
| 78 | + |
| 79 | +**But!** A component can sometimes be the better approach, as it can be tree shaken, is |
| 80 | +better suited for universal/SSR code and can be loaded asynchronously as well! |
| 81 | + |
| 82 | +### Prop overview |
| 83 | + |
| 84 | + |
| 85 | +| Prop | Optional? | Comment | |
| 86 | +|---| --- | --- | |
| 87 | +| target | ✅ | Can be any query selector you want (or a function that returns such). Will be passed to the scroll function | |
| 88 | +| scrollFunction | ✅ | You can define an own scroll function that will take the `target` prop as parameter and can do whatever you like. | |
| 89 | + |
| 90 | + |
| 91 | +### Default scroll function explained |
| 92 | + |
| 93 | +#### Scroll to top |
| 94 | + |
| 95 | +When no `target` prop is set, the default scroll function will trigger a scroll to top: |
| 96 | + |
| 97 | +```html |
| 98 | +<vue-next-level-scroll> |
| 99 | + <img src="https://developmint.de/logo.png"> |
| 100 | +</vue-next-level-scroll> |
| 101 | +``` |
| 102 | + |
| 103 | +#### Scroll to query selector |
| 104 | + |
| 105 | +When the `target` prop is provided, the default scroll function look the DOM node up and smooth scroll to it. |
| 106 | +If the `target` is a class query, the first found element will be chosen to scroll to. |
| 107 | + |
| 108 | +```html |
| 109 | +<vue-next-level-scroll target="#my-target"> |
| 110 | + <img src="https://developmint.de/logo.png"> |
| 111 | +</vue-next-level-scroll> |
| 112 | +<div id="my-target"/> |
| 113 | +``` |
| 114 | + |
| 115 | +#### Scroll to non-existing query selector |
| 116 | + |
| 117 | +When the `target` prop is given but no node matches, a console error will appear. |
| 118 | + |
| 119 | +```html |
| 120 | +<vue-next-level-scroll target="#my-target"> |
| 121 | + <img src="https://developmint.de/logo.png"> |
| 122 | +</vue-next-level-scroll> |
| 123 | +<div id="my-non-existing-target"/> |
| 124 | +``` |
| 125 | + |
| 126 | +```js |
| 127 | +Error: Could not scroll to #my-target |
| 128 | +``` |
| 129 | + |
| 130 | +### Custom scroll function |
| 131 | + |
| 132 | +Most users are satisfied with the default scroll function provided by *VueNextLevelScroll* |
| 133 | +However if you need other behavior you can simply write your own function: |
| 134 | + |
| 135 | +```html |
| 136 | +<template> |
| 137 | + <vue-next-level-scroll |
| 138 | + target="#my-target" |
| 139 | + :scroll="myScroll"> |
| 140 | + <img src="https://developmint.de/logo.png"> |
| 141 | + </vue-next-level-scroll> |
| 142 | + <div id="my-non-existing-target"/> |
| 143 | +</template> |
| 144 | + |
| 145 | +<script> |
| 146 | +export default { |
| 147 | + const myScroll = target => doSomeMagicHere(target) |
| 148 | +} |
| 149 | +</script> |
| 150 | +``` |
| 151 | + |
| 152 | +You might not need the polyfill then as well :wink: |
| 153 | + |
| 154 | +## 🛠️ Contributing |
| 155 | + |
| 156 | +Please see our [CONTRIBUTING.md](./CONTRIBUTING.md) |
| 157 | + |
| 158 | + |
| 159 | +## 📑 License |
| 160 | + |
| 161 | +[MIT License](./LICENSE.md) - Copyright (c) Developmint - Alexander Lichter |
0 commit comments