实现效果:根据屏幕大小自动调整字体大小

1.普通方法

$(function(){
function setRem(){
var clientWidth=$(window).width();
var nowRem=(100*clientWidth/375);/*375这个值是设计图中测量值的一半*/
$("html").css("font-size",parseInt(nowRem, 10)+"px");
};
setRem();
$(function(){
var timer;
$(window).bind("resize",function(){
clearTimeout(timer)
timer=setTimeout(function(){
setRem();
}, 100)
})
});
});

2.移动设备,带事件操作

(function () {
var supportsOrientationChange = 'onorientationchange' in window ? 'orientationchange' : 'resize';
var timeoutId;
function setRem() {
var clientWidth = document.documentElement.clientWidth;
var nowPX = clientWidth / 375 * 100;
document.documentElement.style.fontSize = nowPX + 'px';
}
setRem();
window.addEventListener(supportsOrientationChange, function () {
clearTimeout(timeoutId);
timeoutId = setTimeout(function () {
setRem();
}, 300);
}, false);
})();

相关文章:

  • 2021-12-24
  • 2022-12-23
  • 2022-02-22
  • 2022-12-23
  • 2022-12-23
  • 2021-10-21
  • 2021-12-13
猜你喜欢
  • 2021-06-13
  • 2021-11-06
  • 2021-08-04
  • 2022-12-23
  • 2021-09-29
相关资源
相似解决方案