-
Notifications
You must be signed in to change notification settings - Fork 146
/
jquery.scrolly.js
87 lines (74 loc) · 2.22 KB
/
jquery.scrolly.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
84
85
86
87
/*
* Project: Scrolly : parallax is easy as a matter of fact !
* Description: Based on jQuery boilerplate
* Author: Victor C. / Octave & Octave web agency
* Licence: MIT
*/
!function(root, factory) {
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
} else {
factory(root.jQuery);
}
}(this, function($) {
'use strict';
// Create the defaults once
var pluginName = 'scrolly',
defaults = {
bgParallax: false
},
didScroll = false;
function Plugin( element, options ) {
this.element = element;
this.$element = $(this.element);
this.options = $.extend( {}, defaults, options) ;
this._defaults = defaults;
this._name = pluginName;
this.init();
}
Plugin.prototype.init = function () {
var self = this;
this.startPosition = this.$element.position().top;
this.offsetTop = this.$element.offset().top;
this.height = this.$element.outerHeight(true);
this.velocity = this.$element.attr('data-velocity');
this.bgStart = parseInt(this.$element.attr('data-fit'), 10);
$(document).scroll(function(){
self.didScroll = true;
});
setInterval(function() {
if (self.didScroll) {
self.didScroll = false;
self.scrolly();
}
}, 10);
};
Plugin.prototype.scrolly = function() {
var dT = $(window).scrollTop(),
wH = $(window).height(),
position = this.startPosition;
if(this.offsetTop >= (dT+wH)) {
this.$element.addClass('scrolly-invisible');
} else {
if(this.$element.hasClass('scrolly-invisible')){
position = this.startPosition + (dT + ( wH - this.offsetTop ) ) * this.velocity;
} else {
position = this.startPosition + dT * this.velocity;
}
}
// Fix background position
if(this.bgStart){ position = position + this.bgStart; }
if(this.options.bgParallax === true) {
this.$element.css({backgroundPosition: '50% '+position+'px'});
} else {
this.$element.css({top: position});
}
};
$.fn[pluginName] = function ( options ) {
return this.each(function () {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName, new Plugin( this, options ));
}
});
};
});