简体   繁体   English

缩放div内容以适合固定大小

[英]scale div content to fit a fixed size

I have two div's in my page with the following style. 我的页面上有两个具有以下样式的div。

 #currentExpr { visibility: hidden; height: 1px; width: 1px; } #currentExprFront { height: 3.5cm; font-size: 500%; } 
 <div id="currentExpr">\\( \\)</div> <div id="currentExprFront"></div> 

As you see, the first one is hidden. 如您所见,第一个隐藏。 On it, some dynamic processes (JavaScript, call of external server, ...) are done until the div content (a long math ml expression) is obtained and updated.It is done on this hidden div to skip a lot of flicks, fast changes, ... until final content is obtained. 在其上执行一些动态过程(JavaScript,调用外部服务器等),直到获得并更新div内容(一个长的数学ml表达式)为止。在此隐藏的div上完成此操作可以跳过很多动作,快速更改,直到获得最终内容。

When final content is obtained, currentExpr content is copied to the visible div currentExprFront using the JavaScript statement: 获得最终内容后,使用JavaScript语句将currentExpr内容复制到可见的div currentExprFront

currentExprFront.innerHTML = currentExpr.innerHTML;

Problem: I need to scale the content/div currentExprFront to fit the expected size ( 3.5cm ). 问题:我需要缩放content / div currentExprFront以适合预期的大小( 3.5cm )。 Sometimes, the content is bigger than this maximum. 有时,内容大于此最大值。 A possibility could be, by example, adjusting the font-size percentage. 例如,一种可能是调整字体大小百分比。

Any hint? 有什么提示吗? Thanks a lot. 非常感谢。

This is a demo of what I tried to explain in the comments -- JavaScript can move very quick, so quick that the user's eye will not notice changes (or the browser may not bother to completely render). 这是我尝试在注释中解释的演示-JavaScript可以非常快速地移动,以至于用户的眼睛看不到变化(或者浏览器可能不愿意完全渲染)。 So, using this speed advantage... 因此,利用这种速度优势...

  • Wrap the content into a container (in this case, a span ). 将内容包装到容器中(在本例中为span )。
  • Populate the target div with the content that you're outputting. 使用您要输出的内容填充目标div
  • Measure the height of the span vs. the height of the div 测量span的高度与div的高度
  • Increment the font-size based on whether the content is too large or too small. 根据内容是太大还是太小来增加字体大小。

 const target = document.getElementById('target'); const longText = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus ornare orci non tristique facilisis. Sed erat eros, dapibus sed arcu lobortis, consequat mollis odio. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Etiam efficitur metus enim, placerat varius felis fringilla vitae. Sed consectetur massa nec nulla ullamcorper tincidunt. Morbi eu orci hendrerit, egestas mi ac, consequat odio. Aenean id erat vitae sem mollis convallis quis in quam. Donec varius tempus ligula, in vulputate nisl pretium vel. Nullam ac arcu finibus, aliquam erat quis, lacinia metus. Aenean convallis magna et lectus mollis, eget scelerisque turpis dapibus. Donec sit amet erat mi. Sed tempus, tortor vel vehicula feugiat, elit purus fringilla tellus, eget molestie ex libero interdum mauris. Nam vehicula a justo et viverra. Aenean eget fringilla erat, id blandit sapien.'; const longTextBtn = document.getElementById('longText'); longTextBtn.addEventListener('click', ()=>{ addText( longText ); } ); const shortText = 'Lorem ipsum dolor.'; const shortTextBtn = document.getElementById('shortText'); shortTextBtn.addEventListener('click', ()=>{ addText( shortText ); } ); // addText() :: Responsible for creating the span element and // assigning the text to the target div/container function addText( phrase ){ target.innerHTML = ''; let targetSpan = document.createElement('span'); targetSpan.id = 'tempID'; let data = document.createTextNode( phrase ); targetSpan.appendChild( data ); target.appendChild( targetSpan ); resizeText( targetSpan, target ); } // resizeText() :: A recurssive function that will measure the size // of the content vs. the size of the container and // adjust the font-size accordingly. // Needed improvements: // - Create a stop condition (to prevent an infinite loop) // - Create a condition to stop if/when the counter hits zero (0) function resizeText( span, div ){ let fontSizeTxt = span.style.fontSize || '10px'; span.style.fontSize = fontSizeTxt; let fontSizeNum = parseInt( fontSizeTxt ); // IF the text is SMALLER than the containing DIV if( div.offsetHeight > span.offsetHeight ){ span.style.fontSize = `${fontSizeNum + 1}px`; // if the text is still too small, rerun if( div.offsetHeight > span.offsetHeight ){ resizeText( span, div ); } } // IF the text is LARGER than the containing DIV if( span.offsetHeight > div.offsetHeight ){ span.style.fontSize = `${fontSizeNum - 1}px`; // if the text is still too large, rerun if( div.offsetHeight < span.offsetHeight ){ resizeText( span, div ); } } } 
 #target{ width: 200px; height: 200px; overflow: hidden; font-family: Arial; border: solid 1px black; } #target span{ display: block; } 
 <div id="target"></div> <br /> <button id="longText">Add Long Text</button> <br /> <button id="shortText">Add Short Text</button> 

Based on @Doug answer, the implemented solution has been replace the statement: 基于@Doug的答案,已实现的解决方案已替换该语句:

currentExprFront.innerHTML = currentExpr.innerHTML;

with the statements: 带有以下语句:

const k = 1.25;
const lk = Math.log(k);

function scaleAndCopy( d, o ) {
  /* width and height ratios */
  var h = d.offsetHeight/o.offsetHeight; 
  var v = d.offsetWidth/o.offsetWidth; 

  /* adjust to 1.25^n */ 
  h = Math.pow(1.25,Math.floor(Math.log(h)/lk)); 
  v = Math.pow(1.25,Math.floor(Math.log(v)/lk));

  /* set font size ratio and copy */
  var r = 100*Math.min(h,v); 
  d.style.fontSize = `${r}%`; 
  d.innerHTML = o.innerHTML
}

scale( currentExprFront, currentExpr );

this solution allows a single-step adjustment and keeps the fontSize stable to minor changes in the content, because the possible font size values are not continuous but 100%*1.25^n: ..., 80%, 100%, 125%, 156%, ... . 此解决方案允许单步调整并保持fontSize对内容的微小变化稳定,因为可能的字体大小值不是连续的,而是100%* 1.25 ^ n:...,80%,100%,125%, 156%,...。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM