JS控制字体大小,刷新不变 发表于 2020-01-28 分类于 前端 阅读次数: 本文字数: 1.6k 阅读时长 ≈ 3 分钟 JS控制字体大小,刷新不变 效果: 链接:https://codepen.io/nigatoo/pen/jOEeNWr HTML:12345678910111213<br><div class="font-resizer"><div class="font-sizer-buttons"> <a href="#" id="plustext" class="button" title="Increase Font Size"><span>放大+</span></a> <a href="#" id="minustext" class="button" title="Decrease Font Size"><span>缩小-</span></a></div> </div> <div id="text"> <p>Font Size</p></div> CSS:123456789101112.button{ margin-left:10px; background-color: #4CAF50; /* Green */ border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; border-radius: 5px;} JS:1234567891011121314function resizeText(multiplier) { var elem = document.getElementById("text"); var currentSize = elem.style.fontSize || 1; elem.style.fontSize = ( parseFloat(currentSize) + (multiplier * 0.2)) + "em"; localStorage.setItem('currentSize',currentSize);}document.getElementById("plustext").addEventListener("click", function() {resizeText(1);});document.getElementById("minustext").addEventListener("click", function() {resizeText(-1);});window.onload = function(){ var g = localStorage.getItem('currentSize'); document.getElementById("text").style.fontSize = g;} -------------本文结束 感谢您的阅读------------- 转载别忘了注明出处哈 🍭🍭🍭😜 请我吃糖 微信支付 支付宝