移动端响应式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);

posted @ 2015-07-16 15:53  安雅然  阅读(725)  评论(0编辑  收藏  举报