移动端网站适配代码【优化】

移动端优化 下面是移动适配js代码   

var match,
    scale,
    TARGET_WIDTH = 320;

if (match = navigator.userAgent.match(/Android (\d+\.\d+)/)) {
    if (parseFloat(match[1]) < 4.4) {
        if (TARGET_WIDTH == 320) TARGET_WIDTH++;
        var scale = window.screen.width / TARGET_WIDTH;
        document.querySelector('meta[name="viewport"]').setAttribute('content', 'width=' + TARGET_WIDTH + ', initial-scale = ' + scale + ', target-densitydpi=device-dpi');
    }
} else {
    document.querySelector('meta[name="viewport"]').setAttribute('content', 'width=' + TARGET_WIDTH);
}
作者:邹润阳
测试页面代码


 lang="en">

     charset="UTF-8">
     name="viewport" content="width=250, initial-scale=1.5, user-scalable=no">
    </span>Document<span class="nt" style="color:rgb(32,74,135);font-weight:700;">
    


     id="first" class="block">100px
id="second" class="block">200px
id="third" class="block">300px
id="fourth" class="block">320px
id="log">
作者:
邹润阳

这篇文章感觉很有用 没事儿就鼓捣鼓捣 试试吧

你可能感兴趣的:(优化)