Skip to content

Commit

Permalink
Merge pull request #110 from freestylebit/master
Browse files Browse the repository at this point in the history
Added vertical padding option, made it compatible with npm
  • Loading branch information
tholman authored Jul 26, 2016
2 parents 6279b1d + 405aa66 commit ecdff40
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,26 @@ window.onload = function() {
</script>
```

If you want to scroll to a point on the page with some extra padding on the top, simply add the "verticalPadding" option:

```html
<div class="elevator-button">Take the elevator to the target</div>

<script>
// Elevator script included on the page, already.
window.onload = function() {
var elevator = new Elevator({
element: document.querySelector('.elevator-button'),
targetElement: document.querySelector('#elevator-target'),
verticalPadding: 100, // in pixels
mainAudio: '/src/to/audio.mp3',
endAudio: '/src/to/end-audio.mp3'
});
}
</script>
```

If you're really serious (boring), you don't have to use audio... and can also set a fixed time to scroll to the top
```html
<div class="elevator-button">Back to Top</div>
Expand Down
18 changes: 15 additions & 3 deletions elevator.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var Elevator = function(options) {
var startPosition = null;
var endPosition = 0;
var targetElement = null;
var verticalPadding = null;
var elevating = false;

var startCallback;
Expand Down Expand Up @@ -61,6 +62,11 @@ var Elevator = function(options) {
verticalOffset += element.offsetTop || 0;
element = element.offsetParent;
}

if ( verticalPadding ) {
verticalOffset = verticalOffset - verticalPadding;
}

return verticalOffset;
}

Expand Down Expand Up @@ -138,7 +144,7 @@ var Elevator = function(options) {

function updateEndPosition() {
if(targetElement){
endPosition = getVerticalOffset(targetElement);
endPosition = getVerticalOffset(targetElement);
}
}

Expand Down Expand Up @@ -174,7 +180,7 @@ var Elevator = function(options) {
mainAudio.currentTime = 0;
}

updateEndPosition();
updateEndPosition();
window.scrollTo(0, endPosition);
}
}
Expand Down Expand Up @@ -227,6 +233,10 @@ var Elevator = function(options) {
targetElement = _options.targetElement;
}

if( _options.verticalPadding ) {
verticalPadding = _options.verticalPadding;
}

window.addEventListener('blur', onWindowBlur, false);

if( _options.mainAudio ) {
Expand All @@ -250,4 +260,6 @@ var Elevator = function(options) {
}

init(options);
};
};

module.exports = Elevator;
2 changes: 1 addition & 1 deletion elevator.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "elevator.js",
"version": "1.0.0",
"description": "Elevator.js fixes those awkward \"scroll to top\" moments the old fashioned way.",
"main": "elevator.min.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/tholman/elevator.js.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/tholman/elevator.js/issues"
},
"homepage": "https://github.com/tholman/elevator.js#readme"
}

0 comments on commit ecdff40

Please sign in to comment.