html中字体放大和缩小,然后页面刷新后。字体还是放大或者缩小的。这个功能是如何实现的呢?

只需要说一下,实现思路。或者关键字。谢谢咯

首先说放大缩小字体,直接用js调整字体大小就可以了。

要实现刷新后依然显示之前缩放的大小,那就需要让浏览器记住之前的设置,把字体大小存到cookies中,打开网页再从cookies读取。

给你一段写好的供参考,先引入jquery.cookie.js

var myContent = $('#intro, .article-content, .article-content p');
var fSize = parseInt($.cookie("fontSize") ? $.cookie("fontSize") : parseFloat(myContent.css('fontSize'), 10));
var cDomain = document.location.hostname.match(/[a-z0-9][a-z0-9\-]+\.[a-z\.]{2,6}$/i);
var options = {
path: '/',
domain: cDomain,
expires: 30
};
if(fSize){
myContent.css({'font-size': fSize + 'px'});
$.cookie("fontSize", fSize, options);
}
btnFont.click(function(){
$(this).addClass('on').siblings().removeClass('on');
var cID = $(this).attr("id");
if(cID=="btn-font-plus"){
if(fSize>=20) return;
fSize += 2;
}else if(cID=="btn-font-reduce"){
if(fSize<=12) return;
fSize -= 2;
};
myContent.css({fontSize: fSize});
$.cookie("fontSize", fSize, options);
});

具体使用方法请参考cookie文档

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-09-27
<html>
<head>
<title>JavaScript设置网页字体</title>
</head>
<body>
<div id=zoom>
hello world<br>
hello world<br>
hello world<br>
hello world<br>
hello world<br>
hello world<br>
</div>
<SCRIPT language=JavaScript>
var size=1;
function zoomIn(){
size +=0.2;
document.getElementById('zoom').style.fontSize=size+'em'
}
function zoomOut(){
size -=0.2;
document.getElementById('zoom').style.fontSize=size+'em'
}
</SCRIPT>
<P>
<input type="button" value="+" onClick="zoomIn()"/>
<input type="button" value="-" onClick="zoomOut()"/>
</body>
</html>
第2个回答  2017-11-08
定义一个全局变量字号 给放大和缩小按钮点击事件 点击事件中获取字号大小 然后改变他
第3个回答  2017-09-25
设定一个配置文件,这个存在后台或者本地,页面加载的时候读取配置文件
第4个回答  2016-08-29

我知道在浏览器设置里有缩放功能,不知道是你想要的吗?

追问

谢谢你咯。 但是我说的是代码实现,自己写的。jquery+css+html。。。。不是说的网页上自带的

追答

HTML就帮不了你了

本回答被网友采纳
相似回答