admin管理员组

文章数量:1323502

I am trying to provide a function on an html page with which I can zoom in and out text.

I already tried with all available browsers. Here is my code:

<!DOCTYPE html>
<html>
    <head>
        <style type="text/css">
        #zoomtext
        {
            transform: scale(1);
            transition: transform 0.2s ease-in-out;
        }
        </style>
        <script>
        window.addEventListener("load", function()
        {
            var zoom = 1;
            var zoomStep = 0.2;
        });
        document.getElementById("zoomIn").addEventListener("click",function(){zoom += zoomStep; document.getElementById("zoomtext").style.transform = "scale("+zoom+")";});
        document.getElementById("zoomOut").addEventListener("click",function(){if(zoom > zoomStep){zoom -= zoomStep; document.getElementById("zoomtext").style.transform = "scale("+zoom+")";}});
        </script>
    </head>
    <body>
        <button id="zoomOut">-</button>
        <button id="zoomIn">+</button>
        <article>
            <section id="zoomtext">
                <p>This is a text that I want to be able to zoom in and out by means of the two buttons indicating plus and minus.</p>
            </section>
        </article>
    </body>
</html>

I am trying to provide a function on an html page with which I can zoom in and out text.

I already tried with all available browsers. Here is my code:

<!DOCTYPE html>
<html>
    <head>
        <style type="text/css">
        #zoomtext
        {
            transform: scale(1);
            transition: transform 0.2s ease-in-out;
        }
        </style>
        <script>
        window.addEventListener("load", function()
        {
            var zoom = 1;
            var zoomStep = 0.2;
        });
        document.getElementById("zoomIn").addEventListener("click",function(){zoom += zoomStep; document.getElementById("zoomtext").style.transform = "scale("+zoom+")";});
        document.getElementById("zoomOut").addEventListener("click",function(){if(zoom > zoomStep){zoom -= zoomStep; document.getElementById("zoomtext").style.transform = "scale("+zoom+")";}});
        </script>
    </head>
    <body>
        <button id="zoomOut">-</button>
        <button id="zoomIn">+</button>
        <article>
            <section id="zoomtext">
                <p>This is a text that I want to be able to zoom in and out by means of the two buttons indicating plus and minus.</p>
            </section>
        </article>
    </body>
</html>

There is no error message. Nothing happens upon click.

Share edited Jun 11, 2019 at 15:48 Anshu 1,3372 gold badges16 silver badges28 bronze badges asked Jun 11, 2019 at 14:45 user9user9 1451 gold badge1 silver badge13 bronze badges 1
  • you either need to run the script on document ready, or after you add the html to the page - at the moment the buttons do not exist when you do your getelementbyid as you are just running the script in the header – Pete Commented Jun 11, 2019 at 14:52
Add a ment  | 

3 Answers 3

Reset to default 3
  • Add script at end of body
  • Assign zoom, zoomStep variables outside eventlistener callback

<!DOCTYPE html>
<html>

<head>
  <style type="text/css">
    #zoomtext {
      transform: scale(1);
      transition: transform 0.2s ease-in-out;
    }
  </style>
</head>

<body>
  <button id="zoomOut">-</button>
  <button id="zoomIn">+</button>
  <article>
    <section id="zoomtext">
      <p>This is a text that I want to be able to zoom in and out by means of the two buttons indicating plus and minus.</p>
    </section>
  </article>

  <script>
    var zoom = 1;
    var zoomStep = 0.2;

    document.getElementById("zoomIn").addEventListener("click", function() {
      zoom += zoomStep;
      document.getElementById("zoomtext").style.transform = "scale(" + zoom + ")";
    });
    document.getElementById("zoomOut").addEventListener("click", function() {
      if (zoom > zoomStep) {
        zoom -= zoomStep;
        document.getElementById("zoomtext").style.transform = "scale(" + zoom + ")";
      }
    });
  </script>

</body>

</html>

If you need to set the zoom of the whole window, that might not be possible without an external plugin (see Changing the browser zoom level)

If you want to set the zoom for specifically the #zoomtext element, you could use the "zoom" CSS attribute (https://developer.mozilla/en-US/docs/Web/CSS/zoom), but it is not standard. Another way of handling this would be to increase or decrease the font-size CSS attribute on the #zoomtext element:

document.getElementById("zoomtext").style.fontSize = '12px'

The issue with your code is that zoom and zoomStep are never evaluated. The first event listener just create a function, but the code inside it is never executed.

See a working code here : https://codepen.io/marc-simplon/pen/vqYwYQ?editors=1010

本文标签: javascriptHow can I zoom html upon button clickStack Overflow