viewport-utility
Viewport-utility watches the browser and updates DOM classes and JS properties for easy access in your script or stylesheet. It also provides some simple functions to control scroll behaviour and a fallback for vh units in older browsers.
Postcardware
You're free to use this package (it's MIT-licensed), but if it makes it to your production environment you are required to send us a postcard from your hometown, mentioning which of our package(s) you are using.
Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.
The best postcards will get published on the open source page on our website.
Installation
$ npm i viewport-utility
Demo
There is a small demo page.
Usage
var viewport =
Options
You can change these before init()
, or pass an object in init(options)
.
All config values can be overwritten by data-attributes on the root element.
root: // Typically on the HTML elementtoolbar: // When scrolling to an offset, take element into accountvhItems: // Fix vh units for older browsers & iOS7scrollLinks: // Scroll to anchor in hrefclassPrefix: '$viewport-'lang: 'en' // Set by lang-attribute on root element// Default configconfig:scrollOffset: 0 // Add some space when scrolling to an offsetsmall: 768 // Breakpointstart: 0 // Top margin before viewport is 'started'end: 0 // Bottom margin before viewport is 'ended'
Properties
Following properties are updated, and can be usefull in your app code.
// Propertiesheight: 0width: 0start: 0 // Beginning of viewport relative to documentend: 0 // End of viewport relative to documentstate:scrollDisabled : falsesmall: falselarge: falsescrolling: falsestarted: falseended: falseloaded: falseorientation:portrait: falselandscape: falsedirection:down: falseup: false
Functions
// on the root element// init module with options// yes or no for offset or $element// scroll to offset, selector or $element, also to elements that are visible already (default:true)// scroll to hash in url, typically on page load// update handlers and module
CSS
Classes you can use in Sass.
Default setting is classPrefix: '$viewport-'
.
DOM
Data-attributes on root element will override the js options.
The $viewport-loading
class will be removed on page load.
Default html needed when you don't override options:
When using scroll functions: don't scroll under me...Scroll to topScroll to anchorScroll to anchor, only if out of view...Half page for iOS7
Change log
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email willem@spatie.be instead of using the issue tracker.
Credits
About Spatie
Spatie is webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.
License
The MIT License (MIT). Please see License File for more information.