|
1 | 1 | /*!
|
2 |
| - * jQuery Rellax Plugin v0.1 |
| 2 | + * jQuery Rellax Plugin v0.2.0 |
3 | 3 | * Examples and documentation at http://pixelgrade.github.io/rellax/
|
4 | 4 | * Copyright (c) 2016 PixelGrade http://www.pixelgrade.com
|
5 | 5 | * Licensed under MIT http://www.opensource.org/licenses/mit-license.php/
|
|
9 | 9 | if (!window.requestAnimationFrame) return;
|
10 | 10 |
|
11 | 11 | var $window = $(window),
|
| 12 | + windowWidth = $window.width(), |
12 | 13 | windowHeight = $window.height(),
|
13 |
| - lastKnownScrollY = $window.scrollTop(); |
| 14 | + lastKnownScrollY; |
14 | 15 |
|
15 | 16 | var elements = new Array();
|
16 | 17 |
|
|
21 | 22 | window.requestAnimationFrame(updateAll);
|
22 | 23 | })();
|
23 | 24 |
|
24 |
| - $window.on('resize', function(e) { |
| 25 | + $window.on('load resize', function(e) { |
| 26 | + windowWidth = $window.width(); |
25 | 27 | windowHeight = $window.height();
|
26 | 28 | });
|
27 | 29 |
|
28 |
| - $window.on('scroll', function(e) { |
| 30 | + $window.on('load scroll', function(e) { |
29 | 31 | lastKnownScrollY = $(e.target).scrollTop();
|
30 | 32 | });
|
31 | 33 |
|
|
50 | 52 | self._bindEvents();
|
51 | 53 |
|
52 | 54 | elements.push(self);
|
53 |
| - $el.addClass('rellax-active', true); |
54 | 55 | }
|
55 | 56 |
|
56 | 57 | $.extend(Rellax.prototype, {
|
|
61 | 62 | self._reloadElement();
|
62 | 63 | });
|
63 | 64 | },
|
| 65 | + _scaleElement: function() { |
| 66 | + var parentWidth = this.$parent.data("plugin_" + Rellax).width, |
| 67 | + parentHeight = this.$parent.data("plugin_" + Rellax).height, |
| 68 | + oldWidth = this.width, |
| 69 | + oldHeight = this.height, |
| 70 | + newWidth, |
| 71 | + newHeight; |
| 72 | + |
| 73 | + newHeight = parentHeight + (windowHeight - parentHeight) * this.options.amount; |
| 74 | + newWidth = windowWidth; |
| 75 | + |
| 76 | + scale = Math.max(newWidth / oldWidth, newHeight / oldHeight); |
| 77 | + |
| 78 | + this.width = oldWidth * scale; |
| 79 | + this.height = oldHeight * scale; |
| 80 | + |
| 81 | + this.offset.top -= (newHeight - parentHeight)/2; |
| 82 | + this.offset.left -= (oldWidth * scale - parentWidth)/2; |
| 83 | + }, |
64 | 84 | _reloadElement: function() {
|
65 | 85 | var self = this,
|
66 | 86 | $el = $(this.element).removeAttr('style');
|
|
73 | 93 | self.height = $el.height();
|
74 | 94 | self.offset = $el.offset();
|
75 | 95 |
|
| 96 | + if ( self.$parent.length ) { |
| 97 | + self._scaleElement(); |
| 98 | + } |
| 99 | + |
76 | 100 | if ( self.isContainer ) {
|
77 | 101 | self.width += 2 * self.options.bleed;
|
78 | 102 | self.height += 2 * self.options.bleed;
|
|
87 | 111 | width: self.width,
|
88 | 112 | height: self.height,
|
89 | 113 | marginTop: 0,
|
90 |
| - marginLeft: 0, |
91 |
| - transform: 'none' |
| 114 | + marginLeft: 0 |
92 | 115 | }
|
93 | 116 |
|
94 | 117 | if (self.isContainer) $.extend(style, {zIndex: -1});
|
| 118 | + |
95 | 119 | if (self.$parent.length) {
|
96 | 120 |
|
97 | 121 | $.extend(style, {
|
|
103 | 127 | overflow: 'hidden',
|
104 | 128 | zIndex: -1
|
105 | 129 | });
|
| 130 | + |
106 | 131 | }
|
107 | 132 |
|
108 | 133 | $el.css(style);
|
|
142 | 167 | return this.each(function () {
|
143 | 168 | if ( ! $.data(this, "plugin_" + Rellax) ) {
|
144 | 169 | $.data(this, "plugin_" + Rellax, new Rellax( this, options ));
|
| 170 | + } else { |
| 171 | + var self = $.data(this, "plugin_" + Rellax); |
| 172 | + if ( options && typeof options == 'string' ) { |
| 173 | + if ( options == "refresh" ) { |
| 174 | + self._reloadElement(); |
| 175 | + } |
| 176 | + } |
145 | 177 | }
|
146 | 178 | });
|
147 | 179 | }
|
|
152 | 184 | container: '[data-rellax-container]'
|
153 | 185 | };
|
154 | 186 |
|
155 |
| - // $(document).ready(function() { |
| 187 | + $(document).ready(function() { |
156 | 188 | $('[data-rellax]').rellax();
|
157 |
| - // }); |
| 189 | + }); |
158 | 190 |
|
159 | 191 | })(jQuery, window, document);
|
0 commit comments