-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2e1e031
commit a1f3133
Showing
7 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import 'govuk-frontend/vendor/polyfills/Function/prototype/bind' | ||
|
||
function BackToTop ($module, options) { | ||
this.$module = $module | ||
this.$observedElement = options.$observedElement | ||
this.intersectionRatio = 0 | ||
} | ||
|
||
BackToTop.prototype.init = function () { | ||
var $observedElement = this.$observedElement | ||
|
||
// If there's no element for the back to top to follow, exit early. | ||
if (!$observedElement) { | ||
return | ||
} | ||
|
||
if (!('IntersectionObserver' in window)) { | ||
// If there's no support fallback to regular sticky behaviour | ||
return this.update() | ||
} | ||
|
||
// Create new IntersectionObserver | ||
var observer = new window.IntersectionObserver(function (entries) { | ||
// Available data when an intersection happens | ||
// Back to top visibility | ||
// Element enters the viewport | ||
if (entries[0].intersectionRatio !== 0) { | ||
// How much of the element is visible | ||
this.intersectionRatio = entries[0].intersectionRatio | ||
// Element leaves the viewport | ||
} else { | ||
this.intersectionRatio = 0 | ||
} | ||
this.update() | ||
}.bind(this), { | ||
// Call the observer, when the element enters the viewport, | ||
// when 25%, 50%, 75% and the whole element are visible | ||
threshold: [0, 0.25, 0.5, 0.75, 1] | ||
}) | ||
|
||
observer.observe($observedElement) | ||
} | ||
|
||
BackToTop.prototype.update = function () { | ||
var thresholdPercent = (this.intersectionRatio * 100) | ||
|
||
if (thresholdPercent === 100) { | ||
this.hide() | ||
} else if (thresholdPercent < 90) { | ||
this.show() | ||
} | ||
} | ||
|
||
BackToTop.prototype.hide = function () { | ||
this.$module.classList.add('app-back-to-top--hidden') | ||
} | ||
|
||
BackToTop.prototype.show = function () { | ||
this.$module.classList.remove('app-back-to-top--hidden') | ||
} | ||
|
||
export default BackToTop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
.app-back-to-top { | ||
position: -webkit-sticky; // Needed for Safari on OSX | ||
position: sticky; // sass-lint:disable-line no-duplicate-properties | ||
top: govuk-spacing(6); | ||
margin-bottom: govuk-spacing(6); | ||
} | ||
|
||
.app-back-to-top__icon { | ||
display: inline-block; | ||
width: .8em; | ||
height: 1em; | ||
margin-top: -(govuk-spacing(1)); | ||
margin-right: govuk-spacing(2); | ||
vertical-align: middle; | ||
} | ||
|
||
@supports (position: sticky) { | ||
.js-enabled .app-back-to-top--hidden { | ||
display: none; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{# Safari on OSX with `position: -webkit-sticky` requires a block level element. | ||
To avoid a large focus area we use a wrapper element. #} | ||
<div class="app-back-to-top app-back-to-top--hidden" data-module="app-back-to-top"> | ||
<a class="govuk-link govuk-link--no-visited-state" href="#top"> | ||
<svg class="app-back-to-top__icon" xmlns="http://www.w3.org/2000/svg" width="13" height="17" viewBox="0 0 13 17"> | ||
<path fill="currentColor" d="M6.5 0L0 6.5 1.4 8l4-4v12.7h2V4l4.3 4L13 6.4z"></path> | ||
</svg>Back to top | ||
</a> | ||
</div> |