Skip to content

Commit 149710a

Browse files
committed
Add _scaleElement method
1 parent 39784e7 commit 149710a

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

jquery.rellax.js

+41-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* jQuery Rellax Plugin v0.1
2+
* jQuery Rellax Plugin v0.2.0
33
* Examples and documentation at http://pixelgrade.github.io/rellax/
44
* Copyright (c) 2016 PixelGrade http://www.pixelgrade.com
55
* Licensed under MIT http://www.opensource.org/licenses/mit-license.php/
@@ -9,8 +9,9 @@
99
if (!window.requestAnimationFrame) return;
1010

1111
var $window = $(window),
12+
windowWidth = $window.width(),
1213
windowHeight = $window.height(),
13-
lastKnownScrollY = $window.scrollTop();
14+
lastKnownScrollY;
1415

1516
var elements = new Array();
1617

@@ -21,11 +22,12 @@
2122
window.requestAnimationFrame(updateAll);
2223
})();
2324

24-
$window.on('resize', function(e) {
25+
$window.on('load resize', function(e) {
26+
windowWidth = $window.width();
2527
windowHeight = $window.height();
2628
});
2729

28-
$window.on('scroll', function(e) {
30+
$window.on('load scroll', function(e) {
2931
lastKnownScrollY = $(e.target).scrollTop();
3032
});
3133

@@ -50,7 +52,6 @@
5052
self._bindEvents();
5153

5254
elements.push(self);
53-
$el.addClass('rellax-active', true);
5455
}
5556

5657
$.extend(Rellax.prototype, {
@@ -61,6 +62,25 @@
6162
self._reloadElement();
6263
});
6364
},
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+
},
6484
_reloadElement: function() {
6585
var self = this,
6686
$el = $(this.element).removeAttr('style');
@@ -73,6 +93,10 @@
7393
self.height = $el.height();
7494
self.offset = $el.offset();
7595

96+
if ( self.$parent.length ) {
97+
self._scaleElement();
98+
}
99+
76100
if ( self.isContainer ) {
77101
self.width += 2 * self.options.bleed;
78102
self.height += 2 * self.options.bleed;
@@ -87,11 +111,11 @@
87111
width: self.width,
88112
height: self.height,
89113
marginTop: 0,
90-
marginLeft: 0,
91-
transform: 'none'
114+
marginLeft: 0
92115
}
93116

94117
if (self.isContainer) $.extend(style, {zIndex: -1});
118+
95119
if (self.$parent.length) {
96120

97121
$.extend(style, {
@@ -103,6 +127,7 @@
103127
overflow: 'hidden',
104128
zIndex: -1
105129
});
130+
106131
}
107132

108133
$el.css(style);
@@ -142,6 +167,13 @@
142167
return this.each(function () {
143168
if ( ! $.data(this, "plugin_" + Rellax) ) {
144169
$.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+
}
145177
}
146178
});
147179
}
@@ -152,8 +184,8 @@
152184
container: '[data-rellax-container]'
153185
};
154186

155-
// $(document).ready(function() {
187+
$(document).ready(function() {
156188
$('[data-rellax]').rellax();
157-
// });
189+
});
158190

159191
})(jQuery, window, document);

jquery.rellax.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)