zoukankan      html  css  js  c++  java
  • 终端适配万精油代码——1

     <script>
        (function(){
          var __screen_width__ = document.documentElement.clientWidth;
          var __screen_height__ = document.documentElement.clientHeight;
          var __fontSize__ = __screen_width__ * 100 / 750 + "px";
          document.documentElement.style.fontSize = __fontSize__;
          window.__fontSize__ = __fontSize__;
          window.__screen_width__ = __screen_width__;
          window.__screen_height__ = __screen_height__;
        })();
        </script>

    这是首次接触移动端做微信公众号的东西,查了很多屏幕适配方面的资料,有用@media的,要固定的设置各种尺寸下不同元素的大小挺麻烦的。也有用其它方式的,所以这篇只是适配方式之一。

    引如这个代码块,在此之前设置了

    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, minimum-scale=1, user-scalable=no">
        

    主要是修改font-size的基本值,之后在css里面全部用rem去写大小,因为rem只根据根文档的font-size大小来计算,关于rem,在css分类里面也有介绍它以及与em的区别。

    代码中750是设计图的宽度。设置元素大小全部是在设计图的像素上%100+rem。

    文章更新——————————————————————————————————————————

    今天在开发群里瞎晃哒,看见一个哥们发的:

    (function (doc, win) {
        var docEl = doc.documentElement,
            resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
            recalc = function () {
                var clientWidth = docEl.clientWidth;
                if (!clientWidth) return;
                docEl.style.fontSize = 100 * (clientWidth / 750) + 'px';
            };
        if (!doc.addEventListener) return;
        win.addEventListener(resizeEvt, recalc, false);
        doc.addEventListener('DOMContentLoaded', recalc, false);
    })(document, window);
  • 相关阅读:
    Intellij IDEA 使用spring-boot-devtools无效解决办法一
    使用docker安装myql/redis等软件
    mybatis generator插件系列--分页插件
    mybatis generator插件系列--lombok插件 (减少百分之九十bean代码)
    linux设置端口转发(一键设置)
    redis教程(The little redis book中文版)
    Redis 5种数据结构及其使用场景举例--STRING
    String中hashCode方法的线程安全
    java ShutdownHook介绍与使用
    ACM 模板库
  • 原文地址:https://www.cnblogs.com/yaoyao-sun/p/7111319.html
Copyright © 2011-2022 走看看