Skip to content

Commit 7899202

Browse files
committed
Adding jQuery plugin
1 parent beecf2c commit 7899202

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

jquery.resize.js

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/**
2+
* Detect Element Rezise Plugin for jQuery
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 ( $ ) {
12+
$.fn.resize = function(callback) {
13+
return this.each(function() {
14+
addResizeListener(this, callback);
15+
});
16+
}
17+
18+
$.fn.flow = function(type, callback) {
19+
return this.each(function() {
20+
addFlowListener(this, type, callback);
21+
});
22+
}
23+
24+
function addFlowListener(element, type, fn){
25+
var flow = type == 'over';
26+
element.addEventListener('OverflowEvent' in window ? 'overflowchanged' : type + 'flow', function(e){
27+
if (e.type == (type + 'flow') ||
28+
((e.orient == 0 && e.horizontalOverflow == flow) ||
29+
(e.orient == 1 && e.verticalOverflow == flow) ||
30+
(e.orient == 2 && e.horizontalOverflow == flow && e.verticalOverflow == flow))) {
31+
e.flow = type;
32+
return fn.call(this, e);
33+
}
34+
}, false);
35+
};
36+
37+
function fireEvent(element, type, data, options){
38+
var options = options || {},
39+
event = document.createEvent('Event');
40+
event.initEvent(type, 'bubbles' in options ? options.bubbles : true, 'cancelable' in options ? options.cancelable : true);
41+
for (var z in data) event[z] = data[z];
42+
element.dispatchEvent(event);
43+
};
44+
45+
function addResizeListener(element, fn){
46+
var resize = 'onresize' in element;
47+
if (!resize && !element._resizeSensor) {
48+
var sensor_style = 'position: absolute; top: 0; left: 0; width: 100%; height: 100%; overflow: hidden; z-index: -1;';
49+
var sensor = element._resizeSensor = document.createElement('div');
50+
sensor.className = 'resize-sensor';
51+
//sensor.style = ;
52+
sensor.innerHTML = '<div class="resize-overflow" style="' + sensor_style
53+
+ '"><div></div></div><div class="resize-underflow" style="' + sensor_style
54+
+ '"><div></div></div>';
55+
56+
var x = 0, y = 0,
57+
first = sensor.firstElementChild.firstChild,
58+
last = sensor.lastElementChild.firstChild,
59+
matchFlow = function(event){
60+
var change = false,
61+
width = element.offsetWidth;
62+
if (x != width) {
63+
first.style.width = width - 1 + 'px';
64+
last.style.width = width + 1 + 'px';
65+
change = true;
66+
x = width;
67+
}
68+
var height = element.offsetHeight;
69+
if (y != height) {
70+
first.style.height = height - 1 + 'px';
71+
last.style.height = height + 1 + 'px';
72+
change = true;
73+
y = height;
74+
}
75+
if (change && event.currentTarget != element) fireEvent(element, 'resize');
76+
};
77+
78+
if (getComputedStyle(element).position == 'static'){
79+
element.style.position = 'relative';
80+
element._resizeSensor._resetPosition = true;
81+
}
82+
addFlowListener(sensor, 'over', matchFlow);
83+
addFlowListener(sensor, 'under', matchFlow);
84+
addFlowListener(sensor.firstElementChild, 'over', matchFlow);
85+
addFlowListener(sensor.lastElementChild, 'under', matchFlow);
86+
element.appendChild(sensor);
87+
matchFlow({});
88+
}
89+
var events = element._flowEvents || (element._flowEvents = []);
90+
if (events.indexOf(fn) == -1) events.push(fn);
91+
if (!resize) element.addEventListener('resize', fn, false);
92+
element.onresize = function(e){
93+
events.forEach(function(fn){
94+
fn.call(element, e);
95+
});
96+
};
97+
};
98+
99+
function removeResizeListener(element, fn){
100+
var index = element._flowEvents.indexOf(fn);
101+
if (index > -1) element._flowEvents.splice(index, 1);
102+
if (!element._flowEvents.length) {
103+
var sensor = element._resizeSensor;
104+
if (sensor) {
105+
element.removeChild(sensor);
106+
if (sensor._resetPosition) element.style.position = 'static';
107+
delete element._resizeSensor;
108+
}
109+
if ('onresize' in element) element.onresize = null;
110+
delete element._flowEvents;
111+
}
112+
element.removeEventListener('resize', fn);
113+
};
114+
115+
}( jQuery ));

0 commit comments

Comments
 (0)