zoukankan      html  css  js  c++  java
  • webapp思路和rem适配极其viewport

    webapp在制作时候,页面上要加入viewport标签,用来进行适配;

    viewport的meta标签,指的是在移动端显示的时候,viewport是多大?移动端的浏览器是屏幕宽,viewport一般(手机浏览器)默认的是960px左右,就把页面压缩到

    960px显示,所以是比手机屏幕宽的哟.等比放小,所以,为了显示的好看,要设定viewport为屏幕宽,就是:如此标签

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

    设定width就是设备的宽度,然后就将页面大小缩放这么大显示.所以,在很多时候,在webapp页面的时候,我们直接在pc端测试就行,直接设计640px的宽度来做,然后html和body 的

    宽度设为100%;主要容器宽度就是640px,然后,适配在手机端的时候,就会将其略压缩显示在手机的viewport上,大概样子差不多,而且,在加上rem的适配,一般没问题的.

    而rem适配,是根据html这个根元素来进行的,1rem为这个根元素的字体大小.若将其设为10px,那么1rem就是10px,之后的所有都用rem来表示,然后,在用js或者是css的media query来实现不同屏幕大小时候不容的html的字体,然后,其余所有部分,都会跟随这成比例改变了的.就是这个原理的.

    讲的,实际上也就是两点:1,viewport的作用;2,rem进行适配,当然也有用100%比的,不过out了

    列出测试成功的rem适配的css和js代码:

    css:

    @media only screen and (max- 320px){html{font-size: 9px;} }
    @media only screen and (min- 320px) and (max- 352px){html{font-size: 10px;} }
    @media only screen and (min- 352px) and (max- 384px){html{font-size: 11px;} }
    @media only screen and (min- 384px) and (max- 416px){html{font-size: 12px;} }
    @media only screen and (min- 416px) and (max- 448px){html{font-size: 13px;} }
    @media only screen and (min- 448px) and (max- 480px){html{font-size: 14px;} }
    @media only screen and (min- 480px) and (max- 512px){html{font-size: 15px;} }
    @media only screen and (min- 512px) and (max- 544px){html{font-size: 16px;} }
    @media only screen and (min- 544px) and (max- 576px){html{font-size: 17px;} }
    @media only screen and (min- 576px) and (max- 608px){html{font-size: 18px;} }
    @media only screen and (min- 608px) and (max- 640px){html{font-size: 19px;} }
    @media only screen and (min- 640px){html{font-size: 20px;} }

    js:

    (function (doc, win) {
    var docEl = doc.documentElement,
    resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
    recalc = function () {
    var clientWidth = docEl.clientWidth;
    if (!clientWidth) return;
    if(clientWidth>640){
        clientWidth = 640;
    }
    //这行代码有诈,讲10改成更小的数字,比如5,就没有反应了...字大小可以,但
    //div的宽高就不变了也是醉了
    docEl.style.fontSize = 10 * (clientWidth / 320) + 'px';
    
    };
    
    if (!doc.addEventListener) return;
    win.addEventListener(resizeEvt, recalc, false);
    doc.addEventListener('DOMContentLoaded', recalc, false);
    })(document, window);
  • 相关阅读:
    Struts2+Spring3+Mybatis3开发环境搭建
    spring+struts2+mybatis
    【LeetCode】Populating Next Right Pointers in Each Node
    【LeetCode】Remove Duplicates from Sorted Array
    【LeetCode】Remove Duplicates from Sorted Array II
    【LeetCode】Binary Tree Inorder Traversal
    【LeetCode】Merge Two Sorted Lists
    【LeetCode】Reverse Integer
    【LeetCode】Same Tree
    【LeetCode】Maximum Depth of Binary Tree
  • 原文地址:https://www.cnblogs.com/modle-sherlock/p/5462891.html
Copyright © 2011-2022 走看看