如何使用jQuery增加字体大小的变化?

时间:2011-05-12 08:36:06

标签: jquery css typography

您好我想用jQuery逐步改变页面的字体大小我该怎么做?

类似的东西:

$('body').css({'font-size':'+.01px'});
$('body').css({'font-size':'-.01px'});

10 个答案:

答案 0 :(得分:32)

你可以这样做:

var fontSize = parseInt($("body").css("font-size"));
fontSize = fontSize + 1 + "px";
$("body").css({'font-size':fontSize});


jsFiddle example here

答案 1 :(得分:7)

你不能这样做,因为字体属性存储为字符串,如果你确定字体大小将以像素为单位,你可以这样做:

var fontSize = $('body').css('font-size').split('px')[0];

var fontInt = parseInt(fontSize) + 1;

fontSize = fontInt + 'px';

可能需要略微修改我只是在没有测试的情况下编写它。

答案 2 :(得分:4)

可能取决于jquery的哪个版本,但我使用了以下

<强> HTML

<input type="button" id="button" />
<div id="box"></div>

<强> CODE

$(document).ready(function() {
  $("#button").click(function() {
    $("#box").css("width","+=5");
   });
 });

答案 3 :(得分:1)

HTML

<input id="btn" type="button" value="Increase font" /> <br />
<div id="text" style="font-size:10px">Font size</div>

的Javascript

$(document).ready(function() {
    $('#btn').click(function() {
        $('#text').css("font-size", function() {
            return parseInt($(this).css('font-size')) + 1 + 'px';
        });
    });
});

演示:http://jsfiddle.net/ynhat/CeaqU/

答案 4 :(得分:1)

使用类似的东西

$(document).ready(function() {
    $('id or class of what input').click(function() {
        $('div, p, or span of what font size your increasing').css("font-size", function() {
            return parseInt($(this).css('font-size')) + 1 + 'px';
        });
    });
});

答案 5 :(得分:1)

使用非px字体大小时要小心 - 当字体大小为1.142857em时解析int时使用Sylvain的代码会导致2px!

parseInt(1.142857em)== 1

1 + 1 +'px'== 2px

答案 6 :(得分:1)

尝试如下:

jQuery("body *").css('font-size','+=1');

答案 7 :(得分:0)

以下是我如何使用jQuery UI滑块执行此操作的示例。

示例here

HTML:

    <div class="container">
  <div class="row">
    <div class="col-md-12">
      <div class="text">
        <h1>Hello, increase me or decrease me!</h1>
      </div>
    </div>
  </div>
  <div class="row">
    <div class="col-md-12">
      <div id="mySlider"></div>
    </div>
  </div>
</div>

CSS:

.text h1 {
  color: #333;
  font-family: Arial, sans-serif;
  font-size: 30px;
}

JS:

$("#mySlider").slider({
  range: "min",
  min: 30,
  max: 70,
  slide: function(e, u) {
    $(".text h1").css("font-size", u.value + "px"); // We want to keep 30px as "default"
  }

});

答案 8 :(得分:0)

    $(document).ready(function () {

        var i = 15;

        $("#myBtn").click(function () {
            if (i >= 0) {
             i++
                var b = $("#myText").css({"background-color": "yellow", "font-size": i})

            }
        })
//If you want to decrease it too
        $("#myBtn2").click(function () {
            if (i <= 500) {

                i--
             var b = $("#myText").css({"background-color": "yellow", "font-size": i})

            }
        })
    })

答案 9 :(得分:0)

此解决方案使用百分比而不是使用px,也不需要在HTML中设置大小或需要样式标题。

HTML:

<p>This is a paragraph.</p>
<button id="btn">turn up</button>

jQuery的:

var size = "100%";
$("#btn").click(function() {
size = parseFloat(size)*1.1+"%";// multiplies the initial size by 1.1
    $("p").css("fontSize", size);// changes the size in the CSS
});

<强> Working jsfiddle here