移动端响应式js代码

最大宽度为640 1rem = 32px:
(function() {
var b = document.documentElement,
a = function() {
var a = b.getBoundingClientRect().width;
b.style.fontSize = .0625 * (640 <= a ? 640 : a) + "px"
},
c = null;
window.addEventListener("resize",
function() {
clearTimeout(c);
c = setTimeout(a, 300)
});
a()
})();
最大宽度为640 1rem = 32px:
(function() {
var b = document.documentElement,
a = function() {
var a = b.getBoundingClientRect().width;
b.style.fontSize = .005 * (640 <= a ? 640 : a) + "px"
},
c = null;
window.addEventListener("resize",
function() {
clearTimeout(c);
c = setTimeout(a, 300)
});
a()
})();

不限制最大宽度:

(function (root) {
var docEl = document.documentElement,
timer = null,
width, last;

function changeRem () {
width = docEl.getBoundingClientRect().width;
if (last === width) { return; }
last = width;
root.rem = width / 20;
if (/ZTE U930_TD/.test(navigator.userAgent)) {
root.rem = root.rem * 1.13;
}
docEl.style.fontSize = root.rem + 'px';
}

changeRem();

root.addEventListener('resize', function () {
clearTimeout(timer);
timer = setTimeout(changeRem, 300);
});

root.addEventListener('orientationchange', function () {
clearTimeout(timer);
timer = setTimeout(changeRem, 300);
});
})(window, undefined);

转载于:https://www.cnblogs.com/anyaran/p/4651542.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值