当前位置 : 主页 > 网络编程 > JavaScript >

尺寸屏幕探测+适配

来源:互联网 收集:自由互联 发布时间:2021-06-28
gistfile1.txt (function (doc, win) { var docEl = doc.documentElement, resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',/* recalc函数实现了自适应屏幕的功能,不需要改动 */recalc = function () { var
gistfile1.txt
(function (doc, win) {   
	var docEl = doc.documentElement,   
		resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
		/* recalc函数实现了自适应屏幕的功能,不需要改动 */
		recalc = function () {   
			var clientWidth = docEl.clientWidth,
				scale = 1 / window.devicePixelRatio,
				uiWidth = 750;   
			if (!clientWidth) return;
			document.querySelector('meta[name="viewport"]').setAttribute('content', 'width=device-width, initial-scale='+ scale +', maximum-scale='+ scale +', minimum-scale='+ scale +', user-scalable = no');  
			docEl.style.fontSize = 100 * (clientWidth / uiWidth) + 'px';   
		},
		/* 业务逻辑在init函数里,在此添加业务逻辑 */
		init = function () {
			var getDom = function (id) { return doc.getElementById(id) };
			getDom('forgetPassword').addEventListener('click', function(){
				getDom('mainBox').className += ' find';
			}, false);
		};   
	if (!doc.addEventListener) return;   
	win.addEventListener(resizeEvt, recalc, false);   
	doc.addEventListener('DOMContentLoaded', recalc, false);
	doc.addEventListener('DOMContentLoaded', init, false); 
})(document, window);
上一篇:mouse drag drop
下一篇:扩展js对象的变量
网友评论