2015-06-30 17:40:10 -04:00
|
|
|
function ContainElement(options) {
|
2015-06-30 16:10:22 -04:00
|
|
|
var scaleFactor = 1;
|
|
|
|
var element = document.getElementById(options.id);
|
2015-06-30 16:34:11 -04:00
|
|
|
var elementWidth = options.width || element.offsetWidth;
|
|
|
|
var elementHeight = options.height || element.offsetHeight;
|
2015-06-30 16:10:22 -04:00
|
|
|
|
2015-07-02 14:35:51 -04:00
|
|
|
// Apply required attributes to the element and its parents
|
2015-07-02 13:05:28 -04:00
|
|
|
element.style.position = 'absolute';
|
|
|
|
element.parentElement.style.overflow = 'hidden';
|
2015-07-02 14:35:51 -04:00
|
|
|
if (['relative', 'absolute', 'fixed'].indexOf(window.getComputedStyle(element.parentElement, null).getPropertyValue('position')) === -1)
|
2015-06-30 16:10:22 -04:00
|
|
|
element.parentElement.style.position = 'relative';
|
|
|
|
|
2015-06-30 17:40:10 -04:00
|
|
|
function updateContain() {
|
2015-06-30 16:10:22 -04:00
|
|
|
var parentWidth = element.parentElement.offsetWidth;
|
|
|
|
var parentHeight = element.parentElement.offsetHeight;
|
|
|
|
|
2015-07-02 15:16:19 -04:00
|
|
|
// Run the scale/position functionality if able to determine the parent element's width and height
|
2015-06-30 16:10:22 -04:00
|
|
|
if ((parentWidth) && (parentHeight)) {
|
|
|
|
// Calculate the scale factor
|
|
|
|
if (parentWidth > ((parentHeight / (elementHeight * scaleFactor)) * (elementWidth * scaleFactor)))
|
|
|
|
scaleFactor = (parentWidth / elementWidth);
|
|
|
|
else
|
|
|
|
scaleFactor = (parentHeight / elementHeight);
|
|
|
|
|
|
|
|
// Scale the element using the scale factor
|
|
|
|
element.style.width = (elementWidth * scaleFactor) + 'px';
|
|
|
|
element.style.height = (elementHeight * scaleFactor) + 'px';
|
|
|
|
|
2015-07-02 15:10:53 -04:00
|
|
|
// Anchor the element horizontally to the left/center/right
|
2015-06-30 16:10:22 -04:00
|
|
|
if (parentWidth < (elementWidth * scaleFactor)) {
|
|
|
|
switch(options.halign) {
|
|
|
|
case 'left':
|
2015-07-02 14:35:51 -04:00
|
|
|
// Anchor horizontally to the left of the parent element
|
2015-06-30 16:10:22 -04:00
|
|
|
element.style.left = 0 + 'px';
|
|
|
|
break;
|
|
|
|
case 'right':
|
2015-07-02 14:35:51 -04:00
|
|
|
// Anchor horizontally to the right of the parent element
|
2015-06-30 16:10:22 -04:00
|
|
|
element.style.left = (0 - ((elementWidth * scaleFactor) - parentWidth)) + 'px';
|
|
|
|
break;
|
|
|
|
default:
|
2015-07-02 14:35:51 -04:00
|
|
|
// Anchor horizontally to the center of the parent element
|
|
|
|
element.style.left = (0 - (((elementWidth * scaleFactor) - parentWidth) / 2)) + 'px';
|
2015-06-30 16:10:22 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
element.style.left = 0 + 'px';
|
|
|
|
}
|
|
|
|
|
2015-07-02 15:10:53 -04:00
|
|
|
// Anchor the element vertically to the top/center/bottom
|
2015-06-30 16:10:22 -04:00
|
|
|
if ((elementHeight * scaleFactor) > parentHeight) {
|
|
|
|
switch(options.valign) {
|
|
|
|
case 'top':
|
2015-07-02 14:35:51 -04:00
|
|
|
// Anchor vertically to the top of the parent element
|
2015-06-30 16:10:22 -04:00
|
|
|
element.style.top = 0 + 'px';
|
|
|
|
break;
|
|
|
|
case 'bottom':
|
2015-07-02 14:35:51 -04:00
|
|
|
// Anchor veritcally to the bottom of the parent element
|
2015-06-30 16:10:22 -04:00
|
|
|
element.style.top = (0 - ((elementHeight * scaleFactor) - parentHeight)) + 'px';
|
|
|
|
break;
|
|
|
|
default:
|
2015-07-02 14:35:51 -04:00
|
|
|
// Anchor vertically to the center of the parent element
|
|
|
|
element.style.top = (0 - (((elementHeight * scaleFactor) - parentHeight) / 2)) + 'px';
|
2015-06-30 16:10:22 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
element.style.top = 0 + 'px';
|
|
|
|
}
|
|
|
|
} else {
|
2015-07-02 15:15:17 -04:00
|
|
|
// Try again in 30ms if the document didn't load enough to determine the parent element's width and height yet
|
2015-06-30 17:40:10 -04:00
|
|
|
window.setTimeout(updateContain, 30);
|
2015-06-30 16:10:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-02 15:14:23 -04:00
|
|
|
// Run the function to scale and anchor the element
|
2015-06-30 17:40:10 -04:00
|
|
|
updateContain();
|
|
|
|
|
2015-07-02 15:14:23 -04:00
|
|
|
// Add an external scale and anchor update function
|
2015-07-01 12:34:24 -04:00
|
|
|
this.update = updateContain;
|
2015-06-30 16:10:22 -04:00
|
|
|
}
|
|
|
|
|