|  | 
|  | 1 | +(function( factory ) { | 
|  | 2 | +if (typeof define !== 'undefined' && define.amd) { | 
|  | 3 | +define(['scrollmonitor'], factory); | 
|  | 4 | +} else if (typeof module !== 'undefined' && module.exports) { | 
|  | 5 | +module.exports = factory(require('scrollmonitor')); | 
|  | 6 | +} else { | 
|  | 7 | +window.parallax = factory(window.scrollMonitor); | 
|  | 8 | +} | 
|  | 9 | +})(function (scrollMonitor) { | 
|  | 10 | + | 
|  | 11 | +function SpeedParallax (element, speed) { | 
|  | 12 | +this.speed = speed; | 
|  | 13 | +this.element = element; | 
|  | 14 | +} | 
|  | 15 | + | 
|  | 16 | +SpeedParallax.prototype.handleScroll = function (ratio, distance) { | 
|  | 17 | +var pixels = distance * this.speed; | 
|  | 18 | +this.element.style.transform = 'translateY(' + pixels + 'px)'; | 
|  | 19 | +}; | 
|  | 20 | + | 
|  | 21 | +function OptionsParallax (element, options) { | 
|  | 22 | +this.options = options; | 
|  | 23 | +this.element = element; | 
|  | 24 | +} | 
|  | 25 | + | 
|  | 26 | +OptionsParallax.prototype.handleScroll = function (ratio) { | 
|  | 27 | +var transformString = 'translate3D('; | 
|  | 28 | +if (this.options.end.x) { | 
|  | 29 | +transformString += (this.options.end.x * ratio) + 'px,'; | 
|  | 30 | +} else { | 
|  | 31 | +transformString += '0px,'; | 
|  | 32 | +} | 
|  | 33 | +if (this.options.end.y) { | 
|  | 34 | +transformString += (this.options.end.y * ratio) + 'px,'; | 
|  | 35 | +} else { | 
|  | 36 | +transformString += '0px,'; | 
|  | 37 | +} | 
|  | 38 | +if (this.options.end.z) { | 
|  | 39 | +transformString += (this.options.end.z * ratio) + 'px)'; | 
|  | 40 | +} else { | 
|  | 41 | +transformString += '0px)'; | 
|  | 42 | +} | 
|  | 43 | + | 
|  | 44 | +if (this.options.end.rotate) { | 
|  | 45 | +transformString += ' rotate(' + (this.options.end.rotate * ratio) + 'deg)'; | 
|  | 46 | +} | 
|  | 47 | + | 
|  | 48 | +this.element.style.transform = transformString; | 
|  | 49 | +}; | 
|  | 50 | + | 
|  | 51 | +function Root (element, offsets) { | 
|  | 52 | +this.watcher = scrollMonitor.create(element, offsets); | 
|  | 53 | +this.items = []; | 
|  | 54 | + | 
|  | 55 | +this.pxThru = 0; | 
|  | 56 | +this.ratio = 0; | 
|  | 57 | + | 
|  | 58 | +var self = this; | 
|  | 59 | + | 
|  | 60 | +function handleScroll () { | 
|  | 61 | +var start = Math.max(0, self.watcher.top - scrollMonitor.viewportHeight); | 
|  | 62 | +var end = Math.min(self.watcher.bottom, scrollMonitor.documentHeight - scrollMonitor.viewportHeight); | 
|  | 63 | + | 
|  | 64 | +self.pxThru = Math.max(0, scrollMonitor.viewportTop - start); | 
|  | 65 | +self.ratio = self.pxThru / (end - start); | 
|  | 66 | + | 
|  | 67 | +for (var i=0; i < self.items.length; i++) { | 
|  | 68 | +self.items[i].handleScroll.call(self.items[i], self.ratio, self.pxThru); | 
|  | 69 | +} | 
|  | 70 | +} | 
|  | 71 | + | 
|  | 72 | +this.watcher.enterViewport(function () { | 
|  | 73 | +window.addEventListener('scroll', handleScroll); | 
|  | 74 | +}); | 
|  | 75 | +this.watcher.exitViewport(function () { | 
|  | 76 | +window.removeEventListener('scroll', handleScroll); | 
|  | 77 | +}); | 
|  | 78 | +} | 
|  | 79 | + | 
|  | 80 | +Root.prototype.add = function add (element, optionsOrSpeed) { | 
|  | 81 | +var newItem; | 
|  | 82 | +if (typeof optionsOrSpeed === 'number') { | 
|  | 83 | +newItem = new SpeedParallax(element, optionsOrSpeed); | 
|  | 84 | +} else { | 
|  | 85 | +newItem = new OptionsParallax(element, optionsOrSpeed); | 
|  | 86 | +} | 
|  | 87 | + | 
|  | 88 | +this.items.push(newItem); | 
|  | 89 | +}; | 
|  | 90 | + | 
|  | 91 | +return { | 
|  | 92 | +create: function (item, offsets) { | 
|  | 93 | +return new Root(item, offsets); | 
|  | 94 | +} | 
|  | 95 | +}; | 
|  | 96 | +}); | 
0 commit comments