|
1 | 1 | /*! |
2 | | - * pagepiling.js 1.5.3 |
| 2 | + * pagepiling.js 1.5.4 |
3 | 3 | * |
4 | 4 | * https://github.com/alvarotrigo/pagePiling.js |
5 | 5 | * @license MIT licensed |
|
16 | 16 | var lastAnimation = 0; |
17 | 17 | var isTouch = (('ontouchstart' in window) || (navigator.msMaxTouchPoints > 0) || (navigator.maxTouchPoints)); |
18 | 18 | var touchStartY = 0, touchStartX = 0, touchEndY = 0, touchEndX = 0; |
| 19 | + var scrollings = []; |
19 | 20 |
|
20 | 21 | //Defines the delay to take place before being able to scroll to the next section |
21 | 22 | //BE CAREFUL! Not recommened to change it under 400 for a good behavior in laptops and |
|
558 | 559 | * http://blogs.sitepointstatic.com/examples/tech/mouse-wheel/index.html |
559 | 560 | * http://www.sitepoint.com/html5-javascript-mouse-wheel/ |
560 | 561 | */ |
| 562 | + var prevTime = new Date().getTime(); |
| 563 | + |
561 | 564 | function MouseWheelHandler(e) { |
562 | | - if(!isMoving()){ |
563 | | - // cross-browser wheel delta |
564 | | - e = window.event || e; |
565 | | - var delta = Math.max(-1, Math.min(1, |
566 | | - (e.wheelDelta || -e.deltaY || -e.detail))); |
| 565 | + var curTime = new Date().getTime(); |
| 566 | + |
| 567 | + // cross-browser wheel delta |
| 568 | + e = e || window.event; |
| 569 | + var value = e.wheelDelta || -e.deltaY || -e.detail; |
| 570 | + var delta = Math.max(-1, Math.min(1, value)); |
| 571 | + |
| 572 | + var horizontalDetection = typeof e.wheelDeltaX !== 'undefined' || typeof e.deltaX !== 'undefined'; |
| 573 | + var isScrollingVertically = (Math.abs(e.wheelDeltaX) < Math.abs(e.wheelDelta)) || (Math.abs(e.deltaX ) < Math.abs(e.deltaY) || !horizontalDetection); |
| 574 | + |
| 575 | + //Limiting the array to 150 (lets not waste memory!) |
| 576 | + if(scrollings.length > 149){ |
| 577 | + scrollings.shift(); |
| 578 | + } |
| 579 | + |
| 580 | + //keeping record of the previous scrollings |
| 581 | + scrollings.push(Math.abs(value)); |
| 582 | + |
| 583 | + //time difference between the last scroll and the current one |
| 584 | + var timeDiff = curTime-prevTime; |
| 585 | + prevTime = curTime; |
| 586 | + |
| 587 | + //haven't they scrolled in a while? |
| 588 | + //(enough to be consider a different scrolling action to scroll another section) |
| 589 | + if(timeDiff > 200){ |
| 590 | + //emptying the array, we dont care about old scrollings for our averages |
| 591 | + scrollings = []; |
| 592 | + } |
567 | 593 |
|
| 594 | + if(!isMoving()){ |
568 | 595 | var activeSection = $('.pp-section.active'); |
569 | 596 | var scrollable = isScrollable(activeSection); |
570 | 597 |
|
571 | | - //scrolling down? |
572 | | - if (delta < 0) { |
573 | | - scrolling('down', scrollable); |
| 598 | + var averageEnd = getAverage(scrollings, 10); |
| 599 | + var averageMiddle = getAverage(scrollings, 70); |
| 600 | + var isAccelerating = averageEnd >= averageMiddle; |
574 | 601 |
|
575 | | - //scrolling up? |
576 | | - }else { |
577 | | - scrolling('up', scrollable); |
578 | | - } |
| 602 | + if(isAccelerating && isScrollingVertically){ |
| 603 | + //scrolling down? |
| 604 | + if (delta < 0) { |
| 605 | + scrolling('down', scrollable); |
579 | 606 |
|
| 607 | + //scrolling up? |
| 608 | + }else if(delta>0){ |
| 609 | + scrolling('up', scrollable); |
| 610 | + } |
| 611 | + } |
580 | 612 |
|
581 | 613 | return false; |
582 | 614 | } |
583 | 615 | } |
584 | 616 |
|
| 617 | + /** |
| 618 | + * Gets the average of the last `number` elements of the given array. |
| 619 | + */ |
| 620 | + function getAverage(elements, number){ |
| 621 | + var sum = 0; |
| 622 | + |
| 623 | + //taking `number` elements from the end to make the average, if there are not enought, 1 |
| 624 | + var lastElements = elements.slice(Math.max(elements.length - number, 1)); |
| 625 | + |
| 626 | + for(var i = 0; i < lastElements.length; i++){ |
| 627 | + sum = sum + lastElements[i]; |
| 628 | + } |
| 629 | + |
| 630 | + return Math.ceil(sum/number); |
| 631 | + } |
| 632 | + |
585 | 633 | /** |
586 | 634 | * Determines the way of scrolling up or down: |
587 | 635 | * by 'automatically' scrolling a section or by using the default and normal scrolling. |
|
606 | 654 | return true; |
607 | 655 | } |
608 | 656 | }else{ |
609 | | - // moved up/down |
| 657 | + //moved up/down |
610 | 658 | scrollSection(); |
611 | 659 | } |
612 | 660 | } |
|
0 commit comments