判断客户端是PC还是移动端问题的解决方案

今天在帮 莲的Live 2D 做浏览器适配的时候学会的一段代码

利用 Javascript 进行判断

function isPC() { //是否为PC端
	var userAgentInfo = navigator.userAgent;
	var Agents = ["Android", "iPhone",
	              "SymbianOS", "Windows Phone",
	              "iPad", "iPod"
	             ];
	var flag = true;
	for (var v = 0; v < Agents.length; v++) {
		if (userAgentInfo.indexOf(Agents[v]) > 0) {
			flag = false;
			break;
		}
	}
	return flag;
}

然后在 Live 2D 初始化的地方进行调用即可

if (isPC()) // 检测是否为 PC 端,移动端不加载
        initDefine(resourcesPath, backImageName, modelDir); // 初始化模型

The desire of his soul is the prophecy of his fate
你灵魂的欲望,是你命运的先知。

原文地址:https://www.cnblogs.com/RioTian/p/14773677.html