contain-element/contain-element.min.js

1 line
1.3 KiB
JavaScript
Raw Normal View History

function ContainElement(t){function e(){var t=n.parentElement.offsetWidth,r=n.parentElement.offsetHeight;if(t&&r){if(i=t>r/(s*i)*(l*i)?t/l:r/s,f?n.style.transform="scale("+i+")":(n.style.width=l*i+"px",n.style.height=s*i+"px"),t<l*i)switch(a){case"left":n.style.left="0px";break;case"right":n.style.left=0-(l*i-t)+"px";break;default:n.style.left=0-(l*i-t)/2+"px"}else n.style.left="0px";if(s*i>r)switch(o){case"top":n.style.top="0px";break;case"bottom":n.style.top=0-(s*i-r)+"px";break;default:n.style.top=0-(s*i-r)/2+"px"}else n.style.top="0px"}else window.setTimeout(e,30)}var i=1,n=t.element||document.getElementById(t.id),l=t.width||n.offsetWidth,s=t.height||n.offsetHeight,o=t.valign||"center",a=t.halign||"center",f=t.scale;n.style.position="absolute",n.parentElement.style.overflow="hidden",f&&(n.style.transformOrigin="left top"),-1===["relative","absolute","fixed"].indexOf(window.getComputedStyle(n.parentElement,null).getPropertyValue("position"))&&(n.parentElement.style.position="relative"),e(),this.update=e,this.setWidth=function(t){l=t},this.setHeight=function(t){s=t},this.setValign=function(t){o=t},this.setHalign=function(t){a=t},this.getWidth=function(){return l},this.getHeight=function(){return s},this.getValign=function(){return o},this.getHalign=function(){return a},this.getScale=function(){return i}}