Advertisement
ArtSemkin

Untitled

Nov 13th, 2019
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * ScrollUp button
  3.  */
  4. var ScrollUp = function ($scope) {
  5.  
  6.     var $target = $scope.find('.scroll-up'); // your button
  7.     var VISIBLE_CLASS = 'visible'; // class to toggle
  8.     var OFFSET_TOP = 100; // distance offset in 'px' from where the class is being toggled
  9.  
  10.     // prevent console errors
  11.     if (!$target.length) {
  12.         return;
  13.     }
  14.  
  15.     changeButtonVisibility();
  16.  
  17.     /**
  18.      * Visibility
  19.      */
  20.     function changeButtonVisibility() {
  21.  
  22.         /**
  23.          * Handle Smooth Scroll Bar Plugin
  24.          */
  25.         if (typeof window.SB !== 'undefined') {
  26.  
  27.             window.SB.addListener(changeButtonClass); // add listener to Smooth Scrollbar plugin
  28.  
  29.             /**
  30.              * Handle Normal Scroll using Scrollmagic plugin
  31.              */
  32.         } else {
  33.  
  34.             new $.ScrollMagic.Scene({
  35.                     offset: OFFSET_TOP, // height offset from where it will be shown/hidden
  36.                 })
  37.                 .setClassToggle($target, VISIBLE_CLASS)
  38.                 .addTo(window.SMController); // global ScrollMagic controller
  39.  
  40.         }
  41.  
  42.     }
  43.  
  44.     /**
  45.      * Change class depending on the distance
  46.      * (only Smooth Scrollbar plugin)
  47.      */
  48.     function changeButtonClass() {
  49.  
  50.         if (window.SB.offset.y >= OFFSET_TOP) { // height offset from where it will be shown/hidden
  51.             $target.addClass(VISIBLE_CLASS);
  52.         } else {
  53.             $target.removeClass(VISIBLE_CLASS);
  54.         }
  55.  
  56.     }
  57.  
  58.     /**
  59.      * Interaction
  60.      */
  61.     $target.on('click', function (e) {
  62.  
  63.         e.preventDefault();
  64.  
  65.         /**
  66.          * Handle Normal Scroll with simple jQuery
  67.          */
  68.         $('html, body').animate({
  69.             scrollTop: 0
  70.         }, 600, 'swing');
  71.  
  72.         /**
  73.          * Handle Smooth Scroll Bar Plugin
  74.          */
  75.         if (window.SB !== 'undefined') {
  76.  
  77.             // scroll to the target
  78.             window.SB.scrollTo(0, 0, 600, { // x, y, speed
  79.                 easing: function (pos) { // easing function (if needed)
  80.                     if (pos === 0) return 0;
  81.                     if (pos === 1) return 1;
  82.                     if ((pos /= 0.5) < 1) return 0.5 * Math.pow(2, 10 * (pos - 1));
  83.                     return 0.5 * (-Math.pow(2, -10 * --pos) + 2);
  84.                 }
  85.             });
  86.         }
  87.  
  88.     });
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement