简体   繁体   中英

How to let users increase font-size by clicking the text?

I've been trying to figure how to create an idea I had the other day for awhile now and can't seem to get it to work due to being a bit of noob still.

Anyway, to the point, I am trying to create as the title says, a paragraph where the user can simply click on the text / paragraph and have the font size increase each time.

I was able to get a working form where I could put in a multiple choice box and have the users change size with that, but that simply isn't what I want to do..

I was thinking it would look something like this:

<script type="text/javascript" language="javascript">
do
    {
    if (user-clicks) increase font size;
    else keep current font size;    
    }
while (font-size < 4em)
</script>

Can anyone more experienced please help me getting this to work or at least put me on a path where I can more successfully figure it out myself? Thanks in advance for any help!

<html>
<head>
<script language="javascript">

function resizeText(multiplier) {
    multiplyText(multiplier, document.getElementById('myContent'));    
}

function multiplyText(multiplier, txtobj) {
    //keep current font size
    if (txtobj.style.fontSize == '') {
        txtobj.style.fontSize = "100%";
    }
    //keep current font size
    if (multiplier == 0) {
        txtobj.style.fontSize = "100%";
    } 
    else { //get only the number part of the fontsize
    txtobj.style.fontSize = parseFloat(txtobj.style.fontSize) + multiplier + "%";
    }
}


</script>
</head>

<body>
<p id="myContent">
 <a href="javascript:resizeText(10);"> Increase font</a></p>
</body>
</html>

Working Code with JQuery:

 <html>
    <head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(e) {
        var gFont=1;
    $('.fontAuto').click(function(e) {
        if(gFont<4){
                gFont=gFont+0.5;
        $('.fontAuto').css('font-size',gFont+'em');
        }else{
            gFont =1;
            $('.fontAuto').css('font-size','1em');
        }
    });

    });
    </script>
    <style type='text/css'>

    .fontAuto{
        font-size:1em;
    }
    </style>
    </head>
    <body>
    <p class="fontAuto">Text For testing</p>

    </body>
    </html>

Giving you a very short & simple answer-

<p id=p1 style="font-size:25">
    click <a href="javascript:document.getElementById('p1').style.fontSize=50">here</a> to enlarge.
</p>

In case you want to do it on click at any part of the <p>...</p> tag use-

<p id=p1 style="font-size:25" onclick="this.style.fontSize=50">
    click to enlarge.
</p>

Customize it as you wish.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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