开发者

How to increment font size changes with jQuery?

Hi I want to change the font size of the page incrementally with jQ开发者_JAVA百科uery how do I do that?

Something like:

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


You could do so :

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


jsFiddle example here


You can't do it like that because the font propery is stored as a string, if you are sure the font size will be in pixels you could do it like this:

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

var fontInt = parseInt(fontSize) + 1;

fontSize = fontInt + 'px';

That might need modifying slightly I just wrote it without testing.


It might depend on which version of jquery, but I have used the following

HTML

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

CODE

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


Try this:

jQuery("body *").css('font-size','+=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';
        });
    });
});

Demo: http://jsfiddle.net/ynhat/CeaqU/


Use something like this

$(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';
        });
    });
});


Be careful when using non-px font sizes -- Using Sylvain's code when parsing for an int when the font-size is 1.142857em would result in 2px!

parseInt(1.142857em) == 1

1 + 1 + 'px' == 2px


Here's an example of how I did it with jQuery UI slider.

Example 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"
  }

});


    $(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})

            }
        })
    })


This solution uses percentages instead of using px and also does not require the size to be set or require style headers in the 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜