根据字符数动态更改字体大小
我正在尝试根据字符长度调整标题大小。 (WordPress的)
这就是我到目前为止所拥有的。 它不会返回任何错误但它不起作用。
jQuery(function ($) { $(window).load(function () { var textLength = $('.autotitle').val().length; if (textLength 20) { $('.autotitle').css('font-size', '16px'); } }); });
很多时候,当文本很长(基于字母数)(文本可能是通过某些CMS或翻译文本输入的)时,人们希望减小字体大小(使其变小)
$('.text').each(function(){ var el= $(this); var textLength = el.html().length; if (textLength > 20) { el.css('font-size', '0.8em'); } });
试试这个, Demo使用html
而不是使用val
var textLength = $('.autotitle').html().length;
test
1vw代表视口宽度的1% – 响应设计的大部分。
看看这个: http : //css-tricks.com/viewport-sized-typography/
用php函数解决了它
function trim_title() { $title = get_the_title(); $limit = "14"; if(strlen($title) <= $limit) { echo $title; } else { echo ''; echo $title; echo ''; } }
谢谢大家。
如果您使用.autotitle
则更改将应用于您使用.autotitle
作为class
属性的每个元素…
相反,您可以使用特定元素的id
属性将更改应用于该元素
例如:
$('#id').css('font-size','16px');