/
fixto.js
388 lines (313 loc) · 13.7 KB
/
fixto.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
var fixto = (function ($, window, document) {
// Start Computed Style. Please do not modify this module here. Modify it from its own repo. See address below.
/*! Computed Style - v0.1.0 - 2012-07-19
* https://github.com/bbarakaci/computed-style
* Copyright (c) 2012 Burak Barakaci; Licensed MIT */
var computedStyle = (function() {
var computedStyle = {
getAll : function(element){
return document.defaultView.getComputedStyle(element);
},
get : function(element, name){
return this.getAll(element)[name];
},
toFloat : function(value){
return parseFloat(value, 10) || 0;
},
getFloat : function(element,name){
return this.toFloat(this.get(element, name));
},
_getAllCurrentStyle : function(element) {
return element.currentStyle;
}
};
if (document.documentElement.currentStyle) {
computedStyle.getAll = computedStyle._getAllCurrentStyle;
}
return computedStyle;
}());
// End Computed Style. Modify whatever you want to.
var mimicNode = (function(){
/*
Class Mimic Node
Dependency : Computed Style
Tries to mimick a dom node taking his styles, dimensions. May go to his repo if gets mature.
*/
function MimicNode(element) {
this.element = element;
this.replacer = document.createElement('div');
this.replacer.style.visibility = 'hidden';
this.hide();
element.parentNode.insertBefore(this.replacer, element);
}
MimicNode.prototype = {
replace : function(){
var rst = this.replacer.style;
var styles = computedStyle.getAll(this.element);
// rst.width = computedStyle.width(this.element) + 'px';
// rst.height = this.element.offsetHeight + 'px';
// Setting offsetWidth
rst.width = this._width();
rst.height = this._height();
// Adobt margins
rst.marginTop = styles.marginTop;
rst.marginBottom = styles.marginBottom;
rst.marginLeft = styles.marginLeft;
rst.marginRight = styles.marginRight;
// Adopt positioning
rst.cssFloat = styles.cssFloat;
rst.styleFloat = styles.styleFloat; //ie8;
rst.position = styles.position;
rst.top = styles.top;
rst.right = styles.right;
rst.bottom = styles.bottom;
rst.left = styles.left;
// rst.borderStyle = styles.borderStyle;
rst.display = styles.display;
},
hide: function () {
this.replacer.style.display = 'none';
},
_width : function(){
return this.element.getBoundingClientRect().width + 'px';
},
_widthOffset : function(){
return this.element.offsetWidth + 'px';
},
_height : function(){
return this.element.getBoundingClientRect().height + 'px';
},
_heightOffset : function(){
return this.element.offsetHeight + 'px';
},
destroy: function () {
$(this.replacer).remove();
// set properties to null to break references
for (var prop in this) {
if (this.hasOwnProperty(prop)) {
this[prop] = null;
}
}
}
};
var bcr = document.documentElement.getBoundingClientRect();
if(!bcr.width){
MimicNode.prototype._width = MimicNode.prototype._widthOffset;
MimicNode.prototype._height = MimicNode.prototype._heightOffset;
}
return {
MimicNode:MimicNode,
computedStyle:computedStyle
};
}());
// Class FixToContainer
function FixToContainer(child, parent, options) {
this.child = child;
this._$child = $(child);
this.parent = parent;
this._replacer = new mimicNode.MimicNode(child);
this._ghostNode = this._replacer.replacer;
this.options = $.extend({
className: 'fixto-fixed'
}, options);
if(this.options.mind) {
this._$mind = $(this.options.mind);
}
if(this.options.zIndex) {
this.child.style.zIndex = this.options.zIndex;
}
this._saveStyles();
this._saveViewportHeight();
// Create anonymous functions and keep references to register and unregister events.
this._proxied_onscroll = this._bind(this._onscroll, this);
this._proxied_onresize = this._bind(this._onresize, this);
this.start();
}
FixToContainer.prototype = {
// Returns an anonymous function that will call the given function in the given context
_bind : function (fn, context) {
return function () {
return fn.call(context);
};
},
// at ie8 maybe only in vm window resize event fires everytime an element is resized.
_toresize : $.browser.msie && $.browser.version === '8.0' ? document.documentElement : window,
// Returns the total outerHeight of the elements passed to mind option. Will return 0 if none.
_mindtop: function () {
var top = 0;
if(this._$mind) {
$(this._$mind).each(function () {
top += $(this).outerHeight(true);
});
}
return top;
},
_onscroll: function _onscroll() {
this._scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
this._parentBottom = (this.parent.offsetHeight + this._fullOffset('offsetTop', this.parent)) - computedStyle.getFloat(this.parent, 'paddingBottom');
if (!this.fixed) {
if (
this._scrollTop < this._parentBottom &&
this._scrollTop > (this._fullOffset('offsetTop', this.child) - computedStyle.getFloat(this.child, 'marginTop') - this._mindtop()) &&
this._viewportHeight > (this.child.offsetHeight + computedStyle.getFloat(this.child, 'marginTop') + computedStyle.getFloat(this.child, 'marginBottom'))
) {
this._fix();
this._adjust();
}
} else {
if (this._scrollTop > this._parentBottom || this._scrollTop < (this._fullOffset('offsetTop', this._ghostNode) - computedStyle.getFloat(this._ghostNode, 'marginTop') - this._mindtop())) {
this._unfix();
return;
}
this._adjust();
}
},
_adjust: function _adjust() {
var diff = (this._parentBottom - this._scrollTop) - (this.child.offsetHeight + computedStyle.getFloat(this.child, 'marginTop') + computedStyle.getFloat(this.child, 'marginBottom') + this._mindtop());
var mindTop = this._mindtop();
if (diff < 0) {
this.child.style.top = (diff + mindTop) + 'px';
}
else {
this.child.style.top = (0 + mindTop) + 'px';
}
},
/*
I need some performance on calculating offset as it is called a lot of times.
This function calculates cumulative offsetTop way faster than jQuery $.offset
todo: check vs getBoundingClientRect
*/
_fullOffset: function _fullOffset(offsetName, elm) {
var offset = elm[offsetName];
while (elm.offsetParent !== null) {
elm = elm.offsetParent;
offset = offset + elm[offsetName];
}
return offset;
},
_fix: function _fix() {
var child = this.child,
childStyle = child.style;
this._saveStyles();
if(document.documentElement.currentStyle){
var styles = computedStyle.getAll(child);
childStyle.left = (this._fullOffset('offsetLeft', child) - computedStyle.getFloat(child, 'marginLeft')) + 'px';
// Function for ie<9. when hasLayout is not triggered in ie7, he will report currentStyle as auto, clientWidth as 0. Thus using offsetWidth.
childStyle.width = (child.offsetWidth) - (computedStyle.toFloat(styles.paddingLeft) + computedStyle.toFloat(styles.paddingRight) + computedStyle.toFloat(styles.borderLeftWidth) + computedStyle.toFloat(styles.borderRightWidth)) + 'px';
}
else {
childStyle.width = computedStyle.get(child, 'width');
childStyle.left = (child.getBoundingClientRect().left - computedStyle.getFloat(child, 'marginLeft')) + 'px';
}
this._replacer.replace();
childStyle.position = 'fixed';
childStyle.top = this._mindtop() + 'px';
this._$child.addClass(this.options.className);
this.fixed = true;
},
_unfix: function _unfix() {
var childStyle = this.child.style;
this._replacer.hide();
childStyle.position = this._childOriginalPosition;
childStyle.top = this._childOriginalTop;
childStyle.width = this._childOriginalWidth;
childStyle.left = this._childOriginalLeft;
this._$child.removeClass(this.options.className);
this.fixed = false;
},
_saveStyles: function(){
var childStyle = this.child.style;
this._childOriginalPosition = childStyle.position;
this._childOriginalTop = childStyle.top;
this._childOriginalWidth = childStyle.width;
this._childOriginalLeft = childStyle.left;
},
_onresize: function () {
this._saveViewportHeight();
this._unfix();
this._onscroll();
},
_saveViewportHeight: function () {
// ie8 doesn't support innerHeight
this._viewportHeight = window.innerHeight || document.documentElement.clientHeight;
},
// Public method to stop the behaviour of this instance.
stop: function () {
// Unfix the container immediately.
this._unfix();
// remove event listeners
$(window).unbind('scroll', this._proxied_onscroll);
$(this._toresize).unbind('resize', this._proxied_onresize);
this._running = false;
},
// Public method starts the behaviour of this instance.
start: function () {
// Start only if it is not running not to attach event listeners multiple times.
if(!this._running) {
// Trigger onscroll to have the effect immediately.
this._onscroll();
// Attach event listeners
$(window).bind('scroll', this._proxied_onscroll);
$(this._toresize).bind('resize', this._proxied_onresize);
this._running = true;
}
},
//Public method to destroy fixto behaviour
destroy: function () {
this.stop();
// Remove jquery data from the element
this._$child.removeData('fixto-instance');
// Destroy mimic node instance
this._replacer.destroy();
// set properties to null to break references
for (var prop in this) {
if (this.hasOwnProperty(prop)) {
this[prop] = null;
}
}
}
};
var fixTo = function fixTo(childElement, parentElement, options) {
return new FixToContainer(childElement, parentElement, options);
};
/*
No support for touch devices and ie lt 8
*/
var touch = !!('ontouchstart' in window);
var ielt8 = $.browser.msie && $.browser.version < 8;
if(touch || ielt8){
fixTo = function(){
return 'not supported';
};
}
// Let it be a jQuery Plugin
$.fn.fixTo = function (targetSelector, options) {
var $targets = $(targetSelector);
var i = 0;
return this.each(function () {
// Check the data of the element.
var instance = $(this).data('fixto-instance');
// If the element is not bound to an instance, create the instance and save it to elements data.
if(!instance) {
$(this).data('fixto-instance', fixTo(this, $targets[i], options));
}
else {
// If we already have the instance here, expect that targetSelector parameter will be a string
// equal to a public methods name. Run the method on the instance without checking if
// it exists or it is a public method or not. Cause nasty errors when necessary.
var method = targetSelector;
instance[method].call(instance);
}
i++;
});
};
/*
Expose
*/
return {
FixToContainer: FixToContainer,
fixTo: fixTo,
computedStyle:computedStyle,
mimicNode:mimicNode
};
}(window.jQuery, window, document));