* This function scrolls smoothly to an element if there is a hash in the URL.
* E.g. you have `#example` in URL, then it scrolls to element with id `example`.
*
* Note:
* Because of UX, this behaviour is limited only when whole document is loaded in less than 500ms.
* Otherwise, it jumps directly to desired element without smooth scrolling, because too visible jumping through the page would appear.
*/
consthandleLoad=()=>{
if (typeofdocument.querySelector==='undefined'){
return;
}
if (typeofdocument.addEventListener==='undefined'){
return;
}
if (typeofwindowObject.location==='undefined'){
return;
}
handleInteraction (){
if ((typeofdocument.querySelector==='undefined')||(typeofdocument.querySelectorAll==='undefined')){
return;
}
if (typeofdocument.addEventListener==='undefined'){
return;
}
// If no hash, we do not need to run scrolling.
if (!windowObject.location.hash){
return;
}
// If performance is not present, the browser would not scroll smoothly otherwise I guess. So let's skip it completely, it's not worth fallbacking to Date() function.
* This function scrolls smoothly to an element if there is a hash in the URL.
* E.g. you have `#example` in URL, then it scrolls to element with id `example`.
*
* Note:
* Because of UX, this behaviour is limited only when whole document is loaded in less than 500ms.
* Otherwise, it jumps directly to desired element without smooth scrolling, because too visible jumping through the page would appear.
*/
handleLoad (){
if (typeofdocument.querySelector==='undefined'){
return;
}
if (typeofdocument.addEventListener==='undefined'){
return;
}
if (typeofthis.windowObject.location==='undefined'){
return;
}
consthash=e.currentTarget.getAttribute('href');
if (!hash){
return;
}
// If no hash, we do not need to run scrolling.
if (!this.windowObject.location.hash){
return;
}
e.preventDefault();
e.stopPropagation();
// If performance is not present, the browser would not scroll smoothly otherwise I guess. So let's skip it completely, it's not worth fallbacking to Date() function.
if (typeofperformance==='undefined'){
return;
}
scrollTo(hash,documentObject,windowObject);
});
}
});
};
// Start timer.
conststart=performance.now();
/*
* The `load` event has been chosen intentionally as it is the state when everything is ready -
* all styles are loaded and offsets are computed correctly - so the scroll will be computed correctly.
@@ -8,18 +8,45 @@ As smoothscroll functionality is nice and more user-friendly, this library solve
Additionally this library comes with custom easing function registered by default which works as basic ease-in-out with one modification that it skips content if it is too long. This results in nicer transition between two parts of a page.
## Development
## Usage
Import library using ES6 `import` statement and call the smoothscroll function.
It accepts one optional parameter - an `options` object, which allows you to turn on/off some behaviour.
```javascript
SmoothScroll([options])
```
### Options
| Option | Value | Default value | Description |
| --- | --- | --- | --- |
| `load` | `true`/`false` | `true` | Causes smooth scroll to anchored element when the page is loaded.\*
| `interaction` | `true`/`false` | `true` | Causes smooth scroll on given element when user clicks on an `a` tag having `href` starting with `#` character.
\*Note: when the page load lasts more than 500 ms, load smooth scrolling is disable as it would lead to user-unfriendly behaviour like jumping on the page up and down.
Whole library consists only of one file - `index.js`. If you need to check how the smoothscrolling looks, see *Visual check* section of this readme.
Usage example with all options passed:
```javascript
SmoothScroll({
load:true,
interaction:true,
});
```
## Development
## Visual check
Whole library consists only of one file - `index.js`.
For checking a visual feeling of the smoothscroll funcionality, you can take advantage of a testing file `visual.html`.
You need to run build first to get it work with following commands:
If you need to check visually how the smoothscrolling behaviour acts like, you can take advantage of a testing file `visual.html` which has some lorem ipsum data and few of links to navigate through the content and test smooth scrolling.
To get it work, you need to run build first with following commands:
```bash
yarn install
gulp
yarn run gulp
```
And you are ready to view it by opening `visual.html` in your favourite browser. To track for changes, run `gulp watch`instead of `gulp`.