Skip to content
This repository has been archived by the owner on Dec 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #16 from PolymerElements/fitinto-center
Browse files Browse the repository at this point in the history
fix centering an element in another element
  • Loading branch information
Yvonne Yip committed Jun 25, 2015
2 parents f82ad74 + 9250245 commit 17ad03d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
24 changes: 22 additions & 2 deletions iron-fit-behavior.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,26 @@
return fitHeight;
},

get _fitLeft() {
var fitLeft;
if (this.fitInto === window) {
fitLeft = 0;
} else {
fitLeft = this.fitInto.getBoundingClientRect().left;
}
return fitLeft;
},

get _fitTop() {
var fitTop;
if (this.fitInto === window) {
fitTop = 0;
} else {
fitTop = this.fitInto.getBoundingClientRect().top;
}
return fitTop;
},

attached: function() {
if (this.autoFitOnAttach) {
if (window.getComputedStyle(this).display === 'none') {
Expand Down Expand Up @@ -220,12 +240,12 @@
this.style.position = 'fixed';
}
if (!this._fitInfo.positionedBy.vertically) {
var top = (this._fitHeight - this.offsetHeight) / 2;
var top = (this._fitHeight - this.offsetHeight) / 2 + this._fitTop;
top -= this._fitInfo.margin.top;
this.style.top = top + 'px';
}
if (!this._fitInfo.positionedBy.horizontally) {
var left = (this._fitWidth - this.offsetWidth) / 2;
var left = (this._fitWidth - this.offsetWidth) / 2 + this._fitLeft;
left -= this._fitInfo.margin.left;
this.style.left = left + 'px';
}
Expand Down
14 changes: 13 additions & 1 deletion test/iron-fit-behavior.html
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@

<test-fixture id="constrain-target">
<template>
<div class="constrain" style="position: fixed; top: 0; left: 0; width: 50vw; height: 50vh; border: 1px solid black;">
<div class="constrain" style="position: fixed; top: 20px; left: 100px; width: 50vw; height: 50vh; border: 1px solid black;">
<test-fit auto-fit-on-attach class="el">
<div>
Auto center/center to parent element
Expand Down Expand Up @@ -331,6 +331,18 @@
assert.isTrue(rect.height <= crect.height, 'height is less than or equal to fitInto height');
});

test('element centers in another element', function() {
var constrain = fixture('constrain-target');
var el = Polymer.dom(constrain).querySelector('.el')
makeScrolling(el);
el.fitInto = constrain;
el.refit();
var rect = el.getBoundingClientRect();
var crect = constrain.getBoundingClientRect();
assert.closeTo(rect.left - crect.left - (crect.right - rect.right), 0, 5, 'centered horizontally in fitInto');
assert.closeTo(rect.top - crect.top - (crect.bottom - rect.bottom), 0, 5, 'centered vertically in fitInto');
});

});

</script>
Expand Down

0 comments on commit 17ad03d

Please sign in to comment.