Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
function font_size_determination($block,height,width,font_size){
$block.style.fontSize = fontSize + 'px';
var block_height = $block.clientHeight;
var block_width = $block.clientWidth;
if (block_height > height || block_width > width){
font_size = font_size*0.9;
return font_size_determination($block,height,width,font_size);
} else {
return font_size;
}
}
font_size_determination(document.querySelector('.left .frame .frame-content'), 500, 300, 25);
<div class="contain" style="width: 100px; height: 100px">
<div class="containInner">
Some text inside
Some text inside
</div>
</div>var increamentRatio = 1.1,
decreamentRatio = 1 / increamentRatio
function textContaint(containerNode, textContainerNode, ratio) {
var width = containerNode.clientWidth,
height = containerNode.clientHeight,
innerHeight = textContainerNode.clientHeight,
innerWidth = textContainerNode.clientWidth
if (width < innerWidth || height < innerHeight) {
ratio = (ratio && ratio > decreamentRatio) ? false : decreamentRatio
}
else if (width > innerWidth || height > innerHeight) {
ratio = (ratio && ratio < increamentRatio) ? false : increamentRatio
}
else {
return false
}
if (!ratio) {
return false
}
var fontSize = parseFloat(getComputedStyle(containerNode).fontSize)
containerNode.style.fontSize = fontSize * ratio + 'px'
return ratio
}
var textNode = document.querySelector('.contain'),
textContainerNode = textNode.querySelector('.containInner'),
fontSizeRatio
while(fontSizeRatio = textContaint(textNode, textContainerNode, fontSizeRatio)) {}
Масштабирование текста в блоке с помощью Jquery