Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

🔥 Turn your boring fixed header into a smart one with three lines of code.

License

NotificationsYou must be signed in to change notification settings

smastrom/vue-use-fixed-header

npmdependency-countGitHub Workflow StatusGitHub Workflow Status

Vue Use Fixed Header

Turn your boring fixed header into a smart one with three lines of code.


Demo:WebsiteExamples:Vue 3 -Nuxt 3


Features

  • Dead simple - Write three lines of code and you're ready to go
  • Enjoyable - When scrolling down, the header is hidden, when scrolling up, the header is shown.
  • Smart - Behaves as expected on page load, hovering, dropdown navigation, top-reached and reduced motion.
  • Dynamic - Functionalities are automatically enabled/disabled if your header turns from fixed/sticky to something else or it is hidden at different viewports
  • Flexible - Works with any scrolling container

Installation

pnpm add vue-use-fixed-header
yarn add vue-use-fixed-header
npm i vue-use-fixed-header

Usage

Pass your header's template ref touseFixedHeader. Then apply the returned reactive styles. That's it.

<script setup>import {ref }from'vue'import {useFixedHeader }from'vue-use-fixed-header'constheaderRef=ref(null)const {styles }=useFixedHeader(headerRef)</script><template>   <headerclass="Header"ref="headerRef":style="styles"><!-- Your content-->   </header></template><style scoped>.Header {position:fixed;/* or sticky*/top:0;/* Other styles...*/}</style>

All you have to do is to setposition: fixed (orsticky) to your header.useFixedHeader will take care of the rest.


Automatic behavior toggling

On resize,useFixedHeader checks your header'sposition anddisplay properties to determine whether its functionalities should be enabled or not.

Disabled means that no event listeners are attached and the returned styles match an empty object{}.

Media queries

Hence feel free to have code like this, it will just work as expected:

.Header {position: fixed;}/* Will be disabled */@media (max-width:768px) {   .Header {position: relative;   }}/* Same */@media (max-width:375px) {   .Header {display: none;   }}

Advanced scenarios

Let's suppose your header in some pages is not fixed/sticky and you're using some reactive logic to determine whether it should be or not.

You can pass a signal to thewatch property ofuseFixedHeader to perform a check everytime the value changes:

<script setup>constroute=useRoute()constheaderRef=ref(null)constisPricingPage=computed(()=>route.name==='Pricing')conststyles=useFixedHeader(headerRef, {   watch: isPricingPage,// Will perform a check everytime the value changes})</script><template>   <headerref="headerRef":style="{         ...styles,         position: isPricingPage ? 'relative' : 'fixed',      }"   ><!-- Your content-->   </header></template>

useFixedHeader will automatically toggle functionalities when navigating to/from thePricing page.

You can pass either aref or acomputed (without.value).


API

declarefunctionuseFixedHeader(target:Ref<HTMLElement|null>|HTMLElement|nulloptions?:UseFixedHeaderOptions):{styles:Readonly<ShallowRef<CSSProperties>>isEnter:ComputedRef<boolean>isLeave:ComputedRef<boolean>}interfaceUseFixedHeaderOptions{/**    * Scrolling container. Matches `document.documentElement` if `null`.    *    *@default null    */root:Ref<HTMLElement|null>|HTMLElement|null/**    * Signal without `.value` (ref or computed) to be watched    * for automatic behavior toggling.    *    *@default null    */watch:Ref<T>|ComputedRef<T>/**    * Whether to transition `opacity` property from 0 to 1    * and vice versa along with the `transform` property    *    *@default false    */transitionOpacity:boolean|Ref<boolean>|ComputedRef<boolean>}

License

MIT Licensed - Simone Mastromattei © 2023

About

🔥 Turn your boring fixed header into a smart one with three lines of code.

Topics

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp