click a button to change text font-size of <body> in javascript/css

血红的双手。 提交于 2019-12-04 19:21:16

Maybe it will be more convenient to use CSS and additional classes on body to change base font-size? Related these classes you also can change sizes of other elements without JavaScript/JQuery coding.

HTML

<button class="js-font" data-size="big">Bigger</button>
<button class="js-font" data-size="normal">Normal</button>
<button class="js-font" data-size="small">Smaller</button>

CSS

body, body.normal {
    font-size: 16px;
}
body.big {
    font-size: 36px;
}
body.small {
    font-size: 12px;
}

JQuery

$(function() {

    $('.js-font').on('click', function() {
        $('body')
            .attr('class', '')
            .addClass($(this).data('size'));
    });

});

See fiddle http://jsfiddle.net/shurshilin/94cnutdr/

Hop, it'll help you.

You can actually accomplish this in a far simpler way than you may think.

By setting a starting font size on the body, you can set the font size of your other elements to use relative em units (1em = 1xbody font size in this case, so if the body font size is 14px, 1em = 14px). You can then use Javascript to increase / decrease the body font size, thereby impacting the relative font sizes of these elements.

Animation can be handled using a CSS transition alone.

$(document).ready(function() {
  $('#increase, #decrease').on('click', function() { // click to increase or decrease
    var btn = $(this),
      fontSize = parseInt(window.getComputedStyle(document.body, null).fontSize, 0); // parse the body font size as a number
    if (btn[0].id === "increase") { // detect the button being clicked
      fontSize++; // increase the base font size
    } else {
      fontSize--; // or decrease
    }
    document.body.style.fontSize = fontSize + 'px'; // set the body font size to the new value
  });
});
body {
  font-size: 14px;
}
h1,
h2 {
  transition: font-size 200ms; /* animate font size changes */
}
h1 {
  font-size: 2em; /* h1 element is 2x body font size */
}
h2 {
  font-size: 1.5em; /* h1 element is 1.5x body font size */
}
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>

<button id="increase">increase +</button>
<button id="decrease">decrease -</button>

<h1>Header 1</h1>

<h2>Header 2</h2>

If you use font-sizes with unit 'em' everywhere except for the body, you can change the body font-size and all others will also change.

JSFiddle

$(document).ready(function() {
    $('.change-fs').click(function() {
        $('.body').css('font-size', $(this).attr('data-size'));
    });
});
.body {
    font: 400 10pt sans-serif;
}

h1 {
    font-size: 2em;
}

h2 {
    font-size:1.5em;
}

p {
    font-size: 1em;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="body">
    <h1>A header, really big</h1>
    <h2>A secondary header, still quite big</h2>
    <p>Normal text, default font-size</p>
    <div class="fs-ctrl">
        <div data-size="10pt" class="change-fs">10pt</div>
        <div data-size="12pt" class="change-fs">12pt</div>
        <div data-size="16pt" class="change-fs">16pt</div>
        <div data-size="20pt" class="change-fs">20pt</div>
    </div>
</section>

I wrote a jQuery plugin to accomplish this on more complex existing website designs. https://github.com/msigley/jQuery-Chaos-Fontsize-Selector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!