• We just launched and are currently in beta. Join us as we build and grow the community.

JavaScript - Dynamic Font Resizer

N3verBack

Esports Strategist
N Rep
0
0
0
Rep
0
N Vouches
0
0
0
Vouches
0
Posts
94
Likes
41
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial we will create a Dynamic Font Resizer using JavaScript. This code can change the font size to any part of the web page dynamically. The code use onclick function to put up a switch statement to determine the case value in order to rewritten the font size of the web page text. Feel free to modify and apply it in your system, this is a user-friendly kind of program

We will be using JavaScript as a server-side scripting language because It gives a greater control of your web page and extend its capability in a modern way approach. It is written in HTML or as an external sourcing to add some necessary features in your website.

Getting started:

First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    >
  3. <head

    >
  4. <meta

    charset

    =

    "UTF-8"

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1"

    /

    >
  5. <link

    rel

    =

    "stylesheet"

    type

    =

    "text/css"

    href

    =

    "css/bootstrap.css"

    /

    >
  6. </

    head

    >
  7. <body

    >
  8. <nav class

    =

    "navbar navbar-default"

    >
  9. <div

    class

    =

    "container-fluid"

    >
  10. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    >

    Sourcecodester</

    a

    >
  11. </

    div

    >
  12. </

    nav>
  13. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  14. <div

    class

    =

    "col-md-6 well"

    >
  15. <h3

    class

    =

    "text-primary"

    >

    JavaScript - Dynamic Font Resizer</

    h3

    >
  16. <hr

    style

    =

    "border-top:1px dotted #ccc;"

    /

    >
  17. <div

    class

    =

    "col-md-2"

    ></

    div

    >
  18. <div

    class

    =

    "col-md-8"

    >
  19. <div

    class

    =

    "form-inline"

    >
  20. <label

    >

    Font size:</

    label

    >
  21. <select

    class

    =

    "form-control"

    id

    =

    "size"

    >
  22. <option

    value

    =

    "8pt"

    >

    8pt</

    option

    >
  23. <option

    value

    =

    "10pt"

    >

    10pt</

    option

    >
  24. <option

    value

    =

    "11pt"

    >

    11pt</

    option

    >
  25. <option

    value

    =

    "12pt"

    >

    12pt</

    option

    >
  26. <option

    value

    =

    "14pt"

    >

    14pt</

    option

    >
  27. <option

    value

    =

    "16pt"

    >

    16pt</

    option

    >
  28. <option

    value

    =

    "18pt"

    >

    18pt</

    option

    >
  29. </

    select

    >
  30. <button

    type

    =

    "button"

    onclick

    =

    "resize();"

    class

    =

    "btn btn-primary"

    >

    Resize</

    button

    >
  31. </

    div

    >
  32. <br

    /

    >
  33. <div

    id

    =

    "result"

    class

    =

    "col-md-12"

    style

    =

    "height:200ox; border:1px solid #000;"

    >
  34. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse blandit molestie lobortis. Donec at quam quam. Sed iaculis ut libero quis feugiat. Donec risus ipsum, malesuada eu sodales eu, pretium ac est. In nec lacinia arcu. Sed congue, nibh vel fringilla finibus, turpis lectus condimentum lectus, ultrices iaculis enim mauris eget sapien. Integer ultrices sit amet elit sed vestibulum. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum dapibus, erat et hendrerit laoreet, dolor dolor finibus tortor, eget tempus eros purus nec neque. Nunc dictum neque consequat aliquam ornare. Nulla facilisi. Proin id ex in ante molestie laoreet sed tempor quam. In tristique sem leo, nec scelerisque libero interdum id. Pellentesque non leo dolor. Nullam ac auctor metus. Fusce faucibus quam id suscipit accumsan.
  35. </

    div

    >
  36. </

    div

    >
  37. </

    div

    >
  38. <script

    src

    =

    "js/script.js"

    ></

    script

    >
  39. </

    body

    >
  40. </

    html

    >

Creating the Script

This code contains the script of the application. This code will dynamically change the web page text when the button is clicked. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
  1. function

    resize(

    )

    {
  2. var

    size =

    document.getElementById

    (

    'size'

    )

    .value

    ;
  3. var

    text =

    document.getElementById

    (

    'result'

    )

    ;

  4. switch

    (

    size)

    {
  5. case

    "8pt"

    :
  6. text.style

    .fontSize

    =

    "8px"

    ;
  7. break

    ;
  8. case

    "10pt"

    :
  9. text.style

    .fontSize

    =

    "10px"

    ;
  10. break

    ;
  11. case

    "11pt"

    :
  12. text.style

    .fontSize

    =

    "11px"

    ;
  13. break

    ;
  14. case

    "12pt"

    :
  15. text.style

    .fontSize

    =

    "12px"

    ;
  16. break

    ;
  17. case

    "14pt"

    :
  18. text.style

    .fontSize

    =

    "14px"

    ;
  19. break

    ;
  20. case

    "16pt"

    :
  21. text.style

    .fontSize

    =

    "16px"

    ;
  22. break

    ;
  23. case

    "18pt"

    :
  24. text.style

    .fontSize

    =

    "18px"

    ;
  25. break

    ;
  26. }
  27. }

There you have it we successfully created a Dynamic Font Resizer using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,526

323,535

Top