简体   繁体   English

调整具有多种字体大小的div中的文本大小?

[英]Resizing text in a div with multiple font sizes?

I have some document in a div that is being displayed, and I want the user to be able to adjust the font size of what is inside it. 我在显示的div中有一些文档,我希望用户能够调整其内部的字体大小。 Some sort of code like this: 某种类似的代码:

$("#fontPlusBtn").click(function (){
    $("#textDiv").css("font-size", "*=1.1");
}):

How can I resize all the text nodes (which vary in size) proportionally in this div? 如何在此div中按比例调整所有文本节点(大小不同)?

Update: (Working version) 更新:(工作版)

I combined a few answers here plus altered what I had earlier, so for anyone else stumbling upon this problem later on, here's what I did: 我在这里结合了几个答案,并改变了我之前的内容,所以对于后来遇到这个问题的其他人来说,这就是我所做的:

var upFontSize = function($obj) {
    $obj.children().each(function(index){
        if($(this).children().length > 0){
            upFontSize($(this));
        }else {
            $(this).css({
                "font-size": function(index, value) {
                    return parseFloat(value) * 1.1;
                },
            });
        }
    });
}

This is answers your question and accounts for the multiple text sizes, and sizing them accordingly: 这可以回答您的问题,并考虑多种文字大小,并相应地调整大小:

...And here is a working example: ......这是一个有效的例子:

http://jsfiddle.net/Xr2gj/44/ http://jsfiddle.net/Xr2gj/44/

js: JS:

$(document).ready(function (){
    $("#fontPlusBtn").click(function (){
        $(".textDiv, .textDiv > *").each( function(index){
            $(this).css( {
                "font-size": function(index, value) {
                    return parseFloat(value) * 1.1;
              }
            });
        });
    });
});

css: CSS:

#world{
    font-size: 8pt;
}

#solarSystem{
    font-size: 12pt;
}
#galaxy{
    font-size: 14pt;
}

#universe{
    font-size: 16pt;
}

html: HTML:

<a id="fontPlusBtn" href="#">
    Click to increase font-size
</a>

<div class="textDiv" id="world">
    Hello World!
</div>

<div class="textDiv" id="solarSystem">
    <p><b>Hello Solar System!</b></p>
</div>

<div class="textDiv" id="galaxy">
    <p>Hello Galaxy!</p>
</div>

<div class="textDiv" id="universe">
    <p><div><b><a href="#">Hello Universe!</a></b></div></p>
</div>

​You may be able to mix with some of the other answers, but this http://jsfiddle.net/Xr2gj/44/ shows it working just as you specified. 您可能能够与其他一些答案混合使用,但是这个http://jsfiddle.net/Xr2gj/44/显示它正如您指定的那样工作。

"As of jQuery 1.6, .css() accepts relative values similar to .animate(). Relative values are a string starting with += or -= to increment or decrement the current value. For example, if an element's padding-left was 10px, .css( "padding-left", "+=15" ) would result in a total padding-left of 25px."
reference 参考

So to do that you can use a function callback which will return the actual value, then you return the new value. 所以要做到这一点,你可以使用函数回调,它将返回实际值,然后返回新值。
Like the following. 如下所示。

  $("#fontPlusBtn").click(function (){
        $("#textDiv > *").css("font-size", function(i, value) {
            return parseInt(value) * 1.1;
        });
    });

demo 演示

Here is example - jsFiddle 这是一个例子 - jsFiddle

Here is code: 这是代码:

$("#fontPlusBtn").click(function (){
    var getNr = parseInt($("#textDiv").css("font-size")) * 1.1;
    $(".textDiv").css("font-size", getNr+"px");
});​

Problem is, there isn't any divide or multiply condition in css there is only +=px or -=px . 问题是,css中没有任何除法或乘法条件,只有+=px-=px So what you do is just get the parsed integer from actual element font-size and then multiply it, and then apply it. 所以你要做的就是从实际元素font-size获取解析后的整数,然后将其相乘,然后应用它。

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

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