This repository has been archived by the owner on Jul 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.parallax.thefarm.js
83 lines (80 loc) · 3.03 KB
/
jquery.parallax.thefarm.js
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
/**
* Parallax of unknown origin.
* @version 0.1.12
* @author Albin Jacobsson from The Farm
*/
(function($) {
/**
* Plugin for changing the background-position of elements related to the users
* scrolling. If if finds a way to animate the background-position it will use
* .animate() over .css()
*
* Avalible options are:
* start {function} A functions that returns a boolean which represents
* if the parallax should be going or not. First
* parameter is the element and the second is a jQuery
* $.fn.offset object. Both start and stop needs to
* return true for the paralax to animate.
* stop {function} A functions that returns a boolean which represents
* if the parallax should be going or not. First
* parameter is the element and the second is a jQuery
* $.fn.offset object. Both start and stop needs to
* return true for the paralax to animate.
* xCoeff {number} The multiplyer for the horizontal parallax
* yCoeff {number} The multiplyer for the vertical parallax effect
* xOffset {number} The horizontal offset (in pixels without unit)
* yOffset {number} The vertical offset (in pixels without unit)
* xUnit {string} The unit that should be used when setting the
* horizontal background position
* yUnit {string} The unit that should be used when setting the
* vertical background position
*
* @param {object} options An object containing options
* @return {jQuery} Used for chaining
*/
$.fn.parallax = function(options) {
if (this.length === 0) {
return this;
}
var defaults = {
"start": function(el, elOffset) {
return $(window).scrollTop() >= elOffset.top - $(window).height();
},
"stop": function(el, elOffset) {
return $(window).scrollTop() <= elOffset.top + $(el).height();
},
"xCoeff": 0,
"yCoeff": 2,
"xOffset": 50,
"yOffset": 0,
"xUnit": '%',
"yUnit": 'px',
"xMinValue": false,
"yMinValue": false
};
var opts = $.extend(defaults, options);
return this.each(function(i, el) {
$(window).on('scroll.parallax resize.parallax', function(ev) {
var $el = $(el);
var elOffset = $el.offset();
if (opts.start(el, elOffset) && opts.stop(el, elOffset)) {
var windowTop = $(window).scrollTop();
var newYCoord = ((elOffset.top - windowTop) * opts.yCoeff) + opts.yOffset;
var newXCoord = ((elOffset.top - windowTop) * opts.xCoeff) + opts.xOffset;
var newXVal = (opts.xMinValue !== false && opts.xMinValue > newXCoord) ? opts.xMinValue : newXCoord;
var newYVal = (opts.yMinValue !== false && opts.yMinValue > newYCoord) ? opts.yMinValue : newYCoord;
if (canAnimateBackgroundPosition) {
$el.stop().animate({
"background-position": newXVal + opts.xUnit + " " + newYVal + opts.yUnit
}, 250, 'easeOutCirc');
} else {
$el.css({
"background-position": newXVal + opts.xUnit + " " + newYVal + opts.yUnit
});
}
}
});
});
};
var canAnimateBackgroundPosition = $.isFunction($.fx.step.backgroundPosition);
}(jQuery));