Javascript jquery根据元素div大小调整文本大小
我有一个特定的问题,我需要根据父对象中的元素数量自动调整文本大小。我尝试了一些库,比如BigText和FitText,但它们似乎不起作用Javascript jquery根据元素div大小调整文本大小,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我有一个特定的问题,我需要根据父对象中的元素数量自动调整文本大小。我尝试了一些库,比如BigText和FitText,但它们似乎不起作用 <div class="prize-cont"> <div class="prize"> <h2>$1000</h2> <span>Youth Grand Prize</span> </div> <div class
<div class="prize-cont">
<div class="prize">
<h2>$1000</h2>
<span>Youth Grand Prize</span>
</div>
<div class="prize">
<h2>$1000</h2>
<span>Adult Grand Prize</span>
</div>
<div class="prize">
<h2>$200</h2>
<span>Finalists</span>
</div>
<div class="prize">
<h2>T-Shirt</h2>
<span>Viewer's Choice</span>
</div>
</div>
例如,如果我的h2是20px,我的跨度是12px,我有4个奖品,它们都可以放在容器中,但是我再加一个奖品,我需要从所有元素中减去任何差值,使所有跨度都是10px,所有h2都是15px,这样它们就可以放在容器中
我已经在这方面工作了几个小时,似乎还没弄明白。如果他们知道一个插件/库可以做到这一点,或者知道一个简单的脚本,那么任何帮助都将不胜感激
$(function() {
var containerHeight = $('.prize-cont').height();
var prizeContainers = $('.prize');
var prizeNum = prizeContainers.length;
// The max height of a single prize container
var heightPerPrize = containerHeight / prizeNum;
var current;
var header;
var span;
// For each prize container...
$.each(prizeContainers, function() {
current = $(this);
current.height(heightPerPrize);
// Set header height to a fractional height, here it is 1/3
header = current.find('h2').height(heightPerPrize / 3);
// Set span height to a fractional height, here it is 2/3
span = current.find('span').height((heightPerPrize / 3) * 2)
});
});
<div class="prize-cont">
<div class="prize">
<div>
<h2>$1000</h2>
<span>Youth Grand Prize</span>
</div>
</div>
<div class="prize">
<div>
<h2>$1000</h2>
<span>Adult Grand Prize</span>
</div>
</div>
<div class="prize">
<div>
<h2>$200</h2>
<span>Finalists</span>
</div>
</div>
<div class="prize">
<div>
<h2>T-Shirt</h2>
<span>Viewer's Choice</span>
</div>
</div>
</div>
像这样的怎么样:
$(function() {
var containerHeight = $('.prize-cont').height();
var prizeContainers = $('.prize');
var prizeNum = prizeContainers.length;
// The max height of a single prize container
var heightPerPrize = containerHeight / prizeNum;
var current;
var header;
var span;
// For each prize container...
$.each(prizeContainers, function() {
current = $(this);
current.height(heightPerPrize);
// Set header height to a fractional height, here it is 1/3
header = current.find('h2').height(heightPerPrize / 3);
// Set span height to a fractional height, here it is 2/3
span = current.find('span').height((heightPerPrize / 3) * 2)
});
});
<div class="prize-cont">
<div class="prize">
<div>
<h2>$1000</h2>
<span>Youth Grand Prize</span>
</div>
</div>
<div class="prize">
<div>
<h2>$1000</h2>
<span>Adult Grand Prize</span>
</div>
</div>
<div class="prize">
<div>
<h2>$200</h2>
<span>Finalists</span>
</div>
</div>
<div class="prize">
<div>
<h2>T-Shirt</h2>
<span>Viewer's Choice</span>
</div>
</div>
</div>
像这样的怎么样:
$(function() {
var containerHeight = $('.prize-cont').height();
var prizeContainers = $('.prize');
var prizeNum = prizeContainers.length;
// The max height of a single prize container
var heightPerPrize = containerHeight / prizeNum;
var current;
var header;
var span;
// For each prize container...
$.each(prizeContainers, function() {
current = $(this);
current.height(heightPerPrize);
// Set header height to a fractional height, here it is 1/3
header = current.find('h2').height(heightPerPrize / 3);
// Set span height to a fractional height, here it is 2/3
span = current.find('span').height((heightPerPrize / 3) * 2)
});
});
<div class="prize-cont">
<div class="prize">
<div>
<h2>$1000</h2>
<span>Youth Grand Prize</span>
</div>
</div>
<div class="prize">
<div>
<h2>$1000</h2>
<span>Adult Grand Prize</span>
</div>
</div>
<div class="prize">
<div>
<h2>$200</h2>
<span>Finalists</span>
</div>
</div>
<div class="prize">
<div>
<h2>T-Shirt</h2>
<span>Viewer's Choice</span>
</div>
</div>
</div>
像这样的怎么样:
$(function() {
var containerHeight = $('.prize-cont').height();
var prizeContainers = $('.prize');
var prizeNum = prizeContainers.length;
// The max height of a single prize container
var heightPerPrize = containerHeight / prizeNum;
var current;
var header;
var span;
// For each prize container...
$.each(prizeContainers, function() {
current = $(this);
current.height(heightPerPrize);
// Set header height to a fractional height, here it is 1/3
header = current.find('h2').height(heightPerPrize / 3);
// Set span height to a fractional height, here it is 2/3
span = current.find('span').height((heightPerPrize / 3) * 2)
});
});
<div class="prize-cont">
<div class="prize">
<div>
<h2>$1000</h2>
<span>Youth Grand Prize</span>
</div>
</div>
<div class="prize">
<div>
<h2>$1000</h2>
<span>Adult Grand Prize</span>
</div>
</div>
<div class="prize">
<div>
<h2>$200</h2>
<span>Finalists</span>
</div>
</div>
<div class="prize">
<div>
<h2>T-Shirt</h2>
<span>Viewer's Choice</span>
</div>
</div>
</div>
你可以这样做:
$(function() {
var containerHeight = $('.prize-cont').height();
var prizeContainers = $('.prize');
var prizeNum = prizeContainers.length;
// The max height of a single prize container
var heightPerPrize = containerHeight / prizeNum;
var current;
var header;
var span;
// For each prize container...
$.each(prizeContainers, function() {
current = $(this);
current.height(heightPerPrize);
// Set header height to a fractional height, here it is 1/3
header = current.find('h2').height(heightPerPrize / 3);
// Set span height to a fractional height, here it is 2/3
span = current.find('span').height((heightPerPrize / 3) * 2)
});
});
<div class="prize-cont">
<div class="prize">
<div>
<h2>$1000</h2>
<span>Youth Grand Prize</span>
</div>
</div>
<div class="prize">
<div>
<h2>$1000</h2>
<span>Adult Grand Prize</span>
</div>
</div>
<div class="prize">
<div>
<h2>$200</h2>
<span>Finalists</span>
</div>
</div>
<div class="prize">
<div>
<h2>T-Shirt</h2>
<span>Viewer's Choice</span>
</div>
</div>
</div>
$1000
青年大奖
$1000
成人大奖
$200
决赛选手
T恤
观众选择奖
然后使用溢出/高度的概念来调整文本的大小:
$('.prize-cont div').each(function () {
var container = $(this),
content = container.find('div'),
i = 0;
while (content.height() > container.height() && i < 10) {
var fontSize = container.css('fontSize'),
numSize = parseInt(fontSize, 10),
units = fontSize.split(numSize)[1],
newSize = numSize - 1;
container.css('fontSize', newSize + units);
i++;
}
});
$('.prize cont div')。每个(函数(){
变量容器=$(此),
content=container.find('div'),
i=0;
while(content.height()>container.height()&&i<10){
var fontSize=container.css('fontSize'),
numSize=parseInt(fontSize,10),
单位=fontSize.split(numSize)[1],
newSize=numSize-1;
css('fontSize',newSize+单位);
i++;
}
});
这里有一把工作小提琴供你“摆弄”:
我确信这种方法存在一些问题,但它可能会解决您的问题。需要注意的一点是,少量的JavaScript依赖于对div的内容使用相对字体大小。我还在容器div中添加了一个div,它允许flex。当该高度大于容器时,内容字体大小将减小。您可以执行以下操作:
$(function() {
var containerHeight = $('.prize-cont').height();
var prizeContainers = $('.prize');
var prizeNum = prizeContainers.length;
// The max height of a single prize container
var heightPerPrize = containerHeight / prizeNum;
var current;
var header;
var span;
// For each prize container...
$.each(prizeContainers, function() {
current = $(this);
current.height(heightPerPrize);
// Set header height to a fractional height, here it is 1/3
header = current.find('h2').height(heightPerPrize / 3);
// Set span height to a fractional height, here it is 2/3
span = current.find('span').height((heightPerPrize / 3) * 2)
});
});
<div class="prize-cont">
<div class="prize">
<div>
<h2>$1000</h2>
<span>Youth Grand Prize</span>
</div>
</div>
<div class="prize">
<div>
<h2>$1000</h2>
<span>Adult Grand Prize</span>
</div>
</div>
<div class="prize">
<div>
<h2>$200</h2>
<span>Finalists</span>
</div>
</div>
<div class="prize">
<div>
<h2>T-Shirt</h2>
<span>Viewer's Choice</span>
</div>
</div>
</div>
$1000
青年大奖
$1000
成人大奖
$200
决赛选手
T恤
观众选择奖
然后使用溢出/高度的概念来调整文本的大小:
$('.prize-cont div').each(function () {
var container = $(this),
content = container.find('div'),
i = 0;
while (content.height() > container.height() && i < 10) {
var fontSize = container.css('fontSize'),
numSize = parseInt(fontSize, 10),
units = fontSize.split(numSize)[1],
newSize = numSize - 1;
container.css('fontSize', newSize + units);
i++;
}
});
$('.prize cont div')。每个(函数(){
变量容器=$(此),
content=container.find('div'),
i=0;
while(content.height()>container.height()&&i<10){
var fontSize=container.css('fontSize'),
numSize=parseInt(fontSize,10),
单位=fontSize.split(numSize)[1],
newSize=numSize-1;
css('fontSize',newSize+单位);
i++;
}
});
这里有一把工作小提琴供你“摆弄”:
我确信这种方法存在一些问题,但它可能会解决您的问题。需要注意的一点是,少量的JavaScript依赖于对div的内容使用相对字体大小。我还在容器div中添加了一个div,它允许flex。当该高度大于容器时,内容字体大小将减小。您可以执行以下操作:
$(function() {
var containerHeight = $('.prize-cont').height();
var prizeContainers = $('.prize');
var prizeNum = prizeContainers.length;
// The max height of a single prize container
var heightPerPrize = containerHeight / prizeNum;
var current;
var header;
var span;
// For each prize container...
$.each(prizeContainers, function() {
current = $(this);
current.height(heightPerPrize);
// Set header height to a fractional height, here it is 1/3
header = current.find('h2').height(heightPerPrize / 3);
// Set span height to a fractional height, here it is 2/3
span = current.find('span').height((heightPerPrize / 3) * 2)
});
});
<div class="prize-cont">
<div class="prize">
<div>
<h2>$1000</h2>
<span>Youth Grand Prize</span>
</div>
</div>
<div class="prize">
<div>
<h2>$1000</h2>
<span>Adult Grand Prize</span>
</div>
</div>
<div class="prize">
<div>
<h2>$200</h2>
<span>Finalists</span>
</div>
</div>
<div class="prize">
<div>
<h2>T-Shirt</h2>
<span>Viewer's Choice</span>
</div>
</div>
</div>
$1000
青年大奖
$1000
成人大奖
$200
决赛选手
T恤
观众选择奖
然后使用溢出/高度的概念来调整文本的大小:
$('.prize-cont div').each(function () {
var container = $(this),
content = container.find('div'),
i = 0;
while (content.height() > container.height() && i < 10) {
var fontSize = container.css('fontSize'),
numSize = parseInt(fontSize, 10),
units = fontSize.split(numSize)[1],
newSize = numSize - 1;
container.css('fontSize', newSize + units);
i++;
}
});
$('.prize cont div')。每个(函数(){
变量容器=$(此),
content=container.find('div'),
i=0;
while(content.height()>container.height()&&i<10){
var fontSize=container.css('fontSize'),
numSize=parseInt(fontSize,10),
单位=fontSize.split(numSize)[1],
newSize=numSize-1;
css('fontSize',newSize+单位);
i++;
}
});
这里有一把工作小提琴供你“摆弄”:
我确信这种方法存在一些问题,但它可能会解决您的问题。需要注意的一点是,少量的JavaScript依赖于对div的内容使用相对字体大小。我还在容器div中添加了一个div,它允许flex。当该高度大于容器时,内容字体大小将减小。您可以执行以下操作:
$(function() {
var containerHeight = $('.prize-cont').height();
var prizeContainers = $('.prize');
var prizeNum = prizeContainers.length;
// The max height of a single prize container
var heightPerPrize = containerHeight / prizeNum;
var current;
var header;
var span;
// For each prize container...
$.each(prizeContainers, function() {
current = $(this);
current.height(heightPerPrize);
// Set header height to a fractional height, here it is 1/3
header = current.find('h2').height(heightPerPrize / 3);
// Set span height to a fractional height, here it is 2/3
span = current.find('span').height((heightPerPrize / 3) * 2)
});
});
<div class="prize-cont">
<div class="prize">
<div>
<h2>$1000</h2>
<span>Youth Grand Prize</span>
</div>
</div>
<div class="prize">
<div>
<h2>$1000</h2>
<span>Adult Grand Prize</span>
</div>
</div>
<div class="prize">
<div>
<h2>$200</h2>
<span>Finalists</span>
</div>
</div>
<div class="prize">
<div>
<h2>T-Shirt</h2>
<span>Viewer's Choice</span>
</div>
</div>
</div>
$1000
青年大奖
$1000
成人大奖
$200
决赛选手
T恤
观众选择奖
然后使用溢出/高度的概念来调整文本的大小:
$('.prize-cont div').each(function () {
var container = $(this),
content = container.find('div'),
i = 0;
while (content.height() > container.height() && i < 10) {
var fontSize = container.css('fontSize'),
numSize = parseInt(fontSize, 10),
units = fontSize.split(numSize)[1],
newSize = numSize - 1;
container.css('fontSize', newSize + units);
i++;
}
});
$('.prize cont div')。每个(函数(){
变量容器=$(此),
content=container.find('div'),
i=0;
while(content.height()>container.height()&&i<10){
var fontSize=container.css('fontSize'),
numSize=parseInt(fontSize,10),
单位=fontSize.split(numSize)[1],
newSize=numSize-1;
css('fontSize',newSize+单位);
i++;
}
});
这里有一把工作小提琴供你“摆弄”:
我确信这种方法存在一些问题,但它可能会解决您的问题。需要注意的一点是,少量的JavaScript依赖于对div的内容使用相对字体大小。我还在容器div中添加了一个div,它允许flex。当该高度大于容器时,内容字体大小将减小。请发布您的尝试。