forked from lojjic/PIE
/
BoundsInfo.js
60 lines (51 loc) · 1.73 KB
/
BoundsInfo.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
/**
* Handles calculating, caching, and detecting changes to size and position of the element.
* @constructor
* @param {Element} el the target element
*/
PIE.BoundsInfo = function( el ) {
this.targetElement = el;
};
PIE.BoundsInfo.prototype = {
_locked: 0,
positionChanged: function() {
var last = this._lastBounds,
bounds;
return !last || ( ( bounds = this.getBounds() ) && ( last.x !== bounds.x || last.y !== bounds.y ) );
},
sizeChanged: function() {
var last = this._lastBounds,
bounds;
return !last || ( ( bounds = this.getBounds() ) && ( last.w !== bounds.w || last.h !== bounds.h ) );
},
getLiveBounds: function() {
var el = this.targetElement,
rect = el.getBoundingClientRect(),
isIE9 = PIE.ieDocMode === 9;
return {
x: rect.left,
y: rect.top,
// In some cases scrolling the page will cause IE9 to report incorrect dimensions
// in the rect returned by getBoundingClientRect, so we must query offsetWidth/Height instead
w: isIE9 ? el.offsetWidth : rect.right - rect.left,
h: isIE9 ? el.offsetHeight : rect.bottom - rect.top
};
},
getBounds: function() {
return this._locked ?
( this._lockedBounds || ( this._lockedBounds = this.getLiveBounds() ) ) :
this.getLiveBounds();
},
hasBeenQueried: function() {
return !!this._lastBounds;
},
lock: function() {
++this._locked;
},
unlock: function() {
if( !--this._locked ) {
if( this._lockedBounds ) this._lastBounds = this._lockedBounds;
this._lockedBounds = null;
}
}
};