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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
// animate
( function( window, factory ) {
// universal module definition
/* jshint strict: false */
if ( typeof define == 'function' && define.amd ) {
// AMD
define( [
'fizzy-ui-utils/utils'
], function( utils ) {
return factory( window, utils );
});
} else if ( typeof module == 'object' && module.exports ) {
// CommonJS
module.exports = factory(
window,
require('fizzy-ui-utils')
);
} else {
// browser global
window.Flickity = window.Flickity || {};
window.Flickity.animatePrototype = factory(
window,
window.fizzyUIUtils
);
}
}( window, function factory( window, utils ) {
'use strict';
// -------------------------- requestAnimationFrame -------------------------- //
// get rAF, prefixed, if present
var requestAnimationFrame = window.requestAnimationFrame ||
window.webkitRequestAnimationFrame;
// fallback to setTimeout
var lastTime = 0;
if ( !requestAnimationFrame ) {
requestAnimationFrame = function( callback ) {
var currTime = new Date().getTime();
var timeToCall = Math.max( 0, 16 - ( currTime - lastTime ) );
var id = setTimeout( callback, timeToCall );
lastTime = currTime + timeToCall;
return id;
};
}
// -------------------------- animate -------------------------- //
var proto = {};
proto.startAnimation = function() {
if ( this.isAnimating ) {
return;
}
this.isAnimating = true;
this.restingFrames = 0;
this.animate();
};
proto.animate = function() {
this.applyDragForce();
this.applySelectedAttraction();
var previousX = this.x;
this.integratePhysics();
this.positionSlider();
this.settle( previousX );
// animate next frame
if ( this.isAnimating ) {
var _this = this;
requestAnimationFrame( function animateFrame() {
_this.animate();
});
}
};
var transformProperty = ( function () {
var style = document.documentElement.style;
if ( typeof style.transform == 'string' ) {
return 'transform';
}
return 'WebkitTransform';
})();
proto.positionSlider = function() {
var x = this.x;
// wrap position around
if ( this.options.wrapAround && this.cells.length > 1 ) {
x = utils.modulo( x, this.slideableWidth );
x = x - this.slideableWidth;
this.shiftWrapCells( x );
}
x = x + this.cursorPosition;
// reverse if right-to-left and using transform
x = this.options.rightToLeft && transformProperty ? -x : x;
var value = this.getPositionValue( x );
// use 3D tranforms for hardware acceleration on iOS
// but use 2D when settled, for better font-rendering
this.slider.style[ transformProperty ] = this.isAnimating ?
'translate3d(' + value + ',0,0)' : 'translateX(' + value + ')';
// scroll event
var firstSlide = this.slides[0];
if ( firstSlide ) {
var positionX = -this.x - firstSlide.target;
var progress = positionX / this.slidesWidth;
this.dispatchEvent( 'scroll', null, [ progress, positionX ] );
}
};
proto.positionSliderAtSelected = function() {
if ( !this.cells.length ) {
return;
}
this.x = -this.selectedSlide.target;
this.positionSlider();
};
proto.getPositionValue = function( position ) {
if ( this.options.percentPosition ) {
// percent position, round to 2 digits, like 12.34%
return ( Math.round( ( position / this.size.innerWidth ) * 10000 ) * 0.01 )+ '%';
} else {
// pixel positioning
return Math.round( position ) + 'px';
}
};
proto.settle = function( previousX ) {
// keep track of frames where x hasn't moved
if ( !this.isPointerDown && Math.round( this.x * 100 ) == Math.round( previousX * 100 ) ) {
this.restingFrames++;
}
// stop animating if resting for 3 or more frames
if ( this.restingFrames > 2 ) {
this.isAnimating = false;
delete this.isFreeScrolling;
// render position with translateX when settled
this.positionSlider();
this.dispatchEvent('settle');
}
};
proto.shiftWrapCells = function( x ) {
// shift before cells
var beforeGap = this.cursorPosition + x;
this._shiftCells( this.beforeShiftCells, beforeGap, -1 );
// shift after cells
var afterGap = this.size.innerWidth - ( x + this.slideableWidth + this.cursorPosition );
this._shiftCells( this.afterShiftCells, afterGap, 1 );
};
proto._shiftCells = function( cells, gap, shift ) {
for ( var i=0; i < cells.length; i++ ) {
var cell = cells[i];
var cellShift = gap > 0 ? shift : 0;
cell.wrapShift( cellShift );
gap -= cell.size.outerWidth;
}
};
proto._unshiftCells = function( cells ) {
if ( !cells || !cells.length ) {
return;
}
for ( var i=0; i < cells.length; i++ ) {
cells[i].wrapShift( 0 );
}
};
// -------------------------- physics -------------------------- //
proto.integratePhysics = function() {
this.x += this.velocity;
this.velocity *= this.getFrictionFactor();
};
proto.applyForce = function( force ) {
this.velocity += force;
};
proto.getFrictionFactor = function() {
return 1 - this.options[ this.isFreeScrolling ? 'freeScrollFriction' : 'friction' ];
};
proto.getRestingPosition = function() {
// my thanks to Steven Wittens, who simplified this math greatly
return this.x + this.velocity / ( 1 - this.getFrictionFactor() );
};
proto.applyDragForce = function() {
if ( !this.isPointerDown ) {
return;
}
// change the position to drag position by applying force
var dragVelocity = this.dragX - this.x;
var dragForce = dragVelocity - this.velocity;
this.applyForce( dragForce );
};
proto.applySelectedAttraction = function() {
// do not attract if pointer down or no cells
if ( this.isPointerDown || this.isFreeScrolling || !this.cells.length ) {
return;
}
var distance = this.selectedSlide.target * -1 - this.x;
var force = distance * this.options.selectedAttraction;
this.applyForce( force );
};
return proto;
}));
|