移动端rem与px适应js

方法一:

(function (doc, win) {
				var docEl = doc.documentElement,
					resizeEvt = "orientationchange" in window ? "orientationchange" : "resize",
					recalc = function () {
						var clientWidth = docEl.clientWidth;
						//var foot = document.getElementById("foot");
						if (!clientWidth) return;
						if (clientWidth<640){
							docEl.style.fontSize = 120 * (clientWidth / 640) + "px";
							console.log(120 * (clientWidth / 640) + "px");
						}else{
							docEl.style.fontSize = "120px";
						}
					};

				if (!doc.addEventListener) return;
				win.addEventListener(resizeEvt, recalc, false);
				doc.addEventListener('DOMContentLoaded', recalc, false);
			})(document, window);  

方法二:

var deviceWidth = document.documentElement.clientWidth;
			if (deviceWidth > 640) deviceWidth = 640;
			document.documentElement.style.fontSize = deviceWidth / 3.75 + 'px';  
posted @ 2018-12-11 10:21  漫天风沙  阅读(715)  评论(0编辑  收藏  举报