-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.scrollAnimate.js
164 lines (99 loc) · 3.86 KB
/
jquery.scrollAnimate.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
(function($) {
$.fn.scrollAnimate = function(params) {
params = $.extend({ startFromElement: false, scrollType: 'vertical', startScroll: 0, endScroll: 0, cssProperty: '', before: 0, after: 0 }, params);
var scrollRange = params.endScroll - params.startScroll;
var element = $(this);
// create objects literal and assign the variable property for before and after
var cssArgsBefore = {};
cssArgsBefore[params.cssProperty] = params.before;
var cssArgsAfter = {};
cssArgsAfter[params.cssProperty] = params.after;
// setup startFromElement
if(params.startFromElement) {
startingElement = $(params.startFromElement);
startingElementOffset = startingElement.offset();
startingElementOffsetTop = startingElementOffset.top;
startingElementOffsetLeft = startingElementOffset.left;
var windowWidth = $(window).width();
var windowHeight = $(window).height();
var scrollFromTop = startingElementOffsetTop - windowHeight;
var scrollFromLeft = startingElementOffsetLeft - windowWidth;
$(window).bind('resize', function() {
windowWidth = $(window).width();
windowHeight = $(window).height();
scrollFromTop = startingElementOffsetTop - windowHeight;
scrollFromLeft = startingElementOffsetLeft - windowWidth;
});
}
// setup css3 transform
if(params.cssProperty == 'transform') {
// set css3 transform webkit and moz fallbacks
cssArgsBefore['-webkit-transform'] = params.before;
cssArgsAfter['-webkit-transform'] = params.after;
cssArgsBefore['-moz-transform'] = params.before;
cssArgsAfter['-moz-transform'] = params.after;
// get int from css3 transform rotate and skew
if(params.before.indexOf('deg') != -1) {
var before = params.before.split('(');
before = before[1].split('deg');
before = parseFloat(before[0]);
var after = params.after.split('(');
after = after[1].split('deg');
after = parseFloat(after[0]);
} else
// get int from css3 transform scale
if(params.before.indexOf('scale') != -1) {
var before = params.before.split('(');
before = before[1].split(')');
before = parseFloat(before[0]);
var after = params.after.split('(');
after = after[1].split(')');
after = parseFloat(after[0]);
}
}
var currentCss = {};
this.each(function() {
scrollAnimate();
$(window).bind('scroll', function() {
scrollAnimate();
});
function scrollAnimate() {
if(params.scrollType == 'vertical') {
var scroll = $(window).scrollTop();
if(params.startFromElement) {
scroll = scroll - scrollFromTop;
}
} else if(params.scrollType == 'horizontal') {
var scroll = $(window).scrollLeft();
if(params.startFromElement) {
scroll = scroll - scrollFromLeft;
}
}
var scrollPercentage = (scroll - params.startScroll) / scrollRange;
if(scroll < params.startScroll) {
element.css(cssArgsBefore);
} else if(scroll > params.endScroll) {
element.css(cssArgsAfter);
} else {
if(params.cssProperty == 'transform') {
var currentTransformValue = before + (after - before) * scrollPercentage;
if(params.before.indexOf('rotate') != -1)
var currentTransform = 'rotate(' + currentTransformValue + 'deg)';
else if(params.before.indexOf('skew') != -1)
var currentTransform = 'skew(' + currentTransformValue + 'deg)';
else if(params.before.indexOf('scale') != -1)
var currentTransform = 'scale(' + currentTransformValue + ')';
currentCss[params.cssProperty] = currentTransform;
currentCss['-moz-transform'] = currentTransform;
currentCss['-webkit-transform'] = currentTransform;
element.css(currentCss);
} else {
currentCss[params.cssProperty] = params.before + (params.after - params.before) * scrollPercentage;
element.css(currentCss);
}
}
}
});
return this;
};
})(jQuery);