Skip to content

Commit 072a3fc

Browse files
el-fuegomarcj
authored andcommitted
Fix subpixel sizes support (marcj#204)
1 parent fdb851e commit 072a3fc

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

src/ElementQueries.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,26 @@
3838
return parseFloat(fontSize) || 16;
3939
}
4040

41+
/**
42+
* Get element size
43+
* @param {HTMLElement} element
44+
* @returns {Object} {width, height}
45+
*/
46+
function getElementSize(element) {
47+
if (!element.getBoundingClientRect) {
48+
return {
49+
width: element.offsetWidth,
50+
height: element.offsetHeight
51+
}
52+
}
53+
54+
var rect = element.getBoundingClientRect();
55+
return {
56+
width: Math.round(rect.width),
57+
height: Math.round(rect.height)
58+
}
59+
}
60+
4161
/**
4262
*
4363
* @copyright https://github.com/Mr0grog/element-query/blob/master/LICENSE
@@ -85,7 +105,7 @@
85105
function SetupInformation(element) {
86106
this.element = element;
87107
this.options = {};
88-
var key, option, width = 0, height = 0, value, actualValue, attrValues, attrValue, attrName;
108+
var key, option, elementSize, value, actualValue, attrValues, attrValue, attrName;
89109

90110
/**
91111
* @param {Object} option {mode: 'min|max', property: 'width|height', value: '123px'}
@@ -102,8 +122,7 @@
102122
*/
103123
this.call = function() {
104124
// extract current dimensions
105-
width = this.element.offsetWidth;
106-
height = this.element.offsetHeight;
125+
elementSize = getElementSize(this.element);
107126

108127
attrValues = {};
109128

@@ -115,7 +134,7 @@
115134

116135
value = convertToPx(this.element, option.value);
117136

118-
actualValue = option.property == 'width' ? width : height;
137+
actualValue = option.property == 'width' ? elementSize.width : elementSize.height;
119138
attrName = option.mode + '-' + option.property;
120139
attrValue = '';
121140

src/ResizeSensor.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,26 @@
5353
}
5454
}
5555

56+
/**
57+
* Get element size
58+
* @param {HTMLElement} element
59+
* @returns {Object} {width, height}
60+
*/
61+
function getElementSize(element) {
62+
if (!element.getBoundingClientRect) {
63+
return {
64+
width: element.offsetWidth,
65+
height: element.offsetHeight
66+
}
67+
}
68+
69+
var rect = element.getBoundingClientRect();
70+
return {
71+
width: Math.round(rect.width),
72+
height: Math.round(rect.height)
73+
}
74+
}
75+
5676
/**
5777
* Class for dimension change detection.
5878
*
@@ -131,8 +151,9 @@
131151
var expandChild = expand.childNodes[0];
132152
var shrink = element.resizeSensor.childNodes[1];
133153
var dirty, rafId, newWidth, newHeight;
134-
var lastWidth = element.offsetWidth;
135-
var lastHeight = element.offsetHeight;
154+
var size = getElementSize(element);
155+
var lastWidth = size.width;
156+
var lastHeight = size.height;
136157

137158
var reset = function() {
138159
expandChild.style.width = '100000px';
@@ -161,8 +182,9 @@
161182
};
162183

163184
var onScroll = function() {
164-
newWidth = element.offsetWidth;
165-
newHeight = element.offsetHeight;
185+
var size = getElementSize(element);
186+
var newWidth = size.width;
187+
var newHeight = size.height;
166188
dirty = newWidth != lastWidth || newHeight != lastHeight;
167189

168190
if (dirty && !rafId) {
@@ -195,7 +217,7 @@
195217

196218
ResizeSensor.detach = function(element, ev) {
197219
forEachElement(element, function(elem){
198-
if (!elem) return
220+
if (!elem) return;
199221
if(elem.resizedAttached && typeof ev == "function"){
200222
elem.resizedAttached.remove(ev);
201223
if(elem.resizedAttached.length()) return;

0 commit comments

Comments
 (0)