var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } (function ($) { 'use strict'; var _defaults = {}; var Parallax = function () { function Parallax(el, options) { _classCallCheck(this, Parallax); // If exists, destroy and reinitialize if (!!el.M_Parallax) { el.M_Parallax.destroy(); } this.el = el; this.$el = $(el); this.el.M_Parallax = this; this.options = $.extend({}, Parallax.defaults, options); this.$img = this.$el.find('img').first(); this._updateParallax(); this._setupEventHandlers(); this._setupStyles(); Parallax._parallaxes.push(this); } _createClass(Parallax, [{ key: 'destroy', /** * Teardown component */ value: function destroy() {} }, { key: '_setupEventHandlers', value: function _setupEventHandlers() { this._handleImageLoadBound = this._handleImageLoad.bind(this); this.$img[0].addEventListener('load', this._handleImageLoadBound); if (Parallax._parallaxes.length === 0) { Parallax._handleScrollThrottled = M.throttle(Parallax._handleScroll, 5); window.addEventListener('scroll', Parallax._handleScrollThrottled); } } }, { key: '_setupStyles', value: function _setupStyles() { this.$img[0].style.opacity = 1; } }, { key: '_handleImageLoad', value: function _handleImageLoad() { this._updateParallax(); this.$img.each(function () { var el = this; if (el.complete) $(el).trigger("load"); }); } }, { key: '_updateParallax', value: function _updateParallax() { var containerHeight = this.$el.height() > 0 ? this.el.parentNode.offsetHeight : 500; var imgHeight = this.$img[0].offsetHeight; var parallaxDist = imgHeight - containerHeight; var bottom = this.$el.offset().top + containerHeight; var top = this.$el.offset().top; var scrollTop = M.getDocumentScrollTop(); var windowHeight = window.innerHeight; var windowBottom = scrollTop + windowHeight; var percentScrolled = (windowBottom - top) / (containerHeight + windowHeight); var parallax = parallaxDist * percentScrolled; if (bottom > scrollTop && top < scrollTop + windowHeight) { this.$img[0].style.transform = 'translate3D(-50%, ' + parallax + 'px, 0)'; } } }], [{ key: 'init', value: function init($els, options) { var arr = []; $els.each(function () { arr.push(new Parallax(this, options)); }); return arr; } /** * Get Instance */ }, { key: 'getInstance', value: function getInstance(el) { var domElem = !!el.jquery ? el[0] : el; return domElem.M_Parallax; } }, { key: '_handleScroll', value: function _handleScroll() { for (var i = 0; i < Parallax._parallaxes.length; i++) { var parallaxInstance = Parallax._parallaxes[i]; parallaxInstance._updateParallax.call(parallaxInstance); } } }, { key: 'defaults', get: function () { return _defaults; } }]); return Parallax; }(); /** * @static * @memberof Parallax */ Parallax._parallaxes = []; M.Parallax = Parallax; if (M.jQueryLoaded) { M.initializeJqueryWrapper(Parallax, 'parallax', 'M_Parallax'); } })(cash);