admin管理员组文章数量:1323157
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
3 Answers
Reset to default 3- Add
script
at end ofbody
- 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
版权声明:本文标题:javascript - How can I zoom html upon button click? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742141845a2422613.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论