-
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.
Move back to top code into components folder
- Loading branch information
1 parent
c29bc01
commit 8cf1549
Showing
2 changed files
with
49 additions
and
41 deletions.
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,44 @@ | ||
function BackToTop ($module, options) { | ||
this.$module = $module | ||
this.$observedElement = options.$observedElement | ||
} | ||
|
||
BackToTop.prototype.init = function () { | ||
var $module = this.$module | ||
var $observedElement = this.$observedElement | ||
|
||
// Create new IntersectionObserver | ||
var observer = new window.IntersectionObserver(function (entries) { | ||
// Available data when an intersection happens | ||
// Back to top visibility | ||
var intersectionRatio = 0 | ||
// Element enters the viewport | ||
if (entries[0].intersectionRatio !== 0) { | ||
// How much of the element is visible | ||
intersectionRatio = entries[0].intersectionRatio | ||
// Element leaves the viewport | ||
} else { | ||
intersectionRatio = 0 | ||
} | ||
updateStatus(intersectionRatio) | ||
}, { | ||
// 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) | ||
|
||
// Display the current status | ||
function updateStatus (threshold) { | ||
var thresholdPercent = (threshold * 100) | ||
|
||
if (thresholdPercent === 100) { | ||
$module.classList.add('app-back-to-top--hidden') | ||
} else if (thresholdPercent < 90) { | ||
$module.classList.remove('app-back-to-top--hidden') | ||
} | ||
} | ||
} | ||
|
||
export default BackToTop |