Socket
Socket
Sign inDemoInstall

vanilla-back-to-top

Package Overview
Dependencies
Maintainers
1
Versions
59
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vanilla-back-to-top

Tiny and configurable Back To Top button with no dependencies


Version published
Weekly downloads
2.8K
increased by10.39%
Maintainers
1
Weekly downloads
 
Created
Source

Vanilla Back To Top

npm version License Dependency Status File Size Downloads

Tiny and configurable Back To Top button with no dependencies that will work for basically all users in the world

Works universally great with frameworks - React, Angular, Vue etc, and without them, e.g., on pre-rendered static websites like Jekyll, Hugo or Hexo

Just the button:

How to use

Global declaration

This is the simplest way to use it, works great with classic non modular JavaScript.

Add this to your HTML:

<script src="https://unpkg.com/vanilla-back-to-top@7.0.1/dist/vanilla-back-to-top.min.js"></script>
<script>addBackToTop()</script>

If you don't want to rely on unpkg.com, save the file to your project and serve it from your server:

<script src="/assets/vanilla-back-to-top.min.js"></script>
<script>addBackToTop()</script>

Isolated UMD module

This is how you would use it with a modular app, e.g., with React, Angular, Vue etc

Install the package with npm:

npm install --save vanilla-back-to-top

Import it as ES6, Node.js or RequireJS module, for example:

import { addBackToTop } from 'vanilla-back-to-top'
addBackToTop()
// your Vue/React/Angular/etc bootstrapping code here

Styling

You can change the looks of the button using #back-to-top selector in your CSS:

#back-to-top {
  background-image: url(back-to-top.png)
}
@media screen and (max-width: 479px) {
  #back-to-top {
    right: 10px;
    bottom: 10px;
  }
}

Examples of some options

In addition to styling with CSS, you can call addBackToTop function with many options, these are some examples:

  • Change colors:
addBackToTop({
  backgroundColor: 'pink',
  textColor: '#ad19b7'
})
  • Change size:
addBackToTop({
  size: 40
})
addBackToTop({
  innerHTML: '<svg viewBox="0 0 24 24"><path d="M0 0h24v24H0V0z"/><path fill="#fff" d="M4 12l1.41 1.41L11 7.83V20h2V7.83l5.58 5.59L20 12l-8-8-8 8z"/></svg>'
});

All options

These are all the options you can possibly provide to addBackToTop function:

// the values are also the default ones, so the call is equivalent to addBackToTop()
addBackToTop({
  id: 'back-to-top',
  showWhenScrollTopIs: 1, // px
  onClickScrollTo: 0, // px
  scrollDuration: 100, // ms
  innerHTML = '<svg viewBox="0 0 24 24"><path d="M7.41 15.41L12 10.83l4.59 4.58L18 14l-6-6-6 6z"></path></svg>', // https://material.io/icons/#ic_keyboard_arrow_up
  size: 56, // px
  cornerOffset: 20, // px
  backgroundColor: '#000',
  textColor: '#fff',
  zIndex: 1
})
OptionDescription
idid attribute of the button
showWhenScrollTopIsShow the button when page got scrolled by this number of pixels
onClickScrollToWhere to scroll to, in pixels, when the button gets clicked, 0 means the very top
scrollDurationHow long, in milliseconds, scrolling to top should take. Set to 0 to make scrolling instant
innerHTMLHTML code to put inside the button
sizeWidth and height of the button in pixels
cornerOffsetRight and bottom offset of the button relative to viewport
backgroundColorBackground color of the button
textColorText color of the button
zIndexz-index of the button

★ Star me on GitHub

Keywords

FAQs

Package last updated on 06 Jan 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc