mirror of
https://github.com/WilliamsNY/contain-element.git
synced 2024-12-17 22:08:23 -05:00
Replace tabs with (4) spaces
This commit is contained in:
parent
52e1f93e4a
commit
9ea6f875b4
1 changed files with 67 additions and 67 deletions
|
@ -1,78 +1,78 @@
|
||||||
function ContainElement(options) {
|
function ContainElement(options) {
|
||||||
var scaleFactor = 1;
|
var scaleFactor = 1;
|
||||||
var element = document.getElementById(options.id);
|
var element = document.getElementById(options.id);
|
||||||
var elementWidth = options.width || element.offsetWidth;
|
var elementWidth = options.width || element.offsetWidth;
|
||||||
var elementHeight = options.height || element.offsetHeight;
|
var elementHeight = options.height || element.offsetHeight;
|
||||||
|
|
||||||
// Apply required attributes to the element and its parents
|
// Apply required attributes to the element and its parents
|
||||||
element.style.position = 'absolute';
|
element.style.position = 'absolute';
|
||||||
element.parentElement.style.overflow = 'hidden';
|
element.parentElement.style.overflow = 'hidden';
|
||||||
if (['relative', 'absolute', 'fixed'].indexOf(window.getComputedStyle(element.parentElement, null).getPropertyValue('position')) === -1)
|
if (['relative', 'absolute', 'fixed'].indexOf(window.getComputedStyle(element.parentElement, null).getPropertyValue('position')) === -1)
|
||||||
element.parentElement.style.position = 'relative';
|
element.parentElement.style.position = 'relative';
|
||||||
|
|
||||||
function updateContain() {
|
function updateContain() {
|
||||||
var parentWidth = element.parentElement.offsetWidth;
|
var parentWidth = element.parentElement.offsetWidth;
|
||||||
var parentHeight = element.parentElement.offsetHeight;
|
var parentHeight = element.parentElement.offsetHeight;
|
||||||
|
|
||||||
// Run the scale/position functionality if able to determine the parent element's width and height
|
// Run the scale/position functionality if able to determine the parent element's width and height
|
||||||
if ((parentWidth) && (parentHeight)) {
|
if ((parentWidth) && (parentHeight)) {
|
||||||
// Calculate the scale factor
|
// Calculate the scale factor
|
||||||
if (parentWidth > ((parentHeight / (elementHeight * scaleFactor)) * (elementWidth * scaleFactor)))
|
if (parentWidth > ((parentHeight / (elementHeight * scaleFactor)) * (elementWidth * scaleFactor)))
|
||||||
scaleFactor = (parentWidth / elementWidth);
|
scaleFactor = (parentWidth / elementWidth);
|
||||||
else
|
else
|
||||||
scaleFactor = (parentHeight / elementHeight);
|
scaleFactor = (parentHeight / elementHeight);
|
||||||
|
|
||||||
// Scale the element using the scale factor
|
// Scale the element using the scale factor
|
||||||
element.style.width = (elementWidth * scaleFactor) + 'px';
|
element.style.width = (elementWidth * scaleFactor) + 'px';
|
||||||
element.style.height = (elementHeight * scaleFactor) + 'px';
|
element.style.height = (elementHeight * scaleFactor) + 'px';
|
||||||
|
|
||||||
// Anchor the element horizontally to the left/center/right
|
// Anchor the element horizontally to the left/center/right
|
||||||
if (parentWidth < (elementWidth * scaleFactor)) {
|
if (parentWidth < (elementWidth * scaleFactor)) {
|
||||||
switch(options.halign) {
|
switch(options.halign) {
|
||||||
case 'left':
|
case 'left':
|
||||||
// Anchor horizontally to the left of the parent element
|
// Anchor horizontally to the left of the parent element
|
||||||
element.style.left = 0 + 'px';
|
element.style.left = 0 + 'px';
|
||||||
break;
|
break;
|
||||||
case 'right':
|
case 'right':
|
||||||
// Anchor horizontally to the right of the parent element
|
// Anchor horizontally to the right of the parent element
|
||||||
element.style.left = (0 - ((elementWidth * scaleFactor) - parentWidth)) + 'px';
|
element.style.left = (0 - ((elementWidth * scaleFactor) - parentWidth)) + 'px';
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
// Anchor horizontally to the center of the parent element
|
// Anchor horizontally to the center of the parent element
|
||||||
element.style.left = (0 - (((elementWidth * scaleFactor) - parentWidth) / 2)) + 'px';
|
element.style.left = (0 - (((elementWidth * scaleFactor) - parentWidth) / 2)) + 'px';
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
element.style.left = 0 + 'px';
|
element.style.left = 0 + 'px';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Anchor the element vertically to the top/center/bottom
|
// Anchor the element vertically to the top/center/bottom
|
||||||
if ((elementHeight * scaleFactor) > parentHeight) {
|
if ((elementHeight * scaleFactor) > parentHeight) {
|
||||||
switch(options.valign) {
|
switch(options.valign) {
|
||||||
case 'top':
|
case 'top':
|
||||||
// Anchor vertically to the top of the parent element
|
// Anchor vertically to the top of the parent element
|
||||||
element.style.top = 0 + 'px';
|
element.style.top = 0 + 'px';
|
||||||
break;
|
break;
|
||||||
case 'bottom':
|
case 'bottom':
|
||||||
// Anchor veritcally to the bottom of the parent element
|
// Anchor veritcally to the bottom of the parent element
|
||||||
element.style.top = (0 - ((elementHeight * scaleFactor) - parentHeight)) + 'px';
|
element.style.top = (0 - ((elementHeight * scaleFactor) - parentHeight)) + 'px';
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
// Anchor vertically to the center of the parent element
|
// Anchor vertically to the center of the parent element
|
||||||
element.style.top = (0 - (((elementHeight * scaleFactor) - parentHeight) / 2)) + 'px';
|
element.style.top = (0 - (((elementHeight * scaleFactor) - parentHeight) / 2)) + 'px';
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
element.style.top = 0 + 'px';
|
element.style.top = 0 + 'px';
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Try again in 30ms if the document didn't load enough to determine the parent element's width and height yet
|
// Try again in 30ms if the document didn't load enough to determine the parent element's width and height yet
|
||||||
window.setTimeout(updateContain, 30);
|
window.setTimeout(updateContain, 30);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run the function to scale and anchor the element
|
// Run the function to scale and anchor the element
|
||||||
updateContain();
|
updateContain();
|
||||||
|
|
||||||
// Add an external scale and anchor update function
|
// Add an external scale and anchor update function
|
||||||
this.update = updateContain;
|
this.update = updateContain;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue