Code Monkey home page Code Monkey logo

nuxt-anchorscroll's Introduction

nuxt-anchorscroll

npm version npm downloads License Nuxt

This module provides scroll implementation (scroll to top and scroll to anchor element). Originally it was intended for anchor scrolling that's why it's called nuxt-anchorscroll

Features

  • Configured out of the box
  • Supports both kinds of layouts*
  • Extendable

Configured out of the box

  1. For top scroll - scroll instantly, until top with zero offset, ignore x axis
  2. For anchor scroll - scroll smoothly, until top element with zero offset, ignore x axis
  3. Surfaces - html and body elements
  4. General function - scroll to anchor if element exist (uses route.hash as selector), otherwise to top - respects page meta nuxt-anchorscroll options

Supports both kinds of layouts*

In common case, you use cropped html or full html. In first case (you can check this now) scroll to anchor will not work. If it so, you can have a minimal setup.

But in case if anchor scroll is handled (by browser), you need additional setup - full explanation in module playground.

Extendable

Anchor scroll can be specified for needed route via matched field of NuxtApp.$anchorScroll runtime configuration (default configuration setups before script setup)

nuxtApp.$anchorScroll!.matched.push(({ path, hash }) => {
  // Exit when route is not represent fixed example
  if (!path.startsWith('/standard/fixed'))
    return undefined

  if (hash) {
    // All anchor element on this route is mangled
    const targetSelector = `#fixed-${hash.slice(1)}`
    const targetElement = document.querySelector(targetSelector)
    if (targetElement) {
      return {
        toAnchor: {
          target: targetElement as HTMLElement,
          scrollOptions: toValue(useNuxtApp().$anchorScroll?.defaults?.toAnchor) ?? {},
        },
      }
    }
  }
})

Also your matched function can specify different surfaces for scrolling.

nuxtApp.$anchorScroll!.matched.push(({ path, hash }) => {
  // Exit when route is not represent fixed example
  if (!path.startsWith('/scrollable'))
    return undefined

  const surfaces = [...document.querySelectorAll('#exited-scrollable-surface')]

  return {
    toAnchor: {
      surfaces,
      scrollOptions: {
        /* ... */
      },
    },
    toTop: {
      surfaces,
      scrollOptions: {
        /* ... */
      },
    }
  }
})

Quick Setup

  1. Add nuxt-anchorscroll dependency to your project

Use your favorite package manager (I prefer yarn)

yarn add -D nuxt-anchorscroll

pnpm add -D nuxt-anchorscroll

npm install --save-dev nuxt-anchorscroll
  1. Add nuxt-anchorscroll to the modules section of nuxt.config.ts
export default defineNuxtConfig({
  modules: [
    'nuxt-anchorscroll',
  ]
})
  1. Additionally, if you are using transitions, probably you also want to scroll on different hook
export default defineNuxtConfig({
  modules: [
    'nuxt-anchorscroll',
  ],

  anchorscroll: {
    hooks: [
      // Or any valid hook if needed
      // Default is `page:finish`
      'page:transition:finish',
    ],
  },
})
  1. Additionally, if you using standard layout, see playground explanation.

That's it! You can now use nuxt-anchorscroll in your Nuxt app โœจ

Composable

Most probably that you want to scroll to anchor ro to top on click. That's possible via useAnchorScroll composable

// Default to top is instant
const { scrollToAnchor, scrollToTop } = useAnchorScroll({
  toTop: {
    scrollOptions: {
      behavior: 'smooth',
      offsetTop: 0,
    }
  },
})

And use it in template

<template>
  <div
    class="box"
    mt-12
    flex flex-row gap-4 align-baseline
  >
    <h2
      :id="id"
      text-3xl font-extrabold
    >
      <slot />
    </h2>
    <NuxtLink
      :href="`#${id}`"
      mb-a mt-a
      text-xl
      @click="scrollToAnchor(id)"
    >
      #
    </NuxtLink>
  </div>
</template>

nuxt-anchorscroll's People

Contributors

helltraitor avatar

Stargazers

Saeed Aldosary avatar Matthias A. avatar  avatar starNGC2237 avatar

Watchers

 avatar

nuxt-anchorscroll's Issues

scrollToAnchor() doesn't scroll to offset

Hello and thank you for your module! I'm facing an issue I cannot fix and seems to be a bug. But maybe I'm just not understanding correctly, so sorry if its that.

I have a top menu, and the menu items should scroll to anchors in the page.
They are defined like this:

<ul>
<li>
  <NuxtLink :to="/" @click="scrollToTop()>
    LOGO
  </NuxtLink>
</li>

<li v-for="menuItem in listOfMenuItems" :key="menuItem.id">
   <NuxtLink  :to="{path: '/', hash: `#${menuItem.id}`}"  @click="scrollToAnchor(menuItem.id)">
       {{ menuItem.title }}
   </NuxtLink>
</li>
</ul>

I've also defined scrollToAnchor:

const { scrollToAnchor, scrollToTop } = useAnchorScroll({
  scrollOptions: {
    behavior: 'smooth',
    offsetTop: -400,
  },
})

Clicking on an element using scrollToAnchor(), it scrolls to the element, but with offsetTop 0.
Changing its value in scrollOptions also does not affect the outcome. It always scrolls to offsetTop: 0.

However, I've also noticed that it does affect the scroll of scrollToTop().

At the moment, I'm kind of lost, but was expecting for scrollToAnchor() to respect the offsetToTop.

Thank you and sorry to bother.

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.