Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions src/ElementQueries.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@
return parseFloat(fontSize) || 16;
}

/**
* Get element size
* @param {HTMLElement} element
* @returns {Object} {width, height}
*/
function getElementSize(element) {
if (!element.getBoundingClientRect) {
return {
width: element.offsetWidth,
height: element.offsetHeight
}
}

var rect = element.getBoundingClientRect();
return {
width: Math.round(rect.width),
height: Math.round(rect.height)
}
}

/**
*
* @copyright https://github.com/Mr0grog/element-query/blob/master/LICENSE
Expand Down Expand Up @@ -85,7 +105,7 @@
function SetupInformation(element) {
this.element = element;
this.options = {};
var key, option, width = 0, height = 0, value, actualValue, attrValues, attrValue, attrName;
var key, option, elementSize, value, actualValue, attrValues, attrValue, attrName;

/**
* @param {Object} option {mode: 'min|max', property: 'width|height', value: '123px'}
Expand All @@ -102,8 +122,7 @@
*/
this.call = function() {
// extract current dimensions
width = this.element.offsetWidth;
height = this.element.offsetHeight;
elementSize = getElementSize(this.element);

attrValues = {};

Expand All @@ -115,7 +134,7 @@

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

actualValue = option.property == 'width' ? width : height;
actualValue = option.property == 'width' ? elementSize.width : elementSize.height;
attrName = option.mode + '-' + option.property;
attrValue = '';

Expand Down
32 changes: 27 additions & 5 deletions src/ResizeSensor.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,26 @@
}
}

/**
* Get element size
* @param {HTMLElement} element
* @returns {Object} {width, height}
*/
function getElementSize(element) {
if (!element.getBoundingClientRect) {
return {
width: element.offsetWidth,
height: element.offsetHeight
}
}

var rect = element.getBoundingClientRect();
return {
width: Math.round(rect.width),
height: Math.round(rect.height)
}
}

/**
* Class for dimension change detection.
*
Expand Down Expand Up @@ -130,8 +150,9 @@
var expandChild = expand.childNodes[0];
var shrink = element.resizeSensor.childNodes[1];
var dirty, rafId, newWidth, newHeight;
var lastWidth = element.offsetWidth;
var lastHeight = element.offsetHeight;
var size = getElementSize(element);
var lastWidth = size.width;
var lastHeight = size.height;

var reset = function() {
expandChild.style.width = '100000px';
Expand Down Expand Up @@ -160,8 +181,9 @@
};

var onScroll = function() {
newWidth = element.offsetWidth;
newHeight = element.offsetHeight;
var size = getElementSize(element);
var newWidth = size.width;
var newHeight = size.height;
dirty = newWidth != lastWidth || newHeight != lastHeight;

if (dirty && !rafId) {
Expand Down Expand Up @@ -194,7 +216,7 @@

ResizeSensor.detach = function(element, ev) {
forEachElement(element, function(elem){
if (!elem) return
if (!elem) return;
if(elem.resizedAttached && typeof ev == "function"){
elem.resizedAttached.remove(ev);
if(elem.resizedAttached.length()) return;
Expand Down