Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * ScrollUp button
- */
- var ScrollUp = function ($scope) {
- var $target = $scope.find('.scroll-up'); // your button
- var VISIBLE_CLASS = 'visible'; // class to toggle
- var OFFSET_TOP = 100; // distance offset in 'px' from where the class is being toggled
- // prevent console errors
- if (!$target.length) {
- return;
- }
- changeButtonVisibility();
- /**
- * Visibility
- */
- function changeButtonVisibility() {
- /**
- * Handle Smooth Scroll Bar Plugin
- */
- if (typeof window.SB !== 'undefined') {
- window.SB.addListener(changeButtonClass); // add listener to Smooth Scrollbar plugin
- /**
- * Handle Normal Scroll using Scrollmagic plugin
- */
- } else {
- new $.ScrollMagic.Scene({
- offset: OFFSET_TOP, // height offset from where it will be shown/hidden
- })
- .setClassToggle($target, VISIBLE_CLASS)
- .addTo(window.SMController); // global ScrollMagic controller
- }
- }
- /**
- * Change class depending on the distance
- * (only Smooth Scrollbar plugin)
- */
- function changeButtonClass() {
- if (window.SB.offset.y >= OFFSET_TOP) { // height offset from where it will be shown/hidden
- $target.addClass(VISIBLE_CLASS);
- } else {
- $target.removeClass(VISIBLE_CLASS);
- }
- }
- /**
- * Interaction
- */
- $target.on('click', function (e) {
- e.preventDefault();
- /**
- * Handle Normal Scroll with simple jQuery
- */
- $('html, body').animate({
- scrollTop: 0
- }, 600, 'swing');
- /**
- * Handle Smooth Scroll Bar Plugin
- */
- if (window.SB !== 'undefined') {
- // scroll to the target
- window.SB.scrollTo(0, 0, 600, { // x, y, speed
- easing: function (pos) { // easing function (if needed)
- if (pos === 0) return 0;
- if (pos === 1) return 1;
- if ((pos /= 0.5) < 1) return 0.5 * Math.pow(2, 10 * (pos - 1));
- return 0.5 * (-Math.pow(2, -10 * --pos) + 2);
- }
- });
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement