Git Product home page Git Product logo

vue-next-level-scroll's Introduction

VueNextLevelScroll - Bring your scroll game to the next level!

Build Status Code coverage Downloads Version License We use Conventional Commits Thanks badge

"Click to scroll" component using the modern Browser API.

๐Ÿ”ฅ Features

  • Just one tiny file
  • Component based (great for async loading and code splitting)
  • Supports navigation through VueRouter
  • Universal code/SSR-safe
  • Well tested and documented
  • Compatible with Node 8.0+
  • Vue as the only dependency
  • Highly customizable

๐Ÿ”Ž Getting started

โญ Demo

A live demo is available at https://oqxy5xr5ny.sse.codesandbox.io/.

๐Ÿ“ฆ Through NPM

$ npm install vue-next-level-scroll

Synchronous import

import VueNextLevelScroll from 'vue-next-level-scroll'


export default {
  components: {
    VueNextLevelScroll
  }
}

Async import

export default {
  components: {
    VueNextLevelScroll: () => import('vue-next-level-scroll')
  }
}

๐Ÿ”— Using a CDN

UNPKG | jsDelivr (available as window.nextLevelScroll)

Vue.component('scroll', window.nextLevelScroll)

// Continue as you wish. If you want to load a scroll behavior polyfill, do it **before** adding the CDN link.

๐Ÿ› ๏ธ Usage

You might like to go for a Polyfill

VueNextLevelScroll uses the new ScrollBehavior specification by default. Unfortunately, Firefox is the only browser that has it built-in (by now). For this reason, you might like to go for this polyfill (don't worry, less than 2kB after GZIP).

The component based approach

As you likely have seen, there are various Vue directives out their that handle scrolling as well. You might have used one or two of them already or built one yourself.

But! A component can sometimes be the better approach, as it can be tree shaken, is better suited for universal/SSR code and can be loaded asynchronously as well!

Prop overview

Prop Optional? Comment
target โœ… Can be any query selector you want (or a function that returns such). Will be passed to the scroll function
tag โœ… Defaults to div. The HTML tag used for the VueNextLevelScroll component
scrollFunction โœ… You can define an own scroll function that will take the target prop as parameter and can do whatever you like.
shouldNavigate โœ… If set, VueRouter will reflect navigation changes in the url(top: no hash, target: hash)
navigationType โœ… Defaults to push. The navigation type of that VueRouter should use. Usually either push or replace

Default scroll function explained

Scroll to top

When no target prop is set, the default scroll function will trigger a scroll to top:

<vue-next-level-scroll>
  <img src="https://developmint.de/logo.png">
</vue-next-level-scroll>

Scroll to query selector

When the target prop is provided, the default scroll function look the DOM node up and smooth scroll to it. If the target is a class query, the first found element will be chosen to scroll to.

<vue-next-level-scroll target="#my-target">
  <img src="https://developmint.de/logo.png">
</vue-next-level-scroll>
<div id="my-target"/>

Scroll to non-existing query selector

When the target prop is given but no node matches, a console error will appear.

<vue-next-level-scroll target="#my-target">
  <img src="https://developmint.de/logo.png">
</vue-next-level-scroll>
<div id="my-non-existing-target"/>
Error: Could not scroll to #my-target

Custom scroll function

Most users are satisfied with the default scroll function provided by VueNextLevelScroll However if you need other behavior you can simply write your own function:

<template>
    <vue-next-level-scroll
      target="#my-target"
      :scrollFunction="myScroll">
      <img src="https://developmint.de/logo.png">
    </vue-next-level-scroll>
    <div id="my-non-existing-target"/>
</template>

<script>
export default {
 methods: {
   myScroll (target) { doSomeMagicHere(target) }
  }
}
</script>

You might not need the polyfill then as well ๐Ÿ˜‰

โš™๏ธ Contributing

Please see our CONTRIBUTING.md

๐Ÿ“‘ License

MIT License - Copyright (c) Developmint - Alexander Lichter

vue-next-level-scroll's People

Contributors

lupas avatar mannil avatar renovate[bot] avatar

Watchers

 avatar

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.