avalynx / avalynx-scrolltotop
AvalynxScrollToTop is a simple, lightweight, and customizable scroll-to-top button component. Based on Bootstrap >=5.3 without any framework dependencies.
Package info
github.com/avalynx/avalynx-scrolltotop
Language:JavaScript
pkg:composer/avalynx/avalynx-scrolltotop
Requires
- twbs/bootstrap: ^5.3
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
AvalynxScrollToTop is a simple, lightweight, and customizable scroll-to-top button component. It is based on Bootstrap (version 5.3 or higher) and does not require any other framework dependencies.
Features
- Flexible Positioning: Place the button at
bottom-right,bottom-left,top-rightortop-left. - Container Support: Attach the button to the page (
position: fixed) or to any scrollable container (position: sticky). - Smooth Scrolling: Scrolls back to the top with native smooth scroll behavior (can be disabled).
- Custom Icons: Use the default SVG arrow or any icon markup (e.g. Font Awesome). The markup is sanitized to prevent XSS.
- Optional Theme: A square, sharp-cornered theme stylesheet that takes its colors from Bootstrap variables and supports light and dark mode.
- Accessible: Configurable
aria-labeland a visible focus ring.
Examples
Here is an overview of various use cases for AvalynxScrollToTop:
- Overview
- Simple scrolltotop
- Scrolltotop with options
- Scrolltotop in container
- Scrolltotop in every position
- Scrolltotop theme
Installation
To use AvalynxScrollToTop in your project, you can directly include it in your HTML file. Ensure you have Bootstrap 5.3 or higher included in your project for AvalynxScrollToTop to work correctly.
First, include Bootstrap:
<!-- Bootstrap --> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3/dist/js/bootstrap.bundle.min.js"></script>
Then, include AvalynxScrollToTop:
<link href="path/to/avalynx-scrolltotop.css" rel="stylesheet"> <script src="path/to/avalynx-scrolltotop.js"></script>
Replace path/to/avalynx-scrolltotop.js and path/to/avalynx-scrolltotop.css with the actual path to the files in your project. The stylesheet is the optional theme; without it, the button uses the Bootstrap classes from the className option.
Installation via jsDelivr (Link)
AvalynxScrollToTop is also available via jsDelivr. You can include it in your project like this:
<link href="https://cdn.jsdelivr.net/npm/avalynx-scrolltotop@1.0.2/dist/css/avalynx-scrolltotop.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/avalynx-scrolltotop@1.0.2/dist/js/avalynx-scrolltotop.js"></script>
Make sure to also include Bootstrap's JS/CSS in your project to ensure AvalynxScrollToTop displays correctly.
Installation via NPM (Link)
AvalynxScrollToTop is also available as a npm package. You can add it to your project with the following command:
npm install avalynx-scrolltotop
After installing, you can import AvalynxScrollToTop into your JavaScript file like this:
import { AvalynxScrollToTop } from 'avalynx-scrolltotop'; import 'avalynx-scrolltotop/dist/css/avalynx-scrolltotop.css';
Make sure to also include Bootstrap's JS/CSS in your project to ensure AvalynxScrollToTop displays correctly.
Installation via Symfony AssetMapper
php bin/console importmap:require avalynx-scrolltotop
After installing, you can import AvalynxScrollToTop into your JavaScript file like this:
import { AvalynxScrollToTop } from 'avalynx-scrolltotop'; import 'avalynx-scrolltotop/dist/css/avalynx-scrolltotop.css';
Make sure to also include Bootstrap's JS/CSS in your project to ensure AvalynxScrollToTop displays correctly.
Installation via Symfony AssetComposer
More information about the Symfony AssetComposer Bundle can be found here.
{% do addAssetComposer('avalynx/avalynx-scrolltotop/dist/css/avalynx-scrolltotop.css') %}
{% do addAssetComposer('avalynx/avalynx-scrolltotop/dist/js/avalynx-scrolltotop.js') %}
Make sure to also include Bootstrap's JS/CSS in your project to ensure AvalynxScrollToTop displays correctly.
Installation via Composer (Link)
AvalynxScrollToTop is also available as a Composer package. You can add it to your project with the following command:
composer require avalynx/avalynx-scrolltotop
After installing, you can import AvalynxScrollToTop into your HTML file like this:
<link href="vendor/avalynx/avalynx-scrolltotop/dist/css/avalynx-scrolltotop.css" rel="stylesheet"> <script src="vendor/avalynx/avalynx-scrolltotop/dist/js/avalynx-scrolltotop.js"></script>
Make sure to also include Bootstrap's JS/CSS in your project to ensure AvalynxScrollToTop displays correctly.
Usage
To create a scroll-to-top button for the page, simply instantiate a new AvalynxScrollToTop object:
new AvalynxScrollToTop();
With custom options:
new AvalynxScrollToTop('body', { scrollTrigger: 200, position: 'bottom-left', className: 'btn btn-dark shadow-sm', smoothScroll: true });
For a scrollable container:
new AvalynxScrollToTop('#my-scrollable-container', { scrollTrigger: 50, position: 'bottom-right' });
Theme
The optional stylesheet avalynx-scrolltotop.css renders a square, sharp-cornered button. Its colors come from Bootstrap variables (--bs-primary, --bs-btn-*) and can be overridden with real values:
button.avalynx-scrolltotop { --avalynx-scrolltotop-color: #ffffff; --avalynx-scrolltotop-bg: #a42636; --avalynx-scrolltotop-bg-hover: #8b202e; --avalynx-scrolltotop-bg-active: #831e2b; --avalynx-scrolltotop-focus-ring: rgba(164, 38, 54, 0.35); }
Options
AvalynxScrollToTop allows the following options for customization:
selector: (string) The CSS selector for the container element (default:'body').options: An object containing the following keys:scrollTrigger: (number) The scroll position (in pixels) that triggers the button visibility (default:100).smoothScroll: (boolean) Whether scrolling back to the top should be smooth (default:true).position: (string) The position of the button. One ofbottom-right,bottom-left,top-right,top-left(default:'bottom-right').className: (string) Additional CSS classes for the button (default:'btn btn-primary').icon: (string) The icon markup. Scripts, event-handler attributes,javascript:URLs and embedded/browsing elements are stripped (default: SVG arrow-up).ariaLabel: (string) The aria-label of the button (default:'Scroll to top').zIndex: (string) The z-index of the button (default:'1000').
Contributing
Contributions are welcome! If you'd like to contribute, please fork the repository and submit a pull request with your changes or improvements. We're looking for contributions in the following areas:
- Bug fixes
- Feature enhancements
- Documentation improvements
Before submitting your pull request, please ensure your changes are well-documented and follow the existing coding style of the project.
License
AvalynxScrollToTop is open-sourced software licensed under the MIT license.
Contact
If you have any questions, feature requests, or issues, please open an issue on our GitHub repository or submit a pull request.
Thank you for considering AvalynxScrollToTop for your project!