File size: 4,283 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*!
 * parallax-element.js
 * jQuery plugin to add parallax effect to individual elements
 * @author  Shaun M. Baer
 * @url     https://github.com/iamhexcoder/parallax-element
 * @license MIT
 */
(function( $, window, document, undefined ) { $.fn.parallaxElement = function(options) {
/*! requestAnimationFrame Polyfill via Paul Irish: https://gist.github.com/paulirish/1579671 */
  (function() {
    var lastTime = 0;
    var vendors = ['ms', 'moz', 'webkit', 'o'];
    for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
      window.requestAnimationFrame  = window[vendors[x]+'RequestAnimationFrame'];
      window.cancelAnimationFrame   = window[vendors[x]+'CancelAnimationFrame'] ||
                                      window[vendors[x]+'CancelRequestAnimationFrame'];
    }
    if (!window.requestAnimationFrame) {
      window.requestAnimationFrame = function(callback, element) {
        var currTime = new Date().getTime();
        var timeToCall = Math.max(0, 16 - (currTime - lastTime));
        var id = window.setTimeout(function() { callback(currTime + timeToCall); },
                 timeToCall);
        lastTime = currTime + timeToCall;
        return id;
      };
    }
    if (!window.cancelAnimationFrame) {
      window.cancelAnimationFrame = function(id) {
        clearTimeout(id);
      };
    }
  }());
/*! end requestAnimationFrame Polyfill */
  var defaults = {
    defaultSpeed:  0.2,   
    useOffset:     true,  
    defaultOffset: 200,   
    disableMobile: false, 
    minWidth:      false  
  };
  var opts = $.extend( {}, defaults, options );
  if( opts.disableMobile && /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
    return false;
  }
  var windowYOffset = window.pageYOffset;
  var winHeight     = "innerHeight" in window ? window.innerHeight : document.documentElement.offsetHeight;
  var winWidth      = "innerWidth" in window ? window.innerWidth : document.documentElement.clientWidth;
  var navToggle     = $('#filter-toggle');
  var winBottom     = (windowYOffset + winHeight);
  var body          = document.body;
  var html          = document.documentElement;
  var docHeight     = Math.max( body.scrollHeight, body.offsetHeight,
                      html.clientHeight, html.scrollHeight, html.offsetHeight );
  $(window).on('resize', function(){
    winHeight = "innerHeight" in window ? window.innerHeight : document.documentElement.offsetHeight;
    winWidth  = window.innerWidth;
    docHeight = Math.max( body.scrollHeight, body.offsetHeight,
                    html.clientHeight, html.scrollHeight, html.offsetHeight );
  });
  function runScrollElement(el) {
    var thisTop = el.offset().top;
    if( opts.useOffset && thisTop - opts.defaultOffset > ( winBottom ) ) {
      return false;
    }
    var speed = el.attr('data-speed') ? ( el.attr('data-speed') * -1 ) : ( opts.defaultSpeed * -1 );
    var val;
    if(el.hasClass('scroll-start-zero')) {
      val = ( windowYOffset * speed );
    } else {
      val = ( ( windowYOffset - thisTop ) + ( winHeight / 2 ) ) * speed;
    }
    if(val > docHeight) {
      val = docHeight;
    }
    var styleVal = 'translate3d( 0px, ' + val + 'px, 0px)';
    el.css({
      "-webkit-transform": styleVal,
      "-moz-transform": styleVal,
      "-ms-transform": styleVal,
      "-o-transform": styleVal,
      "transform": styleVal
    });
  }
  var scrollEls = [];
  this.each( function(i) {
    scrollEls.push( $(this) );
  });
  window.addEventListener("load", function(){
    if ( opts.minWidth && opts.minWidth > winWidth ) {
      $(this).removeAttr('style');
      return false;
    }
    $.each(scrollEls, function(i, el) {
      windowYOffset = window.pageYOffset;
      winBottom     = (windowYOffset + winHeight);
      runScrollElement(el);
    });
  });
  return document.addEventListener('scroll', function(){
    if( opts.minWidth && opts.minWidth > winWidth ) {
      $(this).removeAttr('style');
      return false;
    }
    $.each(scrollEls, function(i, el) {
      windowYOffset = window.pageYOffset;
      winBottom     = (windowYOffset + winHeight);
      window.requestAnimationFrame( function() {
        runScrollElement(el);
      });
    });
  });
}; }( jQuery, window, document ));