Advertisement
Guest User

Untitled

a guest
Jul 5th, 2016
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2. onePageScroll(".main", {});
  3. </script>
  4.  
  5. function onePageScroll(element, options) {
  6.     _root = this;
  7.     el = document.querySelector(element);
  8.     sections = document.querySelectorAll("section");
  9.     total = sections.length;
  10.     status = "off";
  11.     topPos = 0;
  12.     lastAnimation = 0;
  13.     paginationList = "";
  14.     body = document.querySelector("body");
  15.  
  16.     this.init = function() {
  17.         /*-------------------------------------------*/
  18.         /*  Prepare Everything                       */
  19.         /*-------------------------------------------*/
  20.  
  21.         _addClass(el, "onepage-wrapper")
  22.         el.style.position = "relative";
  23.  
  24.         for( var i = 0; i < sections.length; i++){
  25.             _addClass(sections[i], "ops-section")
  26.             sections[i].dataset.index = i + 1;
  27.             topPos = topPos + 100;
  28.         }
  29.  
  30.         _swipeEvents(el);
  31.         document.addEventListener("swipeDown",  function(event){
  32.             if (!_hasClass(body, "disabled-onepage-scroll")) event.preventDefault();
  33.             moveUp(el);
  34.         });
  35.         document.addEventListener("swipeUp", function(event){
  36.             if (!_hasClass(body, "disabled-onepage-scroll")) event.preventDefault();
  37.             moveDown(el);
  38.         });
  39.  
  40.         // Create Pagination and Display Them
  41.  
  42.         if(window.location.hash != "" && window.location.hash != "#1") {
  43.             var init_index =  window.location.hash.replace("#", ""),
  44.             next = document.querySelector("section" + "[data-index='" + (init_index) + "']"),
  45.             next_index = next.dataset.index;
  46.  
  47.             _addClass( document.querySelector("section" + "[data-index='" + init_index + "']") ,"active")
  48.             _addClass(body, "viewing-page-"+ init_index)
  49.  
  50.             if(next) {
  51.                 _addClass(next, "active")
  52.  
  53.                 body.className = body.className.replace(/\bviewing-page-\d.*?\b/g, '');
  54.                 _addClass(body, "viewing-page-" + next_index)
  55.             }
  56.  
  57.             var pos = ((init_index - 1) * 100) * -1;
  58.             _transformPage(el, pos, init_index);
  59.         } else {
  60.             _addClass(document.querySelector("section" + "[data-index='1']"), "active");
  61.             _addClass(body, "viewing-page-1");
  62.         }
  63.  
  64.         _paginationHandler = function() {
  65.             var page_index = this.dataset.index;
  66.             moveTo(el, page_index);
  67.         }
  68.  
  69.         _mouseWheelHandler = function(event) {
  70.             event.preventDefault();
  71.             var delta = event.wheelDelta || -event.detail;
  72.             if (!_hasClass(body, "disabled-onepage-scroll")) _init_scroll(event, delta);
  73.         }
  74.  
  75.         document.addEventListener('mousewheel', _mouseWheelHandler);
  76.         document.addEventListener('DOMMouseScroll', _mouseWheelHandler);
  77.  
  78.         _keydownHandler = function(e) {
  79.             var tag = e.target.tagName.toLowerCase();
  80.  
  81.             if (!_hasClass(body, "disabled-onepage-scroll")) {
  82.                 switch(e.which) {
  83.                     case 38:
  84.                         if (tag != 'input' && tag != 'textarea') moveUp(el)
  85.                         break;
  86.                     case 40:
  87.                         if (tag != 'input' && tag != 'textarea') moveDown(el)
  88.                         break;
  89.                     default:
  90.                         return;
  91.                 }
  92.             }
  93.  
  94.             return false;
  95.         }
  96.  
  97.         document.onkeydown = _keydownHandler;
  98.         return false;
  99.     }
  100.  
  101.     /*-------------------------------------------------------*/
  102.     /*  Private Functions                                    */
  103.     /*-------------------------------------------------------*/
  104.     /*------------------------------------------------*/
  105.     /*  Credit: Eike Send for the awesome swipe event */
  106.     /*------------------------------------------------*/
  107.     _swipeEvents = function(el){
  108.         var startX,
  109.         startY;
  110.  
  111.         document.addEventListener("touchstart", touchstart);  
  112.  
  113.         function touchstart(event) {
  114.             var touches = event.touches;
  115.             if (touches && touches.length) {
  116.                 startX = touches[0].pageX;
  117.                 startY = touches[0].pageY;
  118.                 document.addEventListener("touchmove", touchmove);
  119.             }
  120.         }
  121.  
  122.         function touchmove(event) {
  123.             var touches = event.touches;
  124.             if (touches && touches.length) {
  125.                 event.preventDefault();
  126.                 var deltaX = startX - touches[0].pageX;
  127.                 var deltaY = startY - touches[0].pageY;
  128.  
  129.                 if (deltaX >= 50) {
  130.                     var event = new Event('swipeLeft');
  131.                     document.dispatchEvent(event);
  132.                 }
  133.                 if (deltaX <= -50) {
  134.                     var event = new Event('swipeRight');
  135.                     document.dispatchEvent(event);
  136.                 }
  137.                 if (deltaY >= 50) {
  138.                     var event = new Event('swipeUp');
  139.                     document.dispatchEvent(event);
  140.                 }
  141.                 if (deltaY <= -50) {
  142.                     var event = new Event('swipeDown');
  143.                     document.dispatchEvent(event);
  144.                 }
  145.  
  146.                 if (Math.abs(deltaX) >= 50 || Math.abs(deltaY) >= 50) {
  147.                     document.removeEventListener('touchmove', touchmove);
  148.                 }
  149.             }
  150.         }
  151.  
  152.     };
  153.     /*-----------------------------------------------------------*/
  154.     /*  Utility to add/remove class easily with javascript       */
  155.     /*-----------------------------------------------------------*/
  156.  
  157.   _trim = function(str) {
  158.       return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
  159.   }
  160.  
  161.   _hasClass = function(ele,cls) {
  162.     if (ele.className) {
  163.       return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
  164.     } else {
  165.       return ele.className = cls;
  166.     }
  167.   }
  168.  
  169.   _addClass = function(ele,cls) {
  170.     if (!_hasClass(ele,cls)) ele.className += " "+cls;
  171.     ele.className = _trim(ele.className)
  172.   }
  173.  
  174.   _removeClass = function(ele,cls) {
  175.     if (_hasClass(ele,cls)) {
  176.         var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
  177.         ele.className=ele.className.replace(reg,' ');
  178.     }
  179.     ele.className = _trim(ele.className)
  180.   }
  181.  
  182.   /*-----------------------------------------------------------*/
  183.     /*  Transtionend Normalizer by Modernizr                     */
  184.     /*-----------------------------------------------------------*/
  185.  
  186.   _whichTransitionEvent = function(){
  187.     var t;
  188.     var el = document.createElement('fakeelement');
  189.     var transitions = {
  190.       'transition':'transitionend',
  191.       'OTransition':'oTransitionEnd',
  192.       'MozTransition':'transitionend',
  193.       'WebkitTransition':'webkitTransitionEnd'
  194.     }
  195.  
  196.     for(t in transitions){
  197.         if( el.style[t] !== undefined ){
  198.             return transitions[t];
  199.         }
  200.     }
  201.   }
  202.  
  203.   /*-----------------------------------------------------------*/
  204.     /*  Function to perform scroll to top animation              */
  205.     /*-----------------------------------------------------------*/
  206.  
  207.   _scrollTo = function(element, to, duration) {
  208.     if (duration < 0) return;
  209.     var difference = to - element.scrollTop;
  210.     var perTick = difference / duration * 10;
  211.  
  212.     setTimeout(function() {
  213.         element.scrollTop = element.scrollTop + perTick;
  214.         if (element.scrollTop == to) return;
  215.         _scrollTo(element, to, duration - 10);
  216.     }, 10);
  217.   }
  218.  
  219.  
  220.      
  221.   /*---------------------------------*/
  222.   /*  Function to transform the page */
  223.   /*---------------------------------*/
  224.  
  225.   _transformPage = function(el2, pos, index, next_el) {
  226.    
  227.     var transformCSS = "-webkit-transform: translate3d(0, " + pos + "%, 0); -webkit-transition: -webkit-transform 500ms ease; -moz-transform: translate3d(0, " + pos + "%, 0); -moz-transition: -moz-transform 500ms ease; -ms-transform: translate3d(0, " + pos + "%, 0); -ms-transition: -ms-transform 500ms ease; transform: translate3d(0, " + pos + "%, 0); transition: transform 500ms ease;";
  228.    
  229.     el2.style.cssText = transformCSS;
  230.    
  231.     var transitionEnd = _whichTransitionEvent();
  232.      el2.addEventListener(transitionEnd, endAnimation, false);
  233.    
  234.     function endAnimation() {
  235.       el2.removeEventListener(transitionEnd, endAnimation)
  236.     }
  237.   }
  238.    
  239.     /*-------------------------------------------*/
  240.   /*  Initialize scroll detection              */
  241.   /*-------------------------------------------*/
  242.  
  243.   _init_scroll = function(event, delta) {
  244.         var deltaOfInterest = delta,
  245.             timeNow = new Date().getTime();
  246.            
  247.         // Cancel scroll if currently animating or within quiet period
  248.         if(timeNow - lastAnimation < 0 + 500) {
  249.             event.preventDefault();
  250.             return;
  251.         }
  252.  
  253.         if (deltaOfInterest < 0) {
  254.             moveDown(el)
  255.         } else {
  256.             moveUp(el)
  257.         }
  258.        
  259.         lastAnimation = timeNow;
  260.     }
  261.    
  262.  
  263.   /*-------------------------------------------------------*/
  264.   /*  Public Functions                                     */
  265.   /*-------------------------------------------------------*/
  266.  
  267.   /*---------------------------------*/
  268.   /*  Function to move down section  */
  269.   /*---------------------------------*/
  270.  
  271.    this.moveDown = function(el3) {
  272.    
  273.     if (typeof el3 == "string") el3 = document.querySelector(el3);
  274.    
  275.     var index = document.querySelector("section" +".active").dataset.index,
  276.             current = document.querySelector("section" + "[data-index='" + index + "']"),
  277.             next = document.querySelector("section" + "[data-index='" + (parseInt(index) + 1) + "']");
  278.            
  279.            
  280.         if(!next) {
  281.                 return
  282.  
  283.         }else {
  284.             pos = (index * 100) * -1;
  285.         }
  286.         var next_index = next.dataset.index;
  287.         _removeClass(current, "active");
  288.         _addClass(next, "active");
  289.  
  290.         body.className = body.className.replace(/\bviewing-page-\d.*?\b/g, '');
  291.         _addClass(body, "viewing-page-"+ next_index);
  292.  
  293.         _transformPage(el3, pos, next_index, next);
  294.     }
  295.    
  296.     /*---------------------------------*/
  297.   /*  Function to move up section    */
  298.   /*---------------------------------*/
  299.    
  300.     this.moveUp = function(el4) {
  301.      
  302.       if (typeof el4 == "string") el4 = document.querySelector(el4);
  303.      
  304.       var index = document.querySelector("section" +".active").dataset.index,
  305.             current = document.querySelector("section" + "[data-index='" + index + "']"),
  306.             next = document.querySelector("section" + "[data-index='" + (parseInt(index) - 1) + "']");
  307.  
  308.         if(!next) {
  309.                 return
  310.         }else {
  311.             pos = ((next.dataset.index - 1) * 100) * -1;
  312.         }
  313.         var next_index = next.dataset.index;
  314.         _removeClass(current, "active")
  315.         _addClass(next, "active")
  316.        
  317.         body.className = body.className.replace(/\bviewing-page-\d.*?\b/g, '');
  318.         _addClass(body, "viewing-page-"+ next_index);
  319.  
  320.         _transformPage(el4, pos, next_index, next);
  321.     }
  322.    
  323.   this.init();
  324. }
  325. /*------------------------------------------------*/
  326.  /*  Ulitilities Method                            */
  327.  /*------------------------------------------------*/
  328.  
  329.  /*-----------------------------------------------------------*/
  330.  /*  Function by John Resig to replicate extend functionality */
  331.  /*-----------------------------------------------------------*/
  332.  
  333.  Object.extend = function(orig){
  334.    if ( orig == null )
  335.      return orig;
  336.  
  337.    for ( var i = 1; i < arguments.length; i++ ) {
  338.      var obj = arguments[i];
  339.      if ( obj != null ) {
  340.        for ( var prop in obj ) {
  341.          var getter = obj.__lookupGetter__( prop ),
  342.              setter = obj.__lookupSetter__( prop );
  343.  
  344.          if ( getter || setter ) {
  345.            if ( getter )
  346.              orig.__defineGetter__( prop, getter );
  347.            if ( setter )
  348.              orig.__defineSetter__( prop, setter );
  349.          } else {
  350.            orig[ prop ] = obj[ prop ];
  351.          }
  352.        }
  353.      }
  354.    }
  355.  
  356.    return orig;
  357.  };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement