Android viewport setting “user-scalable=no” breaks width / zoom level of viewport

后端 未结 3 2139
终归单人心
终归单人心 2020-12-04 16:23

I am working on a web app which has a width of 640px.

In the document head I set

  

        
3条回答
  •  被撕碎了的回忆
    2020-12-04 16:36

    I wanted mobile to always show a website 640px wide because of a design that would break otherwise. (a design I did not make..) Thereby I wanted to disable zooming for mobile users. What worked for me me is the following:

    - UPDATED 2013-10-31

    First of all, there is no way you can do this without Javascript. You will have to check the user agent string. Therefore I created a mobile-viewport.js and included the script just before the closing tag:

    function writeViewPort() {
        var ua = navigator.userAgent;
        var viewportChanged = false;
        var scale = 0;
    
        if (ua.indexOf("Android") >= 0 && ua.indexOf("AppleWebKit") >= 0) {
            var webkitVersion = parseFloat(ua.slice(ua.indexOf("AppleWebKit") + 12));
            // targets android browser, not chrome browser (http://jimbergman.net/webkit-version-in-android-version/)
            if (webkitVersion < 535) {
                viewportChanged = true;
                scale = getScaleWithScreenwidth();
                document.write('');
            }
        }
    
        if (ua.indexOf("Firefox") >= 0) {
            viewportChanged = true;
            scale = (getScaleWithScreenwidth() / 2);
            document.write('');
        }
    
        if (!viewportChanged) {
            document.write('');
        }
    
        if (ua.indexOf("IEMobile") >= 0) {
            document.write('');
        }
    
        document.write('');
    }
    
    function getScaleWithScreenwidth() {
        var viewportWidth = 640;
        var screenWidth = window.innerWidth;
        return (screenWidth / viewportWidth);
    }
    
    writeViewPort();
    

    The script checks if the visitor has an android (not chrome) or firefox browser. The android browser does not support the combination of width=640 and user-scalable=false, and the firefox browser does have a double screen width for some strange reason. If the visitor has a windows phone IE browser MobileOptimized is set.

提交回复
热议问题