Skip to content

Commit beecf2c

Browse files
committed
Imported the code from http://www.backalleycoder.com/ and also inlined the needed CSS code for the sensors.
1 parent 9053c83 commit beecf2c

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

detect-element-resize.js

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* Detect Element Rezise
3+
*
4+
* https://github.com/sdecima/javascript-detect-element-resize
5+
* Sebastian Decima
6+
*
7+
* Based on the works of Back Alley Coder:
8+
* http://www.backalleycoder.com/2013/03/18/cross-browser-event-based-element-resize-detection/
9+
**/
10+
11+
function addFlowListener(element, type, fn){
12+
var flow = type == 'over';
13+
element.addEventListener('OverflowEvent' in window ? 'overflowchanged' : type + 'flow', function(e){
14+
if (e.type == (type + 'flow') ||
15+
((e.orient == 0 && e.horizontalOverflow == flow) ||
16+
(e.orient == 1 && e.verticalOverflow == flow) ||
17+
(e.orient == 2 && e.horizontalOverflow == flow && e.verticalOverflow == flow))) {
18+
e.flow = type;
19+
return fn.call(this, e);
20+
}
21+
}, false);
22+
};
23+
24+
function fireEvent(element, type, data, options){
25+
var options = options || {},
26+
event = document.createEvent('Event');
27+
event.initEvent(type, 'bubbles' in options ? options.bubbles : true, 'cancelable' in options ? options.cancelable : true);
28+
for (var z in data) event[z] = data[z];
29+
element.dispatchEvent(event);
30+
};
31+
32+
function addResizeListener(element, fn){
33+
var resize = 'onresize' in element;
34+
if (!resize && !element._resizeSensor) {
35+
var sensor_style = 'position: absolute; top: 0; left: 0; width: 100%; height: 100%; overflow: hidden; z-index: -1;';
36+
var sensor = element._resizeSensor = document.createElement('div');
37+
sensor.className = 'resize-sensor';
38+
//sensor.style = ;
39+
sensor.innerHTML = '<div class="resize-overflow" style="' + sensor_style
40+
+ '"><div></div></div><div class="resize-underflow" style="' + sensor_style
41+
+ '"><div></div></div>';
42+
43+
var x = 0, y = 0,
44+
first = sensor.firstElementChild.firstChild,
45+
last = sensor.lastElementChild.firstChild,
46+
matchFlow = function(event){
47+
var change = false,
48+
width = element.offsetWidth;
49+
if (x != width) {
50+
first.style.width = width - 1 + 'px';
51+
last.style.width = width + 1 + 'px';
52+
change = true;
53+
x = width;
54+
}
55+
var height = element.offsetHeight;
56+
if (y != height) {
57+
first.style.height = height - 1 + 'px';
58+
last.style.height = height + 1 + 'px';
59+
change = true;
60+
y = height;
61+
}
62+
if (change && event.currentTarget != element) fireEvent(element, 'resize');
63+
};
64+
65+
if (getComputedStyle(element).position == 'static'){
66+
element.style.position = 'relative';
67+
element._resizeSensor._resetPosition = true;
68+
}
69+
addFlowListener(sensor, 'over', matchFlow);
70+
addFlowListener(sensor, 'under', matchFlow);
71+
addFlowListener(sensor.firstElementChild, 'over', matchFlow);
72+
addFlowListener(sensor.lastElementChild, 'under', matchFlow);
73+
element.appendChild(sensor);
74+
matchFlow({});
75+
}
76+
var events = element._flowEvents || (element._flowEvents = []);
77+
if (events.indexOf(fn) == -1) events.push(fn);
78+
if (!resize) element.addEventListener('resize', fn, false);
79+
element.onresize = function(e){
80+
events.forEach(function(fn){
81+
fn.call(element, e);
82+
});
83+
};
84+
};
85+
86+
function removeResizeListener(element, fn){
87+
var index = element._flowEvents.indexOf(fn);
88+
if (index > -1) element._flowEvents.splice(index, 1);
89+
if (!element._flowEvents.length) {
90+
var sensor = element._resizeSensor;
91+
if (sensor) {
92+
element.removeChild(sensor);
93+
if (sensor._resetPosition) element.style.position = 'static';
94+
delete element._resizeSensor;
95+
}
96+
if ('onresize' in element) element.onresize = null;
97+
delete element._flowEvents;
98+
}
99+
element.removeEventListener('resize', fn);
100+
};

0 commit comments

Comments
 (0)