mirror of
https://github.com/WilliamsNY/contain-element.git
synced 2024-11-21 22:02:31 -05:00
Use || instead of ?:
This commit is contained in:
parent
10009824fc
commit
9aa7ecc880
3 changed files with 4 additions and 11 deletions
|
@ -29,6 +29,7 @@ Written by [Williams New York](http://williamsnewyork.com)
|
||||||
### Authors
|
### Authors
|
||||||
|
|
||||||
* Kevin MacMartin
|
* Kevin MacMartin
|
||||||
|
* Luke Evers
|
||||||
|
|
||||||
## LICENSE
|
## LICENSE
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
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 ? options.width : element.offsetWidth;
|
var elementWidth = options.width || element.offsetWidth;
|
||||||
var elementHeight = options.height ? options.height : element.offsetHeight;
|
var elementHeight = options.height || element.offsetHeight;
|
||||||
|
|
||||||
// Apply required attributes to the element and its parents if they aren't already set
|
// Apply required attributes to the element and its parents if they aren't already set
|
||||||
if (element.style.position != 'absolute')
|
if (element.style.position != 'absolute')
|
||||||
|
|
10
contain-element.min.js
vendored
10
contain-element.min.js
vendored
|
@ -1,9 +1 @@
|
||||||
|
function containElement(e){function t(){var s=i.parfunction containElement(e){function t(){var s=i.parentElement.offsetWidth,a=i.parentElement.offsetHeight;if(s&&a){if(n=s>a/(o*n)*l*n?s/l:a/o,i.style.width=l*n+"px",i.style.height=o*n+"px",l*n>s)switch(e.halign){case"left":i.style.left="0px";break;case"right":i.style.left=0-(l*n-s)+"px";break;default:i.style.left=0-(l*n-s)/2+"px"}else i.style.left="0px";if(o*n>a)switch(e.valign){case"top":i.style.top="0px";break;case"bottom":i.style.top=0-(o*n-a)+"px";break;default:i.style.top=0-(o*n-a)/2+"px"}else i.style.top="0px"}else window.setTimeout(t,30)}var n=1,i=document.getElementById(e.id),l=e.width||i.offsetWidth,o=e.height||i.offsetHeight;"absolute"!=i.style.position&&(i.style.position="absolute"),"hidden"!=i.parentElement.style.overflow&&(i.parentElement.style.overflow="hidden"),"relative"!=i.parentElement.style.position&&"absolute"!=i.parentElement.style.position&&"fixed"!=i.parentElement.style.position&&(i.parentElement.style.position="relative"),t(),window.onload=function(){t()},window.onresize=function(){t()},screen.addEventListener("orientationchange",function(){t()})}entElement.offsetWidth,a=i.parentElement.offsetHeight;if(s&&a){if(n=s>a/(o*n)*l*n?s/l:a/o,i.style.width=l*n+"px",i.style.height=o*n+"px",l*n>s)switch(e.halign){case"left":i.style.left="0px";break;case"right":i.style.left=0-(l*n-s)+"px";break;default:i.style.left=0-(l*n-s)/2+"px"}else i.style.left="0px";if(o*n>a)switch(e.valign){case"top":i.style.top="0px";break;case"bottom":i.style.top=0-(o*n-a)+"px";break;default:i.style.top=0-(o*n-a)/2+"px"}else i.style.top="0px"}else window.setTimeout(t,30)}var n=1,i=document.getElementById(e.id),l=e.width||i.offsetWidth,o=e.height||i.offsetHeight;"absolute"!=i.style.position&&(i.style.position="absolute"),"hidden"!=i.parentElement.style.overflow&&(i.parentElement.style.overflow="hidden"),"relative"!=i.parentElement.style.position&&"absolute"!=i.parentElement.style.position&&"fixed"!=i.parentElement.style.position&&(i.parentElement.style.position="relative"),t(),window.onload=function(){t()},window.onresize=function(){t()},screen.addEventListener("orientationchange",function(){t()})}
|
||||||
function containElement(options){var scaleFactor=1;var element=document.getElementById(options.id);var elementWidth=options.width?options.width:element.offsetWidth;var elementHeight=options.height?options.height:element.offsetHeight;if(element.style.position!='absolute')
|
|
||||||
element.style.position='absolute';if(element.parentElement.style.overflow!='hidden')
|
|
||||||
element.parentElement.style.overflow='hidden';if(element.parentElement.style.position!='relative'&&element.parentElement.style.position!='absolute'&&element.parentElement.style.position!='fixed')
|
|
||||||
element.parentElement.style.position='relative';function scaleElement(){var parentWidth=element.parentElement.offsetWidth;var parentHeight=element.parentElement.offsetHeight;if((parentWidth)&&(parentHeight)){if(parentWidth>((parentHeight/(elementHeight*scaleFactor))*(elementWidth*scaleFactor)))
|
|
||||||
scaleFactor=(parentWidth/elementWidth);else
|
|
||||||
scaleFactor=(parentHeight/elementHeight);element.style.width=(elementWidth*scaleFactor)+'px';element.style.height=(elementHeight*scaleFactor)+'px';if(parentWidth<(elementWidth*scaleFactor)){switch(options.halign){case'left':element.style.left=0+'px';break;case'right':element.style.left=(0-((elementWidth*scaleFactor)-parentWidth))+'px';break;default:element.style.left=(0-(((elementWidth*scaleFactor)-parentWidth)/2))+'px';}}else{element.style.left=0+'px';}
|
|
||||||
if((elementHeight*scaleFactor)>parentHeight){switch(options.valign){case'top':element.style.top=0+'px';break;case'bottom':element.style.top=(0-((elementHeight*scaleFactor)-parentHeight))+'px';break;default:element.style.top=(0-(((elementHeight*scaleFactor)-parentHeight)/2))+'px';}}else{element.style.top=0+'px';}}else{window.setTimeout(scaleElement,30);}}
|
|
||||||
scaleElement();window.onload=function(event){scaleElement();};window.onresize=function(event){scaleElement();};screen.addEventListener('orientationchange',function(){scaleElement();});}
|
|
||||||
|
|
Loading…
Reference in a new issue