zoukankan      html  css  js  c++  java
  • 关于移动端的布局

    第一种,flexible布局:

    ;(function(designWidth, maxWidth) {
        var doc = document,
            win = window,
            docEl = doc.documentElement,
            remStyle = document.createElement("style"),
            tid;
    
        function refreshRem() {
            var width = docEl.getBoundingClientRect().width;
            maxWidth = maxWidth || 540;
            width > maxWidth && (width = maxWidth);
            var rem = width * 100 / designWidth;
            remStyle.innerHTML = 'html{font-size:' + rem + 'px;}';
        }
    
        if (docEl.firstElementChild) {
            docEl.firstElementChild.appendChild(remStyle);
        } else {
            var wrap = doc.createElement("div");
            wrap.appendChild(remStyle);
            doc.write(wrap.innerHTML);
            wrap = null;
        }
        //要等 wiewport 设置好后才能执行 refreshRem,不然 refreshRem 会执行2次;
        refreshRem();
    
        win.addEventListener("resize", function() {
            clearTimeout(tid); //防止执行两次
            tid = setTimeout(refreshRem, 300);
        }, false);
    
        win.addEventListener("pageshow", function(e) {
            if (e.persisted) { // 浏览器后退的时候重新计算
                clearTimeout(tid);
                tid = setTimeout(refreshRem, 300);
            }
        }, false);
    
        if (doc.readyState === "complete") {
            doc.body.style.fontSize = "16px";
        } else {
            doc.addEventListener("DOMContentLoaded", function(e) {
                doc.body.style.fontSize = "16px";
            }, false);
        }
    })(750, 750);
    

    rem=px/50;

    第二种,media css实现:

    html {
        font-size:20px
    }
    @media only screen and (min-400px) {
        html {
            font-size:21.33333333px!important
        }
    }
    @media only screen and (min-414px) {
        html {
            font-size:22.08px!important
        }
    }
    @media only screen and (min-480px) {
        html {
            font-size:25.6px!important
        }
    }
    

    rem=px/20

  • 相关阅读:
    JZ36 两个链表的第一个公共结点
    程序员的表达能力
    Git学习(2)-使用Git 代码将本地文件提交到 GitHub
    初识模块
    三元表达式、递归、匿名函数
    CSRF
    XSS前置课程--同源策略
    XSS
    SQL注入基础入门
    Linux下ettercap的安装,make安装软件步骤
  • 原文地址:https://www.cnblogs.com/jone-chen/p/10813538.html
Copyright © 2011-2022 走看看