/
jquery-xallarap.js
72 lines (56 loc) · 1.76 KB
/
jquery-xallarap.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
/*!
* jQuery Xallarap 0.1.1
*
* @dependency jQuery 1.7+
* @author Uniba Inc.
* @license MIT License
*/
(function(window, document, $) {
var $window = $(window)
, scrollTop = 0
, windowHeight = 0;
$.fn.xallarap = function(options, callback) {
var options = options || {}
, callback = options.callback || callback || defaultCallback;
return this.each(function() {
var $this = $(this);
/**
* Store original position and offset.
*/
$this
.data('xap-position', $this.position())
.data('xap-offset', $this.offset())
.data('xap-easing', $this.data('xap-easing') || options.easing || 'swing')
.data('xap-duration', $this.data('xap-duration') || options.duration || 400)
.data('xap-value', $this.data('xap-value') || options.value || 400);
/**
* Scroll handler.
*/
$window.on('scroll', function(e) {
var position = $this.data('xap-position')
, offset = $this.data('xap-offset')
, value = $this.data('xap-value')
, percent = (scrollTop - offset.top - $this.height() / 2 + windowHeight / 2) / offset.top;
callback.call($this, position, offset, value, percent);
});
});
};
/**
* Cache window states.
*/
$(function() {
$(window).on('resize scroll', function(e) {
scrollTop = $window.scrollTop();
windowHeight = $window.height();
}).trigger('resize');
});
/**
* Default callback function.
*/
function defaultCallback(position, offset, value, percent) {
var $this = $(this);
$this
.stop()
.animate({ top: position.top - (value * percent) }, $this.data('xap-duration'), $this.data('xap-easing'));
}
}).call(this, window, document, jQuery);